c# 给button添加不规则的图片以及用pictureBox替代button响应点击事件的方法
程序员文章站
2023-12-20 20:45:10
1.flat button
用这个方法,前提是要把button的type设置为flat
复制代码 代码如下:button1.tabstop = false;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");
}