先上结果:
由于WinForm中没有开关控件,需要自定义。下面就写我制作开关控件的步骤。
1、新建个WinForm程序,右键选择属性。
2、点击资源,将资源的类型修改为图像,然后将Button的图片复制进去(直接从文件夹里复制就行了)
3、新建用户控件,命名为OnOffButton。
在OnOffButton.cs中编写代码。
using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApp1 { public enum CheckStyle { style1 = 0, style2 = 1, style3 = 2, style4 = 3, style5 = 4, style6 = 5 }; public partial class OnOffButton : UserControl { public OnOffButton() { InitializeComponent(); //设置Style支持透明背景色并且双缓冲 this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); this.SetStyle(ControlStyles.DoubleBuffer, true); this.SetStyle(ControlStyles.ResizeRedraw, true); this.SetStyle(ControlStyles.Selectable, true); this.SetStyle(ControlStyles.SupportsTransparentBackColor, true); this.SetStyle(ControlStyles.UserPaint, true); this.BackColor = Color.Transparent; this.Cursor = Cursors.Hand; this.Size = new Size(87, 27); } bool isCheck = false; /// <summary> /// 是否选中 /// </summary> public bool Checked { set { isCheck = value; this.Invalidate(); } get { return isCheck; } } CheckStyle checkStyle = CheckStyle.style1; /// <summary> /// 样式 /// </summary> public CheckStyle CheckStyleX { set { checkStyle = value; this.Invalidate(); } get { return checkStyle; } } protected override void OnPaint(PaintEventArgs e) { Bitmap bitMapOn = null; Bitmap bitMapOff = null; if (checkStyle == CheckStyle.style1) { bitMapOn = global::WindowsFormsApp1.Properties.Resources.btncheckon1; bitMapOff = global::WindowsFormsApp1.Properties.Resources.btncheckoff1; } else if (checkStyle == CheckStyle.style2) { bitMapOn = global::WindowsFormsApp1.Properties.Resources.btncheckon2; bitMapOff = global::WindowsFormsApp1.Properties.Resources.btncheckoff2; } else if (checkStyle == CheckStyle.style3) { bitMapOn = global::WindowsFormsApp1.Properties.Resources.btncheckon3; bitMapOff = global::WindowsFormsApp1.Properties.Resources.btncheckoff3; } else if (checkStyle == CheckStyle.style4) { bitMapOn = global::WindowsFormsApp1.Properties.Resources.btncheckon4; bitMapOff = global::WindowsFormsApp1.Properties.Resources.btncheckoff4; } else if (checkStyle == CheckStyle.style5) { bitMapOn = global::WindowsFormsApp1.Properties.Resources.btncheckon5; bitMapOff = global::WindowsFormsApp1.Properties.Resources.btncheckoff5; } else if (checkStyle == CheckStyle.style6) { bitMapOn = global::WindowsFormsApp1.Properties.Resources.btncheckon6; bitMapOff = global::WindowsFormsApp1.Properties.Resources.btncheckoff6; } Graphics g = e.Graphics; Rectangle rec = new Rectangle(0, 0, this.Size.Width, this.Size.Height); if (isCheck) { g.DrawImage(bitMapOn, rec); } else { g.DrawImage(bitMapOff, rec); } } private void OnOffButton_Click(object sender, EventArgs e) { isCheck = !isCheck; this.Invalidate(); } } }
然后编写OnOffButton.Designer.cs代码。主要修改组件设计器生成的代码
#region 组件设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要修改 /// 使用代码编辑器修改此方法的内容。 /// </summary> private void InitializeComponent() { this.SuspendLayout(); this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.Name = "myButtonCheck"; this.Click += new System.EventHandler(this.OnOffButton_Click); this.ResumeLayout(false); }
然后生成解决方案。就可以在工具箱里看到组件了。
将组件拖入Form中就可以使用。