winform更改悬停时按钮的背景图像属性

时间:2013-04-09 16:52:51

标签: c# winforms button background-image

我想在悬停时更改按钮的backgroundImage属性,如下所示:

    private void btnGetHardwareID_MouseEnter(object sender, EventArgs e)
    {
        btnGetHardwareID.BackgroundImage = (System.Drawing.Image)(Properties.Resources._11949847771696668271pill_button_green_benji__01_svg_med);
        btnGetHardwareID.Refresh();
    }

    private void btnGetHardwareID_MouseLeave(object sender, EventArgs e)
    {
        btnGetHardwareID.BackgroundImage = (System.Drawing.Image)(Properties.Resources._11949847771793996117pill_button_purple_benji_01_svg_med);
        btnGetHardwareID.Refresh();
    }

和btnGetHardwareID的peroperties如下所示:

    // btnGetHardwareID
    // 
    this.btnGetHardwareID.BackColor = System.Drawing.Color.Transparent;
    this.btnGetHardwareID.BackgroundImage = global::Travian.Properties.Resources._11949847771793996117pill_button_purple_benji_01_svg_med;
    this.btnGetHardwareID.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
    this.btnGetHardwareID.FlatAppearance.BorderSize = 0;
    this.btnGetHardwareID.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
    this.btnGetHardwareID.Font = new System.Drawing.Font("Cambria", 10F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
    this.btnGetHardwareID.ForeColor = System.Drawing.Color.White;
    this.btnGetHardwareID.Location = new System.Drawing.Point(31, 53);
    this.btnGetHardwareID.Name = "btnGetHardwareID";
    this.btnGetHardwareID.Size = new System.Drawing.Size(160, 40);
    this.btnGetHardwareID.TabIndex = 25;
    this.btnGetHardwareID.Text = "Get My Hardware-ID";
    this.btnGetHardwareID.UseVisualStyleBackColor = false;
    this.btnGetHardwareID.Click += new System.EventHandler(this.btnGetHardwareID_Click);
    this.btnGetHardwareID.MouseDown += new System.Windows.Forms.MouseEventHandler(this.btnGetHardwareID_MouseDown);
    this.btnGetHardwareID.MouseEnter += new System.EventHandler(this.btnGetHardwareID_MouseEnter);
    this.btnGetHardwareID.MouseLeave += new System.EventHandler(this.btnGetHardwareID_MouseLeave);
    this.btnGetHardwareID.MouseUp += new System.Windows.Forms.MouseEventHandler(this.btnGetHardwareID_MouseUp);

现在的问题是MouseEnter上的backgroundImage是这样的:
Mouse Enter

并且在MouseLeave上是这样的:

Mouse Leave

绿色按钮周围的Grayed BackColor是什么?我该如何删除它?

修改
原始图像如下:

red button - original - png format

green button - original - png format

和MainForm的背景:

main form - background-image

提前致谢

1 个答案:

答案 0 :(得分:3)

您正在寻找的解决方案如下。将FlatAppearance.MouseOverBackColor设置为透明。

this.btnGetHardwareID.FlatAppearance.MouseOverBackColor = System.Drawing.Color.Transparent;