C# 无法在“鼠标输入”和“鼠标离开”事件上更改用户控件的背景色

C# 无法在“鼠标输入”和“鼠标离开”事件上更改用户控件的背景色,c#,.net,winforms,user-interface,user-controls,C#,.net,Winforms,User Interface,User Controls,我正在尝试更改用户控件的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 = Co

我正在尝试更改用户控件的
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);
我不知道发生了什么事。请帮忙

更新:

事件处理程序的连接方式如下:

notContainer.Controls.Add(new NRow());
this.MouseEnter += new System.EventHandler(this.NRow_MouseEnter);
this.MouseLeave += new System.EventHandler(this.NRow_MouseLeave);

如果您的
label1
放置在您的用户控件(UC)NRow中,您也应该是
label1
MouseEnter
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内。

如果
label1
放在用户控件(UC)NRow内,您也应该是
label1
的手柄
MouseEnter
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的
并在还原之前使用该方法确定鼠标坐标是否仍在
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);
        }        
    }
}

通过覆盖
UserControl的
,并在还原之前使用该方法确定鼠标坐标是否仍在
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);
        }        
    }
}

您可以尝试使用此代码将消息从
子控件
传递到您的
用户控件
,在您的情况下,您需要传递消息
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);                
        }
    }
}
公共部分类UserControl1:UserControl
{
公共用户控制1()
{
初始化组件();
}
字典控件=新建字典();
受保护的覆盖void OnLostFocus(事件参数e)
{
base.OnLostFocus(e);
休假(e);
}                
已添加受保护的覆盖无效OnControlAdded(ControlEventArgs e)
{
e、 Control.HandleCreated+=ControlsHandleCreated;
添加的碱基(e);
}
受保护的覆盖无效OnControlRemoved(ControlEventArgs e)
{
e、 Control.HandleCreated-=ControlsHandleCreated;
基础。移除(e);
}
私有void ControlsHandleCreated(对象发送方,事件参数e)
{
控制=发送方作为控制;
国家控制中心;
如果(!controls.TryGetValue(控制,输出nc)){
nc=新的NativeControl(本);
控制[控制]=nc;
}
nc.Handle(control.Handle);
}        
公共类NativeControl:NativeWindow
{
公共NativeControl(UserControl1父级)
{
父母=父母;
}
UserControl1父代;
布尔进来了;
受保护的覆盖无效WndProc(参考消息m)
{
//WM_MOUSEMOVE=0x200
//WM_LBUTTONDOWN=0x201
//WM_LBUTTONUP=0x202
//WM_NCHITTEST=0x84
如果(m.Msg==0x200 | | m.Msg==0x201 | | m.Msg==0x202 | | m.Msg==0x84){
//检查父项是否不是nul,将这些消息传递给父项
如果(父项!=null){
m、 HWnd=Parent.Handle;
母公司WndProc(参考m);
}
如果(m.Msg==0x200&&!已输入){
输入=真;
onmouseinter(EventArgs.Empty);
}
否则输入=假;
}
否则如果(输入)输入=false;
基准WndProc(参考m);
}
}
}

您可以尝试使用此代码将消息从
子控件
传递到您的
用户控件
,在您的情况下,您需要传递消息
WM\u 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);                
        }
    }
}
公共部分类UserControl1:UserControl
{
公共用户控制1()
{
初始化组件();
}
字典控件=新建字典();
受保护的覆盖void OnLostFocus(事件参数e)
{
base.OnLostFocus(e);
休假(e);
}                
已添加受保护的覆盖无效OnControlAdded(ControlEventArgs e)
{
e、 Control.HandleCreated+=ControlsHandleCreated;
添加的碱基(e);
}
受保护的覆盖无效OnControlRemoved(ControlEventArgs e)
{
e、 Control.HandleCreated-=ControlsHandleCreated;
基础。移除(e);
}
私有void ControlsHandleCreated(对象发送方,事件参数e)
{
控制=发送方作为控制;
国家控制中心;
如果(!controls.TryGetValue(控制,输出nc)){
nc=新的NativeControl(本);
控制[控制]=nc;
}
nc.Handle(control.Handle);
}        
公共类NativeControl:NativeWindow
{
公共NativeControl(UserControl1父级)
{
父母=父母;
}
UserControl1父代;
布尔进来了;
受保护的覆盖无效WndProc(参考消息m)
{
//WM_MOUSEMOVE=0x200
//WM_LBUTTONDOWN=0x201
//WM_LBUTTONUP=0x202
//WM_NCHITTEST=0x84
如果(m.Msg==0x200 | | m.Msg==0x201 | | m.Msg==0x202 | | m.Msg==0x84){
//检查父项是否不是nul,将这些消息传递给父项
如果(父项!=null){
m、 HWnd=Parent.Handl