2016-01-27 175 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