2016-01-27 116 views
0

我有一个userControl问题。 当我尝试将其悬停以更改所有组件的颜色时,“ChangeColor”功能无法正确激活。更改UserControl元素的颜色

如果我将鼠标悬停在标签或用户控件的PictureBox的,它是诱发事件MouseLeave

这是我的用户设计的

This is my userControl

public partial class infoUser : UserControl 
{ 
    public infoUser() 
    { 
     InitializeComponent(); 
    } 

    public void SetNome(string nome) 
    { 
     labelUserLogged.Text = nome; 
    } 

    public void ChangeColor(System.Drawing.Color color) 
    { 
     labelUserLogged.BackColor = color; 
     pictureBoxUser.BackColor = color; 
    } 

    private void infoUser_MouseHover(object sender, EventArgs e) 
    { 
     ChangeColor(Color.CadetBlue); 
    } 

    private void infoUser_MouseLeave(object sender, EventArgs e) 
    { 
     ChangeColor(Color.WhiteSmoke); 
    } 

} 

代码

private void InitializeComponent() 
    { 
     this.labelUserLogged = new System.Windows.Forms.Label(); 
     this.pictureBoxUser = new System.Windows.Forms.PictureBox(); 
     ((System.ComponentModel.ISupportInitialize)(this.pictureBoxUser)).BeginInit(); 
     this.SuspendLayout(); 
     // 
     // labelUserLogged 
     // 
     this.labelUserLogged.BackColor = System.Drawing.SystemColors.Control; 
     this.labelUserLogged.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; 
     this.labelUserLogged.Cursor = System.Windows.Forms.Cursors.Hand; 
     this.labelUserLogged.Location = new System.Drawing.Point(0, 0); 
     this.labelUserLogged.Name = "labelUserLogged"; 
     this.labelUserLogged.Size = new System.Drawing.Size(167, 27); 
     this.labelUserLogged.TabIndex = 3; 
     this.labelUserLogged.Text = "Non loggato"; 
     this.labelUserLogged.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; 
     // 
     // pictureBoxUser 
     // 
     this.pictureBoxUser.BackColor = System.Drawing.Color.Transparent; 
     this.pictureBoxUser.Cursor = System.Windows.Forms.Cursors.Hand; 
     this.pictureBoxUser.Image = global::Castor.Gestionale.Properties.Resources.user_icon; 
     this.pictureBoxUser.Location = new System.Drawing.Point(5, 6); 
     this.pictureBoxUser.Name = "pictureBoxUser"; 
     this.pictureBoxUser.Size = new System.Drawing.Size(18, 15); 
     this.pictureBoxUser.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage; 
     this.pictureBoxUser.TabIndex = 4; 
     this.pictureBoxUser.TabStop = false; 
     // 
     // infoUser 
     // 
     this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
     this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
     this.Controls.Add(this.pictureBoxUser); 
     this.Controls.Add(this.labelUserLogged); 
     this.Name = "infoUser"; 
     this.Size = new System.Drawing.Size(171, 30); 
     this.MouseLeave += new System.EventHandler(this.infoUser_MouseLeave); 
     this.MouseHover += new System.EventHandler(this.infoUser_MouseHover); 
     ((System.ComponentModel.ISupportInitialize)(this.pictureBoxUser)).EndInit(); 
     this.ResumeLayout(false); 

    } 
+0

我们应该看到xaml或设计器代码的控件设计。 – jackjop

+0

它是WinForms还是WPF? – Verbon

+0

@Verbon它在WinForms –

回答

0

您的UserControl的子控件将收到鼠标输入时,光标是在它的范围内。所以,当你的鼠标“输入”到标签/ PictureBox的是“离开”的用户控件等
为了使儿童的具体控制“透明”的鼠标事件,你可以使用下面的技巧:

const int WM_NCHITTEST = 0x84, HTTRANSPARENT = (-1); 
class HitTestTransparentPictureBox : PictureBox { 
    protected override void WndProc(ref Message m) { 
     if(m.Msg == WM_NCHITTEST) { 
      m.Result = new IntPtr(HTTRANSPARENT); 
      return; 
     } 
     base.WndProc(ref m); 
    } 
} 
class HitTestTransparentLabel : Label { 
    protected override void WndProc(ref Message m) { 
     if(m.Msg == WM_NCHITTEST) { 
      m.Result = new IntPtr(HTTRANSPARENT); 
      return; 
     } 
     base.WndProc(ref m); 
    } 
} 

然后你就可以与他们的“鼠标透明”类似物代替具体控制在你的用户控件:

this.labelUserLogged = new HitTestTransparentLabel(); 
this.pictureBoxUser = new HitTestTransparentPictureBox(); 

之后,你可以用下面的办法来创建用户控件悬停效果:

public infoUser() { 
    InitializeComponent(); 
    MouseEnter += infoUser_MouseEnter; 
    MouseLeave += infoUser_MouseLeave; 
} 
void infoUser_MouseLeave(object sender, EventArgs e) { 
    Hover = false; 
} 
void infoUser_MouseEnter(object sender, EventArgs e) { 
    Hover = true; 
} 
bool hoverCore; 
protected bool Hover { 
    get { return hoverCore; } 
    set { 
     if(hoverCore == value) return; 
     hoverCore = value; 
     OnHoverChanged(); 
    } 
} 
void OnHoverChanged() { 
    ChangeColor(Hover ? Color.CadetBlue : Color.WhiteSmoke); 
} 
+0

我把该代码放在我的userControl中,但现在标签和图片框不会改变颜色! –

+0

我已经使用您提供的代码片段尝试了我的代码。但是你应该使用'MouseEnter/MouseLeave'事件而不是'MouseHover/MouseLeave'。我已经相应地更新了我的答案。 – DmitryG