为你的CheckBox和RadioButton控件添色加彩

通过扩展现有的CheckBox和RadioButton控件,可以把这些控件的外观改变不同的颜色,下面就是一个简单的例子。

首先建立ColorCheckControl工程,添加两个控件类:

ColoredCheckBox.cs

<xmp>using System; using System.Windows.Forms; using System.Drawing; using System.ComponentModel; namespace ColorCheckControl { /// <summary> /// ColoredCheckBox 的摘要说明。 /// </summary> public class ColoredCheckBox : CheckBox { //添加自定义颜色属性 private Color checkColor; public Color checkBGColor; public ColoredCheckBox() { this.checkColor = this.ForeColor; this.Paint += new PaintEventHandler(this.PaintHandler); } [Description("checkColor由于显示在CheckBox选中时的颜色")] public Color CheckColor { get { return checkColor; } set { checkColor = value; this.Invalidate(); } } public Color checkBgColor { get { return checkBGColor; } set { checkBGColor = value; } } private void PaintHandler (object sender, PaintEventArgs pe) { Point pt = new Point(); if (this.CheckAlign == ContentAlignment.BottomCenter) { pt.X = (this.Width / 2) - 4; pt.Y = this.Height - 11; } if (this.CheckAlign == ContentAlignment.BottomLeft) { pt.X = 3; pt.Y = this.Height - 11; } if (this.CheckAlign == ContentAlignment.BottomRight) { pt.X = this.Width - 11; pt.Y = this.Height - 11; } if (this.CheckAlign == ContentAlignment.MiddleCenter) { pt.X = (this.Width / 2) - 4; pt.Y = (this.Height / 2) - 4; } if (this.CheckAlign == ContentAlignment.MiddleLeft) { pt.X = 3; pt.Y = (this.Height / 2) - 4; } if (this.CheckAlign == ContentAlignment.MiddleRight) { pt.X = this.Width - 11; pt.Y = (this.Height / 2) - 4; } if (this.CheckAlign == ContentAlignment.TopCenter) { pt.X = (this.Width / 2) - 4; pt.Y = 3; } if (this.CheckAlign == ContentAlignment.TopLeft) { pt.X = 3; pt.Y = 3; } if (this.CheckAlign == ContentAlignment.TopRight) { pt.X = this.Width - 11; pt.Y = 3; } DrawBackColor(pe.Graphics,this.checkBGColor,pt); if (this.Checked) DrawCheck(pe.Graphics,this.checkColor,pt); } public void DrawCheck(Graphics g, Color c, Point pt) { Pen pen = new Pen(this.checkColor); g.DrawLine(pen, pt.X, pt.Y + 2, pt.X + 2, pt.Y + 4); g.DrawLine(pen, pt.X, pt.Y + 3, pt.X + 2, pt.Y + 5); g.DrawLine(pen, pt.X, pt.Y + 4, pt.X + 2, pt.Y + 6); g.DrawLine(pen, pt.X + 3, pt.Y + 3, pt.X + 6, pt.Y); g.DrawLine(pen, pt.X + 3, pt.Y + 4, pt.X + 6, pt.Y + 1); g.DrawLine(pen, pt.X + 3, pt.Y + 5, pt.X + 6, pt.Y + 2); } public void DrawBackColor(Graphics g,Color b, Point pt) { SolidBrush br = new SolidBrush(this.checkBgColor); g.FillRectangle(br,pt.X,pt.Y,7,7); } } } </xmp>

ColoredRadioButton.cs

<xmp>using System; using System.Windows.Forms; using System.Drawing; using System.ComponentModel; namespace ColorCheckControl { /// <summary> /// ColoredRadioButton 的摘要说明。 /// </summary> public class CustomColorRadioButton : RadioButton { private Color checkColor; public CustomColorRadioButton() { this.checkColor = this.ForeColor; this.Paint += new PaintEventHandler(this.PaintHandler); } [Description("The color used to display the check painted in the RadioButton")] public Color CheckColor { get { return checkColor; } set { checkColor = value; this.Invalidate(); } } private void PaintHandler (object sender, PaintEventArgs pe) { if (this.Checked) { Point pt = new Point(); if (this.CheckAlign == ContentAlignment.BottomCenter) { pt.X = (this.Width / 2) - 3; pt.Y = this.Height - 9; } if (this.CheckAlign == ContentAlignment.BottomLeft) { pt.X = 4; pt.Y = this.Height - 9; } if (this.CheckAlign == ContentAlignment.BottomRight) { pt.X = this.Width - 9; pt.Y = this.Height - 9; } if (this.CheckAlign == ContentAlignment.MiddleCenter) { pt.X = (this.Width / 2) - 3; pt.Y = (this.Height / 2) - 3; } if (this.CheckAlign == ContentAlignment.MiddleLeft) { pt.X = 4; pt.Y = (this.Height / 2) - 3; } if (this.CheckAlign == ContentAlignment.MiddleRight) { pt.X = this.Width - 9; pt.Y = (this.Height / 2) - 3; } if (this.CheckAlign == ContentAlignment.TopCenter) { pt.X = (this.Width / 2) - 3; pt.Y = 4; } if (this.CheckAlign == ContentAlignment.TopLeft) { pt.X = 4; pt.Y = 4; } if (this.CheckAlign == ContentAlignment.TopRight) { pt.X = this.Width - 9; pt.Y = 4; } DrawCheck(pe.Graphics, this.checkColor,pt); } } public void DrawCheck(Graphics g, Color c, Point pt) { Pen pen = new Pen(this.checkColor); g.DrawLine(pen, pt.X, pt.Y + 1, pt.X + 3, pt.Y + 1); g.DrawLine(pen, pt.X, pt.Y + 2, pt.X + 3, pt.Y + 2); g.DrawLine(pen, pt.X + 1, pt.Y, pt.X + 1, pt.Y + 3); g.DrawLine(pen, pt.X + 2, pt.Y, pt.X + 2, pt.Y + 3); } } } </xmp>

然后建立表单Form1,代码如下:

Form1.cs

<xmp>using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace ColorCheckControl { /// <summary> /// Form1 的摘要说明。 /// </summary> /// public class Form1 : System.Windows.Forms.Form { private ColorCheckControl.ColoredCheckBox checkBox1; private ColorCheckControl.ColoredCheckBox checkBox2; private ColorCheckControl.CustomColorRadioButton radioButton1; private ColorCheckControl.CustomColorRadioButton radioButton2; private System.Windows.Forms.LinkLabel linkLabel1; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1)); this.checkBox1 = new ColorCheckControl.ColoredCheckBox(); this.checkBox2 = new ColorCheckControl.ColoredCheckBox(); this.radioButton1 = new ColorCheckControl.CustomColorRadioButton(); this.radioButton2 = new ColorCheckControl.CustomColorRadioButton(); this.linkLabel1 = new System.Windows.Forms.LinkLabel(); this.SuspendLayout(); // // checkBox1 // this.checkBox1.checkBgColor = System.Drawing.Color.LimeGreen; this.checkBox1.CheckColor = System.Drawing.Color.Red; this.checkBox1.Checked = true; this.checkBox1.CheckState = System.Windows.Forms.CheckState.Checked; this.checkBox1.Location = new System.Drawing.Point(60, 64); this.checkBox1.Name = "checkBox1"; this.checkBox1.Size = new System.Drawing.Size(224, 24); this.checkBox1.TabIndex = 1; this.checkBox1.Text = "扩展的CheckBox类:彩色CheckBox"; // // checkBox2 // this.checkBox2.checkBgColor = System.Drawing.Color.SkyBlue; this.checkBox2.CheckColor = System.Drawing.Color.Red; this.checkBox2.Checked = true; this.checkBox2.CheckState = System.Windows.Forms.CheckState.Checked; this.checkBox2.Location = new System.Drawing.Point(60, 96); this.checkBox2.Name = "checkBox2"; this.checkBox2.Size = new System.Drawing.Size(224, 24); this.checkBox2.TabIndex = 2; this.checkBox2.Text = "扩展的CheckBox类:彩色CheckBox"; // // radioButton1 // this.radioButton1.CheckColor = System.Drawing.Color.Red; this.radioButton1.Checked = true; this.radioButton1.Location = new System.Drawing.Point(60, 8); this.radioButton1.Name = "radioButton1"; this.radioButton1.Size = new System.Drawing.Size(224, 24); this.radioButton1.TabIndex = 3; this.radioButton1.TabStop = true; this.radioButton1.Text = "扩展的RadioButton类:彩色单选框"; // // radioButton2 // this.radioButton2.CheckColor = System.Drawing.Color.Blue; this.radioButton2.Location = new System.Drawing.Point(60, 32); this.radioButton2.Name = "radioButton2"; this.radioButton2.Size = new System.Drawing.Size(224, 24); this.radioButton2.TabIndex = 4; this.radioButton2.Text = "扩展的RadioButton类:彩色单选框"; // // linkLabel1 // this.linkLabel1.LinkArea = new System.Windows.Forms.LinkArea(0, 100); this.linkLabel1.Location = new System.Drawing.Point(108, 128); this.linkLabel1.Name = "linkLabel1"; this.linkLabel1.Size = new System.Drawing.Size(128, 23); this.linkLabel1.TabIndex = 5; this.linkLabel1.TabStop = true; this.linkLabel1.Text = "【孟宪会之精彩世界】"; this.linkLabel1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.ClientSize = new System.Drawing.Size(344, 157); this.Controls.Add(this.linkLabel1); this.Controls.Add(this.radioButton2); this.Controls.Add(this.radioButton1); this.Controls.Add(this.checkBox2); this.Controls.Add(this.checkBox1); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.MaximizeBox = false; this.Name = "Form1"; this.Text = "为你的CheckBox和RadioButton控件增色加彩"; this.Closing += new System.ComponentModel.CancelEventHandler(this.Form1_Closing); this.ResumeLayout(false); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> /// [STAThread] static void Main() { Application.Run(new Form1()); } private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e) { System.Diagnostics.Process.Start("IExplore.exe","http://xml.sz.luohuedu.net"); } } } </xmp>

运行结果如下:

优快云_Dev_Image_2003-8-71748451.gif

c#自定义控件的制作,美化的RadioButton 自定义一个RadioButton,增加了设置小圆框的多种颜色外观,可以拖放到工具箱里使用 立即下载

相关推荐

UE5游戏逆向实战:用FModel提取.pak文件中的3D模型(附Dumper-7避坑指南)

本文详细介绍了在UE5游戏逆向工程中使用FModel提取.pak文件中3D模型的实战技巧,包括Dumper-7工具的深度配置与避坑指南。针对UE5特有的unversioned属性系统,提供了从源码编译到智能注入的全链路解决方案,帮助开发者高效获取游戏资产并优化处理流程。

weixin_30374009的博客 500

C#自定义控件美化的RadionButton和CheckButton加入了Image图标

前段时间刚在优快云上传了一个美化的Radionbutton和CheckButton,但是加入imagelist后,显示不出来,在这里我对代码做了一些更改,希望对大家的学习有帮助。 控件效果图如下: 1.      在VS2017环境下新建一个自定义用户控件 2.      更改父类为RadionButton 3.      更改一下MyRadionButton.Designe

程序人生起波澜 4206

CATIA_DELMIA_V5R2019安装包下载及安装教程破解

CATIA_V5R2019/DELMIA_V5R2019安装包下载及安装教程破解

一只机械工装熊的博客 8414

elementUI el-checkbox-group 按钮button点击前后,颜色不一样的设置。

<el-checkbox-group v-model="param.valueArray" @change="valueArrayChange()" text-color="#FFF" fill="rgba(192, 196, 204, 1)" size="mini"> <el-checkbox-button v-for="item in param.conductStatusList" :label=...

weixin_47372236的博客 2858

360加固DEX解密与ELF修复技术解析

本文深入解析360加固的DEX解密与ELF修复技术,详细介绍了其多层保护机制、Java层入口分析、Native层加载流程及反调试绕过技巧。通过RC4和zlib解密技术,结合Frida工具实现内存Dump和ELF文件修复,为逆向工程师提供实用解决方案。

weixin_29327525的博客 514

【STM32】BLDC驱动&控制开发笔记 | 07_SPI通信测试 - STM32F407用SPI配置DRV8323驱动芯片

使用STM32F407的SPI通信功能配置DRV8323S驱动芯片的详细过程说明。

这里是 F菌的进阶之路。 6294

为你的CheckBox和RadioButton控件添色加彩 (转)

为你的CheckBox和RadioButton控件添色加彩 (转)[@more@]通过扩展现有的CheckBox和RadioButton控件,可以把这些控件的外观改变不同的颜色,下面就是一个简单的例子。 首先建立ColorCh...

csd3176的博客 130

R语言生存分析之限制性立方样条分析:样条回归、和多项式回归有什么不同?是否存在非线性关系、限制性立方样条cox回归模型

R语言生存分析之限制性立方样条分析:样条回归、和多项式回归有什么不同?是否存在非线性关系、限制性立方样条cox回归模型

statistics+insight+vista+power 635

【WinForm详细教程一】WinForm中的窗体、Label、TextBox及Button控件、RadioButton和CheckBox、ListBox

代码实现了一个基本的用户登录界面,用户可以输入用户名和密码,点击 “登录” 按钮进行登录,或者点击 “关闭” 按钮关闭窗体。同时,也包含了一些用户友好的功能,如在文本框中显示提示文本,输入文本时改变文本颜色,以及在输入验证时显示错误消息框。在用户与窗体进行交互时,它会显示消息框以获得用户的确认或取消操作,并根据用户的选择执行相应的操作。方法显示一个窗体时,用户可以自由地在新打开的窗体和原窗体之间切换。事件的事件处理器中添加逻辑,以响应用户的操作。事件的事件处理器中添加逻辑,以响应用户的操作。

QHCV的博客 5719

Java的API帮助文档

JDK帮助文档 SUN公司为JDK工具包提供了一整套文档资料,我们习惯上称之为JDK文档。 JDK文档中提供了Java中的各种技术的详细资料,以及JDK中提供的各种类的帮助说明。 JDk文档是Java语言的完整说明,大多数书籍中的类的介绍都要参照它来完成,它是编程者经常查阅的资料 如何理解API:就当做是一个“字典”,“使用手册”,API就相当于是一个电子的帮助文档,可以帮我们查看JDK提供的类的信息,平时查看的时候可结合百度一起看。 官网下载: 1、登录Oracle官网(Oracl.

人生得意须尽欢 2万+

【重识云原生】第四章云网络4.7.5节vDPA方案——virtio的半硬件虚拟化实现

vDPA(vhost Data Path Acceleration)即是让virtio数据平面不需主机干预的解决方案。该框架由Redhat提出,实现了virtio数据平面的硬件卸载。控制平面仍然采用原来的控制平面协议,当控制信息被传递到硬件中,硬件完成数据平面的配置之后,数据通信过程由硬件设备(智能网卡)完成,虚拟机与网卡之间直通。中断信息也由网卡直接发送至虚拟机不需要主机的干预。......

junbaozi的专栏 2190

c#:RadioButton控件与CheckBox控件的用法

一、用途 1.RadioButton控件 单选按钮,当与其他单选按钮成对出现时,允许用户从一组选项中选择单个选项。也就是说,当同一个容器中(Form、Panel、GroupBox、PictureBox等)存在两个以上的单选按钮时,只能有一个被选中。但不在同一个容器中的几组单选按钮彼此不关联,是可以有多个被选中的。 2.CheckBox控件 复选框,允许用户选择和清除关联选项

匆匆那年 3758

英飞凌TC3xx之一起认识GTM系列(一)先来认识GTM架构

本文讲述英飞凌TC3xx的通用计时器GTM的架构。

无所适从的我的博客 4601

Android RadioButton和CheckBox控件

1. RadioButton类 RadioButton继承CompoundButton,是单选按钮。 android:checked设置按钮是否选中。用setOnCheckChangeLinstener来对单选按钮进行监听。 RadioButton选择以后不能取消,主要是它覆盖了toggle方法,使其在被选中后无法取消。但可通过setChecked()方法取消。 public clas...

假装你是大灰狼的专栏 2270

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

汉书HE8,好用的汇编代码编写工具

汉书HE8,好用的汇编代码编写工具早期编写汇编代码所用功能强大

C#中Button、radioButton、checkBox、textBox和 label控件的简单示例。

简介 按钮类控件:Button 选择类控件:radioButton、checkBox 文本类控件:label 实现的效果图: 4个label,1个textBox,2个radioButton和3个checkBox组合形成一个简单的选择信息并显示。 label属性 设置label文本为姓名、性别、爱好和为空。 在代码视图中自定义性别和爱好变量。 private String strGender = ""; //性别 private String strLi

Liuxm 2893

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

qml Button RadioButton GroupBox CheckBox 按钮控件类

Button 导入 import QtQuick.Controls 1.4 或 import QtQuick.Controls 2.4 根据自己的编译器选择相应的版本 新建一个Qt Quick工程,然后在window对象中调用我们的Button控件 属性checkable默认是不选中的,即触发按钮,按下去马上弹起来。当设置为true时,Button变为切换按钮,有两种状态:按下/弹起 添加一些信号槽连接 import QtQuick 2.9 import QtQuick.Window 2

qq_33373173的博客 2004

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

Android控件与布局——基础控件RadioButton

最近在用原生的控件和布局绘制一些界面并使用,虽然这些都是Android基本知识,但是有的时候真的感觉力不从心,感觉有必要对Android常用的控件和布局做一个系统的了解。后续一个月甚至更多的时间都会围绕这个主题展开,毕竟这里面还是有不少高级控件的,我也会尽量结合应用深入的进行了解。 项目GitHub地址入口 上一篇:CheckBox 下一篇:CheckedTextV...

hfut_why的博客 2万+

Android之ToggleButton、Switch、CheckBox、RadioButton控件

在一组RadioButton中,只能选择其中的一个选项,选中一个RadioButton会自动取消其他RadioButton的选中状态。滑块可以水平滑动,并根据用户的操作来切换开关的状态。当RadioButton的选中状态发生改变时,会触发相应的回调方法,可以在回调方法中执行相应的操作。这个和我们的CheckBox是十分类似的,不同的点在于,这个控件可以由非选中状态通过点击事件转为选中状态,但是。当ToggleButton的状态发生改变时,会触发相应的回调方法,可以在回调方法中执行相应的操作。

Sean_Word的博客 2014

Android之RadioButton和CheckBox控件

Android之RadioButton和CheckBox控件 RadioButton控件介绍 RadioButton继承CompoundButton,是单选按钮。 android:checked设置按钮是否选中。 用setOnCheckedChangeListener(OnCheckedChangeListener)来对单选按钮进行监听。 实现RadioButton控件 public class ...

low666的博客 1678

Android开发--RadioButton和CheckBox控件的使用

在Android的开发中可能会用到RadioButton和CheckBox这两个控件,如果你对MFC开发熟悉的话,相信对这两个控件并不陌生,它们的形状也都是一样的,RadioButton是圆形单选按钮,既然是单选按钮,就需要规划好Group的概念,即在那一组中只能选择一个选项。而CheckBox是正方形的控件,即多选按钮,没有组的概念,一个控件就是一个单独的对象,互相不干扰,下面请看实现的具体例子

张勤一 1909
上一篇: 利用WebClient类向服务器上载文件
下一篇: 在Web应用程序中执行计划任务(多线程)
java2000_test
博客等级 码龄19年 10粉丝 320原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值