C#:如何从窗体及其控件中拖动一个? [英] C#: How to drag a from by the form and its controls?

查看:180
本文介绍了C#:如何从窗体及其控件中拖动一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码通过单击并拖动窗体本身来拖动无边界窗体.它起作用,但是当您单击并拖动位于窗体上的控件时,它就不起作用.单击某些控件而不是其他控件时,我需要能够将其拖动-按标签拖动,但不按按钮和文本框拖动.我该怎么办?

I use the following code to drag a borderless form, by clicking and dragging the form itself. It works, but it doesn't for when you click and drag a control located on the form. I need to be able to drag it when clicked on some of the controls but not others - drag by labels, but don't by buttons and text boxes. How do I do it?

protected override void WndProc(ref Message m)
{
    base.WndProc(ref m);

    const int WM_NCHITTEST = 0x84;
    const int HTCLIENT = 0x1;
    const int HTCAPTION = 0x2;

    if (m.Msg == WM_NCHITTEST && (int)m.Result == HTCLIENT)
        m.Result = (IntPtr)HTCAPTION;
}

推荐答案

实际上,我找到了解决方案

Actually, I found the solution here.

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

// Paste the below code in the your label control MouseDown event
if (e.Button == MouseButtons.Left)
{
    ReleaseCapture();
    SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
}

有效.

此外,在我上面的代码中,如果需要调整大小,则if语句应更改为

Also, in my code above, if resizing is desired, if statement should be changed to

        if (m.Msg == WM_NCHITTEST)
            if ((int)m.Result == HTCLIENT)
                m.Result = (IntPtr)HTCAPTION;

这篇关于C#:如何从窗体及其控件中拖动一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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