原文:利用SendMessage实现窗口拖动

利用SendMessage实现窗口拖动

                                           周银辉



想想以前用跟踪鼠标位移的方式来实现窗口拖动的方式还真有些傻, 后来, .Net3.0以来的Window类内置了DragMove方法, 似乎让我们方便的不少, 但, 最近这个方法也不能满足需求了, 因为我需要DragMove过程中向外发事件来通知我"拖动开始了"和"拖动结束了", 可惜的是Window类没有提供者两个事件 (也曾企图通过其他方式来得到通知, 比如监视MouseUp等, 效果不好).

所以就自己来实现窗口拖动吧

不必同监视鼠标位移手动更新窗口位置, 其实通过向窗口发送SC_MOVE命令来移动窗口就可以了,这个命令会帮我们完成位置计算和更新工作:

        public const int SC_MOVE = 0xf012;
        public const int WM_SYSCOMMAND = 0x112;
        public const int WM_LBUTTONUP = 0x202;         [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);         private static void DragAndMoveInner(IntPtr hwnd)
        {
            OnDragAndMoveStarted(hwnd);             SendMessage(hwnd, WM_SYSCOMMAND, (IntPtr)SC_MOVE, IntPtr.Zero);
            SendMessage(hwnd, WM_LBUTTONUP, IntPtr.Zero, IntPtr.Zero);             OnDragAndMoveEnded(hwnd);
        }

其中WM_SYSCOMMAND是说明向窗口发送指定的命名, 命令的具体值通过第3个参数传进去.

注意到上面在拖动结束时发送了一个WM_LBUTTONUP消息, 这是因为当鼠标左键按下(并移动)时我们会调用该函数来开始拖动,你的应用程序师可以检测到开始拖动前的这个MouseDown事件de, 但SC_MOVE会拦截MouseUp来结束拖动.你的应用程序监视不到这个MouseUp事件,所以你可能会发现鼠标左键Down和Up数目不配对, 所以在拖动结束时我们Mock了一个Up事件.

由于SendMessage 方法是不会立即返回的(同步的, SendMessageCallback  与 SendNotifyMessage 是立即放回的), 所以在SendMessage执行完毕时,也就是我们"拖动"操作完毕之时, 所以我们可以在这里调用OnDragAndMoveEnded(hwnd)来引发我们自定义的"拖动结束"事件



SendMessage第三个参数(wParam)可以包含的具体的指令值,可以参考下面的枚举:

        public enum WM_SYSCOMMAND_WPARAM
        {
            SC_FIRST = 0xF000,             // Sizes the window.
            SC_SIZE = SC_FIRST,             // Moves the window.
            SC_MOVE = SC_FIRST + 0x10,             // Minimizes the window.
            SC_MINIMIZE = SC_FIRST + 0x20,             // Maximizes the window.
            SC_MAXIMIZE = SC_FIRST + 0x30,             // Moves to the next window.
            SC_NEXTWINDOW = SC_FIRST + 0x40,             // Moves to the previous window.
            SC_PREVWINDOW = SC_FIRST + 0x50,             // Closes the window.
            SC_CLOSE = SC_FIRST + 0x60,             //Scrolls vertically
            SC_VSCROLL = SC_FIRST + 0x70,             // Scrolls horizontally.
            SC_HSCROLL = SC_FIRST + 0x80,             // Retrieves the window menu as a result of a mouse click.
            SC_MOUSEMENU = SC_FIRST + 0x90,             // Retrieves the window menu as a result of a keystroke.
            // For more information, see the Remarks section.
            SC_KEYMENU = SC_FIRST + 0x100, 
             
            SC_ARRANGE = SC_FIRST + 0x110,             // Restores the window to its normal position and size.
            SC_RESTORE = SC_FIRST + 0x120,             // Activates the Start menu.
            SC_TASKLIST = SC_FIRST + 0x130,             // Executes the screen saver application specified 
            // in the [boot] section of the System.ini file.
            SC_SCREENSAVE = SC_FIRST + 0x140,             // Activates the window associated with the application-specified hot key. 
            // The lParam parameter identifies the window to activate.
            SC_HOTKEY = SC_FIRST + 0x150,             // Selects the default item; 
            // the user double-clicked the window menu.
            SC_DEFAULT = SC_FIRST + 0x160,             // Sets the state of the display.
            // This command supports devices that have power-saving features,
            // such as a battery-powered personal computer.
            // The lParam parameter can have the following values:
            // -1 - the display is powering on
            //  1 - the display is going to low power
            //  2 - the display is being shut off
            SC_MONITORPOWER = SC_FIRST + 0x170, 
           
            // Changes the cursor to a question mark with a pointer. 
            // If the user then clicks a control in the dialog box, 
            // the control receives a WM_HELP message.
            SC_CONTEXTHELP = SC_FIRST + 0x180,              SC_SEPARATOR = 0xF00F
        }

