WPF窗體拖動

本文鏈接:https://blog.csdn.net/AsynSpace/article/details/97635362

 

爲啥要寫這篇文章呢。是因爲我發現我的窗體在使用DragMove()時,毫無反應.....也可能是我的使用方式不對?這就不清楚了。

這裏將兩種方法寫下來,也算是做個記錄,也算是給有需要的人一個借鑑的參考。

 

第一種:這種是我在正常窗體情況下使用時完全OK的寫法,也就是在給窗體增加AllowsTransparency="True" WindowStyle="None" 之前的樣子。也就是官方示例的寫法。官方鏈接

protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
    base.OnMouseLeftButtonDown(e);

    // Begin dragging the window
    this.DragMove();
}

 

第二種:這種時我在給窗體XAML中加入 AllowsTransparency="True" WindowStyle="None"  之後的寫法。不知道是個人問題還是什麼問題,反正我的DragMove()不管用了......,於是也就有了下面的代碼的出現。

        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int WM_LBUTTONUP = 0x0202;




        //MouseMove事件的方法
        private void Window_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            if (e.LeftButton == System.Windows.Input.MouseButtonState.Pressed)
            {
                SendMessage(new WindowInteropHelper(this).Handle, WM_SYSCOMMAND, (IntPtr)61458, IntPtr.Zero);
            }
        }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章