91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

WPF中鼠標拖拽事件怎么封裝

wpf
小億
84
2024-03-08 15:01:25
欄目: 編程語言

在WPF中,可以使用MouseLeftButtonDownMouseLeftButtonUpMouseMove事件來實現鼠標拖拽功能的封裝。以下是一個簡單的示例:

public class DraggableControl : Control
{
    private bool isDragging = false;

    public DraggableControl()
    {
        this.MouseLeftButtonDown += DraggableControl_MouseLeftButtonDown;
        this.MouseLeftButtonUp += DraggableControl_MouseLeftButtonUp;
        this.MouseMove += DraggableControl_MouseMove;
    }

    private void DraggableControl_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        if (e.LeftButton == MouseButtonState.Pressed)
        {
            isDragging = true;
            this.CaptureMouse();
        }
    }

    private void DraggableControl_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {
        if (isDragging)
        {
            isDragging = false;
            this.ReleaseMouseCapture();
        }
    }

    private void DraggableControl_MouseMove(object sender, MouseEventArgs e)
    {
        if (isDragging)
        {
            Point currentPosition = e.GetPosition(this.Parent as UIElement);
            Canvas.SetLeft(this, currentPosition.X - (this.ActualWidth / 2));
            Canvas.SetTop(this, currentPosition.Y - (this.ActualHeight / 2));
        }
    }
}

使用這個DraggableControl類可以方便地給任意的WPF控件添加鼠標拖拽功能。只需要在XAML文件中聲明DraggableControl替代原本的控件即可。

0
铜梁县| 呼玛县| 忻城县| 满城县| 齐齐哈尔市| 汉沽区| 宽甸| 金堂县| 昌图县| 河北省| 介休市| 黔东| 霍山县| 博湖县| 灵璧县| 民乐县| 怀来县| 肇源县| 奉新县| 自贡市| 黄龙县| 阿城市| 山西省| 迁安市| 东莞市| 通渭县| 永平县| 汉川市| 满城县| 姚安县| 十堰市| 遂溪县| 文安县| 民乐县| 浦江县| 苗栗市| 威海市| 若羌县| 无棣县| 阳曲县| 丹东市|