完整的代码,参考下面, 其支持WinForm和WPF 窗口:

    public static class DragMoveExtention
    {
        public static event EventHandler DragAndMoveStarted;
        public static event EventHandler DragAndMoveEnded;         public const int SC_MOVE = 0xf012;
        public const int WM_SYSCOMMAND = 0x112;
        public const int WM_LBUTTONUP = 0x202;         [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);         private static void DragAndMoveInner(IntPtr hwnd)
        {
            OnDragAndMoveStarted(hwnd);             SendMessage(hwnd, WM_SYSCOMMAND, (IntPtr)SC_MOVE, IntPtr.Zero);
            SendMessage(hwnd, WM_LBUTTONUP, IntPtr.Zero, IntPtr.Zero);             OnDragAndMoveEnded(hwnd);
        }         private static void OnDragAndMoveStarted(Object sender)
        {
            if(DragAndMoveStarted != null)
            {
                DragAndMoveStarted(sender, EventArgs.Empty);
            }
        }         private static void OnDragAndMoveEnded(Object sender)
        {
            if(DragAndMoveEnded != null)
            {
                DragAndMoveEnded(sender, EventArgs.Empty);
            }
        }         // use it like this: 
        // wpfWindow.MouseMove += delegate{ wpfWindow.DragAndMove(); };
        public static void DragAndMove(this Window window)
        {
            if (Mouse.LeftButton == MouseButtonState.Pressed)
            {
                IntPtr hwnd = new WindowInteropHelper(window).Handle;
                DragAndMoveInner(hwnd);
            }
        }         // use it like this: 
        // winForm.MouseMove += delegate { winForm.DragAndMove(); };
        public static void DragAndMove(this Form form)
        {
            if (Control.MouseButtons == MouseButtons.Left)
            {
                DragAndMoveInner(form.Handle);
            }
        }
        
    }

