WinForms 相当于 WPF 的 IsHitTestVisible

发布于 2024-10-19 03:55:40 字数 287 浏览 8 评论 0原文

我有一个按钮覆盖,其中有一个子标签。我将 MouseEnter 和 Leave 事件附加到按钮控件。

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

我希望它保留它的颜色,并且我希望它能够更改颜色(例如按钮上的 MouseEnter),但是当鼠标位于标签上时,将在按钮上考虑命中测试。

PS:我知道我可以在标签上添加鼠标进入和离开并处理这些情况,但我希望控件能够自给自足,这样如果参数在其外部发生变化(鼠标进入和离开的颜色),控件仍然会功能正常。

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南七夏 2024-10-26 03:55:40

在寻找其他信息时遇到这个问题,并且不相信接受的答案确实正确。

您可以扩展标签并更改 WndProc 中的命中响应。沿着这些思路:

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

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

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 ); 
    }
}
陈年往事 2024-10-26 03:55:40

简短的回答是你不能。按钮和标签实际上都是窗口,因此当鼠标离开一个窗口到另一个窗口时,会生成 mouseenter 和 mouseleave 事件。

真正的问题是,为什么按钮上需要标签?

The short answer is that you cannot. Both the button and the label are in fact windows, so when the mouse leave one for the other, mouseenter and mouseleave events are generated.

The real question is, why do you need a label on a button?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文