WFP鼠标右键移动窗口

概要

wpf自带移动窗口方法DragMove,但仅限于鼠标主键(左键)按下时生效,有时候需求是非鼠标左键按下时可以实现鼠标移动窗口,则无法使用此方法。故采用win32的方法GetWindowRect和SetWindowPos来实现任意按键结合鼠标移动事件来实现移动窗口。

实现流程

以鼠标右键移动为例:

  1. 监控鼠标右键按下,记录当前鼠标位置
  2. 监控鼠标移动,每次移动内获取鼠标移动后的位置,计算鼠标位置变化,让目标窗口相对原位置偏移
  3. 监控鼠标松开,标记按键事件已处理

代码

xaml

PreviewMouseMove="Window_PreviewMouseMove"
PreviewMouseRightButtonDown="Window_PreviewMouseRightButtonDown"
PreviewMouseRightButtonUp="Window_PreviewMouseRightButtonUp"

xaml.cs

[DllImport("user32.dll")]
public static extern bool SetWindowPos(IntPtr hWnd, int hWndlnsertAfter, int X, int Y, int cx, int cy, uint Flags);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowRect(IntPtr hWnd, ref System.Drawing.Rectangle rect);

private Point _pressedPos;
bool _isDragMoved = false;
private void Window_PreviewMouseMove(object sender, MouseEventArgs e)
{
	var currentPos = e.GetPosition(this);
	if (Mouse.RightButton == MouseButtonState.Pressed && _pressedPos != currentPos)
	{
		 _isDragMoved = true;
		 var hwnd = (System.Windows.Interop.HwndSource.FromDependencyObject(this) as System.Windows.Interop.HwndSource).Handle;
                var windowRect = new System.Drawing.Rectangle();
                GetWindowRect(hwnd,ref windowRect);
                //0x0001 SWP_NOSIZE | 0x0004 SWP_NOZORDER
                //忽略hWndlnsertAfter和cy、cx,即不改变窗口显示顺序和显示长宽,仅移动位置
                SetWindowPos(hwnd,0, windowRect.X + (int)(currentPos.X - _pressedPos.X), windowRect.Y + (int)(currentPos.Y - _pressedPos.Y), 0,0, 0x0001 | 0x0004);
	}
}

private void Window_PreviewMouseRightButtonUp(object sender, MouseButtonEventArgs e)
	{
		if (_isDragMoved)
		{
			_isDragMoved = false;
            e.Handled = true;
        }
    }

private void Window_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
	_pressedPos = e.GetPosition(this);
}
  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值