利用SendMessage实现窗口拖动的更多相关文章

  1. 利用 canvas 破解 某拖动验证码

    利用 canvas 破解 某拖动验证码 http://my.oschina.net/u/237940/blog/337194

  2. Duilib改进窗口拖动,使整个窗口都能拖动两种方法(转载)

    转载:http://www.cnblogs.com/XiHua/articles/3490490.html 转载:http://blog.csdn.net/lostspeed/article/deta ...

  3. duilib进阶教程 -- 改进窗口拖动 (12)

    现在大家应该都知道caption="0,0,0,32",是指示标题栏区了吧,如果想要整个窗口都能拖动呢? 那直接把高度改成和窗口一样不就得了~O(∩_∩)O~ 嗯,这样是可以,比如 ...

  4. Winform 窗口拖动

    把窗口边框去掉后,窗口拖动问题: private Point mouseOffset; //记录鼠标指针的坐标 private bool isMouseDown = false; //记录鼠标按键是否 ...

  5. 窗体的Alpha通道透明色支持(一旦 Form 被定义为利用 LayeredWindow ,窗口的绘图不再响应沿用多年的 WM_Paint 消息)

    参考: http://www.delphibbs.com/delphibbs/dispq.asp?lid=2190768 Windows 2000后,为了支持类似MAC界面的Alpha通道混合效果,提 ...

  6. 29.QT-自定义窗口拖动、自定义QToolButton/QPushButton开关按钮、界面阴影,声音等总结

    自定义窗口及拖动 1.自定义无边框窗口时,需要将窗口标志设为: Qt::FramelessWindowHint |Qt::WindowSystemMenuHint | Qt::WindowMinMax ...

  7. 【Win32 API】利用SendMessage实现winform与wpf之间的消息传递

    原文:[Win32 API]利用SendMessage实现winform与wpf之间的消息传递 引言    有一次心血来潮,突然想研究一下进程间的通信,能够实现消息传递的方法有几种,其中win32ap ...

  8. C# zedgraph利用另一窗口取得的串口数据绘图

    C# zedgraph利用另一窗口获得的串口数据绘图第一次用zedgraph,非常不熟悉,网上很多内容看的云里雾里... 这个程序主界面接收串口数据,而另外一个窗口进行实时曲线绘图,要怎么样实现for ...

  9. 基于jQuery页面窗口拖动预览效果

    今天给大家分享一款基于Query页面窗口拖动预览效果.这是一款基于jQuery+HTML5实现的模拟页面窗口显示拖动窗口预览特效.这款实例适用浏览器:IE8.360.FireFox.Chrome.Sa ...

随机推荐

  1. SqlMapConfig.xml全局配置文件解析(mybatis)

    原文  http://www.cnblogs.com/selene/p/4607004.html 一:SqlMapConfig.xml配置文件的内容和配置顺序如下 properties(属性) set ...

  2. Dcloud课程7 单例模式一般用在什么场景

    Dcloud课程7 单例模式一般用在什么场景 一.总结 一句话总结:连接数据库,这样就保证了和数据之间只有一个连接,从而能够不占用多余资源,这样就极大的减少了资源浪费,减少了mysql或者说服务器压力 ...

  3. 非常全的linux面试笔试题及参考答案

    一.填空题: 1. 在Linux系统中,以 文件 方式访问设备 . 2. Linux内核引导时,从文件/etc/fstab 中读取要加载的文件系统. 3. Linux文件系统中每个文件用 i节点来标识 ...

  4. JS实现放大镜效果(放大图片)

    注意:里边的两张图片(一大一小)可以自己添加,JQ采用jquery-1.11.3.js版,也可自行调换. HTML代码: <!DOCTYPE html> <html> < ...

  5. java程序猿经常使用的工具名称--知道中文意思吗

    在学习java的时候常常会碰到一些单词,可是一般的时候也不是非常在意这个单词的意思,而是能够了解到这个工具或者框架能够做什么就能够了.偶尔总结了一下还蛮有意思的.例如以下, 假设有遗漏,各位能够帮忙补 ...

  6. js匿名函数(变量加括号就是函数)

    js匿名函数(变量加括号就是函数) 一.总结 变量加括号就是函数,而函数的括号是用来传参的 1.类比:以正常函数去想匿名函数,匿名函数比正常函数只是少了函数名,本质还是一样,该怎么传参还是怎么传参,小 ...

  7. ios本地相册 照像 本地视频

    -(IBAction)btnClick{ UIActionSheet* actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate: ...

  8. [RxJS] Stopping a shared observable execution

    ConnectableObservable has the connect() method to conveniently dictate the start of the shared execu ...

  9. mongodb查询部分满足条件的列

    db.tblorders.createIndex( { orderid : -1 },{background:true, name:"index_orderid"} ); db.o ...

  10. linux系统进程的查看与控制

    原文:linux系统进程的查看与控制 一.什么是进程? 进程就是系统未完成并且正在进行的工作. 二.查看系统进程 1.图形方式查看 gnome-system-monitor 2.进程查看命令 ps - ...