WinForm 如何自定义开关控件
Winform 如何 自定义 控件 开关
2023-09-14 09:05:45 时间
先上结果:
由于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中就可以使用。
代码链接:https://download.csdn.net/download/hhhhhhhhhhwwwwwwwwww/14044575
相关文章
- winform控件之BindingNavigator
- 详解C# WinForm如何实现自动更新程序的案例分享
- Winform Vs Installer之添加自定义安装流程
- winform中写app.config文件时调试情况下没有改变的原因
- WinForm中快捷键与组合按键的设置方法
- c#winform读取xml文件创建菜单的代码
- Winform中Treeview实现按需加载的方法
- Winform中GridView分组排序功能实现方法
- C#winform编程中响应回车键的实现代码
- WinForm实现为TextBox设置水印文字功能
- C#中winform实现自动触发鼠标、键盘事件的方法
- WinForm中DefWndProc、WndProc与IMessageFilter的区别
- Winform实现将网页生成图片的方法