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);
            }
        }

 

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