Winforms - 单击/拖动表单中的任意位置以移动它,就像在表单标题中单击一样 [英] Winforms - Click/drag anywhere in the form to move it as if clicked in the form caption

查看:13
本文介绍了Winforms - 单击/拖动表单中的任意位置以移动它,就像在表单标题中单击一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个在 Winforms 应用程序中使用的小型模态表单.它基本上是一个进度条.但我希望用户能够在表单中的任意位置单击并拖动它以在它仍在显示时在桌面上移动它.

I am creating a small modal form that is used in Winforms application. It is basically a progress bar of sorts. But I would like the user to be able to click anywhere in the form and drag it to move it around on the desktop while it is still being displayed.

如何实现这种行为?

推荐答案

Microsoft 知识库文章 320687 对这个问题有详细的回答.

Microsoft KB Article 320687 has a detailed answer to this question.

基本上,当被测试的点位于窗体的客户区时,您重写 WndProc 方法以将 HTCAPTION 返回到 WM_NCHITTEST 消息——实际上,这实际上是告诉 Windows 将单击完全相同地对待出现在表单的标题上.

Basically, you override the WndProc method to return HTCAPTION to the WM_NCHITTEST message when the point being tested is in the client area of the form -- which is, in effect, telling Windows to treat the click exactly the same as if it had occured on the caption of the form.

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

protected override void WndProc(ref Message m)
{
    switch(m.Msg)
    {
        case WM_NCHITTEST:
        base.WndProc(ref m);

        if ((int)m.Result == HTCLIENT)
            m.Result = (IntPtr)HTCAPTION;
        return;
    }

    base.WndProc(ref m);
}

这篇关于Winforms - 单击/拖动表单中的任意位置以移动它,就像在表单标题中单击一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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