【问题标题】:Changing the color of the elements of a UserControl更改 UserControl 元素的颜色
【发布时间】:2016-01-27 16:26:18
【问题描述】:

我的用户控件有问题。 当我尝试将其悬停以更改所有组件的颜色时,“ChangeColor”功能无法正确触发。

如果我将鼠标悬停在用户控件的标签或图片框上,则会引发事件 mouseLeave

这是我的用户控件

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);

    }

【问题讨论】:

标签: c# winforms visual-studio user-controls


【解决方案1】:

当光标在其范围内时,您的 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);
}

【讨论】:

  • 我将该代码放在我的 userControl 中,但现在标签和图片框不会改变颜色!
  • 我已经使用您提供的代码 sn-ps 尝试了我的代码。但是您应该使用MouseEnter/MouseLeave 事件而不是MouseHover/MouseLeave。我已经相应地更新了我的答案。
猜你喜欢
  • 2011-09-16
  • 1970-01-01
  • 2019-12-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-07-14
  • 2020-02-25
相关资源
最近更新 更多