问题描述
我有一个标签作为子项的按钮覆盖。我将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中的hittest响应。这些内容:
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!