0

我正在尝试更改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;
}

但它不起作用。即使我尝试在 BackColor 更改行上添加断点,但控制没有到达那里。我还检查了事件绑定,没问题。用户控件被添加到这样的面板中:

notContainer.Controls.Add(new NRow());

我不知道发生了什么。请帮忙。

更新:

事件处理程序的附加方式如下:

this.MouseEnter += new System.EventHandler(this.NRow_MouseEnter);
this.MouseLeave += new System.EventHandler(this.NRow_MouseLeave);
4

3 回答 3

2

我能够通过覆盖UserControl's OnMouseLeaveandOnMouseEnter并使用PointToClientMethod 来确定鼠标坐标是否仍在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);
        }        
    }
}
于 2013-09-20T23:12:24.077 回答
2

如果您label1放置在您的用户控件 (UC) NRow 中,您应该也可以MouseEnter处理MouseEventlabel1因为,label1当鼠标移过它时,您的 UC 内部可以处理您的鼠标事件而不是您的 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 内。

于 2013-09-20T22:52:09.553 回答
1

您可以尝试使用此代码将消息从 传递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);                
        }
    }
}
于 2013-09-21T03:31:27.207 回答