通过点击拖动控制移动窗口 [英] Moving window by click-drag on a control

查看:112
本文介绍了通过点击拖动控制移动窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WinForms项目。我有我的窗口顶部的面板。我想这面板能够移动窗口,当用户点击它,然后拖动。

I have a WinForms project. I have a panel on the top of my window. I want that panel to be able to move the window, when the user clicks on it and then drags.

我怎样才能做到这一点?

How can I do this?

推荐答案

以下declerations添加到您的类:

Add the following declerations to your class:

    public const int WM_NCLBUTTONDOWN = 0xA1;
    public const int HTCAPTION = 0x2;
    [DllImport("User32.dll")]
    public static extern bool ReleaseCapture();
    [DllImport("User32.dll")]
    public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

在你的面板的的MouseDown 事件将这个:

Put this in your panel's MouseDown event:

    private void panel1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            ReleaseCapture();
            SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
        }
    }

这篇关于通过点击拖动控制移动窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