无法更改用户对Mouse_Enter和Mouse_Leave事件的控件的背景色
本文关键字:Mouse 事件 Leave 控件 背景色 Enter 用户 | 更新日期: 2023-09-27 18:32:07
我正在尝试更改用户控件的BackColor
属性并ForeColor
其中的标签。以下是我的代码:
private void NRow_MouseLeave(object sender, EventArgs e)
{
BackColor = Color.White;
label1.ForeColor = Color.Black;
}
private void NRow_MouseEnter(object sender, EventArgs e)
{
BackColor = Color.Lime;
label1.ForeColor = Color.White;
}
但它不起作用。即使我试图在背景颜色更改行上添加断点,但控制没有到达那里。我还检查了事件绑定,没问题。用户控件将添加到如下所示的面板中:
notContainer.Controls.Add(new NRow());
我不知道发生了什么。请帮忙。
更新:
事件处理程序按如下所示附加:
this.MouseEnter += new System.EventHandler(this.NRow_MouseEnter);
this.MouseLeave += new System.EventHandler(this.NRow_MouseLeave);
如果你的label1
放在你的用户控件(UC)NRow内,你也应该处理MouseEnter
和label1
MouseEvent
。因为,当鼠标移到UC上时,UC内的label1
可以处理鼠标事件,而不是UC。
this.MouseEnter += new System.EventHandler(this.NRow_MouseEnter);
this.MouseLeave += new System.EventHandler(this.NRow_MouseLeave);
label1.MouseEnter += new System.EventHandler(this.NRow_MouseEnter);
label1.MouseLeave += new System.EventHandler(this.NRow_MouseLeave);
注意:以上所有行都应放在UC NRow内。
我能够通过覆盖UserControl's
OnMouseLeave
和OnMouseEnter
并使用PointToClient
方法确定鼠标坐标是否仍在UserControl
内来使其工作,然后再恢复,看看这样的东西是否适合您。
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
protected override void OnMouseEnter(EventArgs e)
{
BackColor = Color.Lime;
label1.ForeColor = Color.White;
base.OnMouseEnter(e);
}
protected override void OnMouseLeave(EventArgs e)
{
if (! Bounds.Contains(PointToClient( MousePosition)))
{
BackColor = Color.White;
label1.ForeColor = Color.Black;
base.OnMouseLeave(e);
}
}
}
您可以
尝试使用此代码将消息从child controls
传递到您的UserControl
,在您的情况下,您需要将消息传递WM_MOUSEMOVE
以及一些小代码以使其按预期工作:
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
Dictionary<Control,NativeControl> controls = new Dictionary<Control,NativeControl>();
protected override void OnLostFocus(EventArgs e)
{
base.OnLostFocus(e);
OnMouseLeave(e);
}
protected override void OnControlAdded(ControlEventArgs e)
{
e.Control.HandleCreated += ControlsHandleCreated;
base.OnControlAdded(e);
}
protected override void OnControlRemoved(ControlEventArgs e)
{
e.Control.HandleCreated -= ControlsHandleCreated;
base.OnControlRemoved(e);
}
private void ControlsHandleCreated(object sender, EventArgs e)
{
Control control = sender as Control;
NativeControl nc;
if(!controls.TryGetValue(control, out nc)) {
nc = new NativeControl(this);
controls[control] = nc;
}
nc.AssignHandle(control.Handle);
}
public class NativeControl : NativeWindow
{
public NativeControl(UserControl1 parent)
{
Parent = parent;
}
UserControl1 Parent;
bool entered;
protected override void WndProc(ref Message m)
{
//WM_MOUSEMOVE = 0x200
//WM_LBUTTONDOWN = 0x201
//WM_LBUTTONUP = 0x202
//WM_NCHITTEST = 0x84
if (m.Msg == 0x200 || m.Msg == 0x201 || m.Msg == 0x202 || m.Msg == 0x84){
//Check if Parent is not nul, pass these messages to the parent
if (Parent != null){
m.HWnd = Parent.Handle;
Parent.WndProc(ref m);
}
if (m.Msg == 0x200 && !entered){
entered = true;
Parent.OnMouseEnter(EventArgs.Empty);
}
else entered = false;
}
else if (entered) entered = false;
base.WndProc(ref m);
}
}
}