WinForms 相当于 WPF 的 IsHitTestVisible [英] WinForms equivalent of WPF's IsHitTestVisible

查看:25
本文介绍了WinForms 相当于 WPF 的 IsHitTestVisible的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮覆盖,它有一个 Label 作为子项.我将 MouseEnter 和 Leave 事件附加到按钮控件.

I have a button override that has a Label as a child. I have MouseEnter and Leave events attached to the button control.

当鼠标进入标签时,按钮的事件被取消(这是很自然的).我的问题是如何在不实际禁用标签的情况下禁用标签的命中测试.

When the mouse enters the label the button's events are nullified (which is natural). My question is how can I disable the label's hit testing without actually disabling the label.

我希望它保留它的颜色并且我希望它能够改变颜色(例如按钮上的鼠标输入),但是当鼠标悬停在标签上时,要考虑按钮上的命中测试.

I want it to retain it's color and I want it to be able to change colors (MouseEnter on button for example), but when the mouse is over the label, the hit test to be considered on the button.

PS:我知道我可以在标签上添加鼠标进入和离开并处理这些情况,但我希望控件是自给自足的,这样如果参数在它之外发生变化(鼠标进入和离开的颜色),控件仍将正常运行.

P.S: I know I can add Mouse Enter and Leave on the Label and handle those cases but I want the control to be self sufficient such that if the parameters change outside of it (the colors on mouse enter and leave), the control will still function properly.

推荐答案

在寻找其他信息时遇到了这个问题,但不相信接受的答案是真的正确.

Ran across this question while looking for other information and don't believe the accepted answer is really correct.

您可以在 WndProc 中扩展标签并更改命中响应.一些类似的东西:

You can extend the label and alter the hittest response in WndProc. Something along these lines:

public class HTTransparentLabel : Label
{
    private const int WM_NCHITTEST = 0x84; 
    private const int HTTRANSPARENT = -1; 

    protected override void WndProc(ref Message message) 
    { 
        if ( message.Msg == (int)WM_NCHITTEST ) 
            message.Result = (IntPtr)HTTRANSPARENT; 
        else 
            base.WndProc( ref message ); 
    }
}

这篇关于WinForms 相当于 WPF 的 IsHitTestVisible的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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