WinForms 相当于 WPF 的 IsHitTestVisible
我有一个按钮覆盖,其中有一个子标签。我将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在寻找其他信息时遇到这个问题,并且不相信接受的答案确实正确。
您可以扩展标签并更改 WndProc 中的命中响应。沿着这些思路:
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:
简短的回答是你不能。按钮和标签实际上都是窗口,因此当鼠标离开一个窗口到另一个窗口时,会生成 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?