c#不同按钮的制作

1.点击触发置位,松开复位 可以给按钮添加不同的背景图片backgroundImage做到置位和复位的不同效果,但在按钮属性中要注意: 1、按钮控件不要有边框

1.点击触发置位,松开复位

  • 可以给按钮添加不同的背景图片backgroundImage做到置位和复位的不同效果,但在按钮属性中要注意:
    1、按钮控件不要有边框 :属性FlatAppearance.BorderSize设置为0
    2、BackgroundImageLayout设置为zoom平铺
    3、FlatStyle设置为Flat
//利用鼠标按下和松开事件分别插入图片private void button1_MouseDown(object sender, MouseEventArgs e){//插入按下的图片button1.BackgroundImage = Properties.Resources.clickYellow;}private void button1_MouseUp(object sender, MouseEventArgs e){//插入松开的图片button1.BackgroundImage = Properties.Resources.clickedRed;}

2.点击触发置位,再次点击复位

和1相似也是插入两张按下和松开的图片,但这里用到标志位判断状态

//全局
public bool IsClick;
private void button1_Click(object sender, EventArgs e){IsClick = !IsClick;if (IsClick == true){button1.BackgroundImage = Properties.Resources.黄色;button1.Text = "手动模式";}else{button1.BackgroundImage = Properties.Resources.红色;button1.Text = "自动模式";}}
//更简洁的三元表达式//button1.BackgroundImage = IsClick == true ? Properties.Resources.黄色 : Properties.Resources.红色;//button1.Text = "手动模式";