我在使用userControl时遇到问题。 当我尝试将其悬停以更改所有组件的颜色时,“ChangeColor”功能无法正确触发。
如果我将鼠标悬停在usercontrol的标签或图片框上,则会引发事件mouseLeave
这是我的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 :(得分:0)
当光标位于其边界内时,UserControl的子控件将接收鼠标输入。因此,当您的鼠标“进入”标签/图片框时,它会“离开”UserControl等等 要使特定的子控件对于鼠标事件“透明”,您可以使用以下技巧:
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);
}
}
然后,您可以使用“鼠标透明”类似物替换UserControl中的特定控件:
this.labelUserLogged = new HitTestTransparentLabel();
this.pictureBoxUser = new HitTestTransparentPictureBox();
之后,您可以使用以下方法在UserControl上创建悬停效果:
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);
}