c# 给button添加不规则的图片以及用pictureBox替代button响应点击事件的方法

1.Flat button

用这个方法,前提是要把button的type设置为Flat

复制代码 代码如下:

button1.TabStop = false;button1.FlatAppearance.BorderSize = 0;
button1.FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255); //设置边框的颜色Transparent
button1.FlatAppearance.MouseOverBackColor = Color.FromArgb(0, 255, 255, 255); //设置鼠标经过时候的背景颜色Transparent
button1.FlatAppearance.MouseDownBackColor = Color.FromArgb(0, 255, 255, 255); //设置鼠标按下时候的背景颜色Transparent

这样按钮的效果图如下

后面的颜色是窗体的颜色不是按钮的颜色。

接下来响应点击事件。

复制代码 代码如下:

//鼠标落下设置一张图片
private void button1_MouseDown(object sender, MouseEventArgs e)
{
     this.button1.Image = Image.FromFile(@"../../images/close_click.png");
}

//鼠标弹起恢复原来图片
private void button1_MouseUp(object sender, MouseEventArgs e)
{
     this.button1.Image = Image.FromFile(@"../../images/close_default.png");
}

//响应点击事件
private void button1_Click(object sender, EventArgs e)
{
     MessageBox.Show("I'm normal button");
}


2.用pictureBox

复制代码 代码如下:

private void pictureBox2_MouseDown(object sender, MouseEventArgs e)
 {
      this.pictureBox2.Image = Image.FromFile(@"../../images/close_click.png");
}

private void pictureBox2_MouseUp(object sender, MouseEventArgs e)
{
     this.pictureBox2.Image = Image.FromFile(@"../../images/close_default.png");
}

private void pictureBox2_Click(object sender, EventArgs e)
{
      MessageBox.Show("I'm closed");
}

你可能感兴趣的:(c# 给button添加不规则的图片以及用pictureBox替代button响应点击事件的方法)