的WinForms - 用于点击控制WM_NCHITEST消息 [英] Winforms - WM_NCHITEST message for click on control

查看:702
本文介绍了的WinForms - 用于点击控制WM_NCHITEST消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的窗口没有边框和几个标签控件(没有什么需要点击)构成。我需要的是能够让用户通过点击任何地方移动的形式,让我发现这个的问题,用下面的code在那里找到。

I have a simple windows form with no border and several label controls (nothing that needs to be clicked). I needed to be able to allow the user to move the form by clicking anywhere on it, so I found this question, and used the following code found there.

    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;
                } else {
                    return;
                }
                break;
        }
        base.WndProc(ref m);            
    }

这工作得很好......一个点。如果我点击窗体本身(背景)上的任意位置, WM_NCHITTEST HTCLIENT ,这样我就可以把我的形式,预期。不过,如果我点击一个标签控件本身,该消息是不同的东西,我不能告诉它是什么。

This works well...to a point. If I click anywhere on the form itself (the background), WM_NCHITTEST is HTCLIENT, so I can move my form as expected. However, if I click on a label control itself, the message is something different, and I can't tell what it is.

我发现这篇文章中有关 WM_NCHITTEST 各种可能的值,但他们都不是我所需要的。

I found this article about the various possible values for WM_NCHITTEST but none of them seem to be what I need.

我知道我可以关闭所有的标签控件和,让我点击关于他们就好像它是形式本身,但我不知道是否有更好的/不同的方式来做到这一点。

I realize I could disable all my label controls and that would allow me to click "on" them as if it was the form itself, but I'm wondering if there's a better/different way to do this.

感谢您的帮助!

推荐答案

正在重写的WndProc 的形式,但是当光标位于标签 WM_NCHITTEST 消息发送给标签。

You are overriding the WndProc for the form, but when the cursor is over a label the WM_NCHITTEST message is sent to the label.

您可以创建导出自己的标签控制标签并覆盖其的WndProc 。这应该总是返回 HTTRANSPARENT 响应 WM_NCHITTEST 。是这样的:

You could create your own label control derived from Label and override its WndProc. This should always return HTTRANSPARENT in response to WM_NCHITTEST. Something like:

private const int HTTRANSPARENT = -1;

protected override void WndProc(ref Message m)
{
    switch (m.Msg)
    {
        case WM_NCHITTEST:
            m.Result = (IntPtr)HTTRANSPARENT;
            return;
    }
    base.WndProc(ref m);
}

另外请注意,有一个在你的的WndProc 一个小bug。如果消息是 WM_NCHITTEST ,但该地区是不是 HTCLIENT 然后调用基类的两倍。

Also note that there's a small bug in your WndProc. If the message is WM_NCHITTEST but the region isn't HTCLIENT then you call the base class twice.

这篇关于的WinForms - 用于点击控制WM_NCHITEST消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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