WinformRadioButton美化改变选中样式
[前⾔]
因项⽬需求,UI要求的RadioButton样式与系统⾃带样式有较⼤区别如下图:
上图是UI要求样式,下图为系统⾃带样式
[解决⽅案]
⽅案:为节省开发时间,特改写系统RadioButton,留有部分功能接⼝属性⽅便本项⽬使⽤
代码:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace JaRadioButton.Controls
{
public partial class JaRadioButton : RadioButton
{
bool isMouseMove =false;
public JaRadioButton()
{
InitializeComponent();
this.MouseMove += JaRadioButton_MouseMove;
this.MouseLeave += JaRadioButton_MouseLeave;
}
private void JaRadioButton_MouseLeave(object sender, EventArgs e)
{
isMouseMove =false;
this.Invalidate();
}
private void JaRadioButton_MouseMove(object sender, MouseEventArgs e)
{
isMouseMove=true;
this.Invalidate();
}
private Color selectRadioForeColor= Color.FromArgb(0X33,0X70,0XCC);
[Description("选中后选择框颜⾊"),Category("⾃定义")]
public Color SelectRadioForeColor
{
get{return selectRadioForeColor;}
set
{
selectRadioForeColor =value;
this.Invalidate();
}
}
private Color selectTextForeColor = Color.FromArgb(0X33,0X70,0XCC);
[Description("选中后⽂本颜⾊,可根据ChangeTextForeColor属性来决定是否改变"),Category("⾃定义")]
public Color SelectTextForeColor
{
get{return selectTextForeColor;}
set
{
{
selectTextForeColor =value;
this.Invalidate();
}
}
private bool changeTextForeColor=false;
[Description("是否改变选中后⽂本颜⾊,默认为false"),Category("⾃定义")]
public bool ChangeTextForeColor
{
get{return changeTextForeColor;}
set
{
changeTextForeColor =value;
this.Invalidate();
}
}
private void JaRadioButton_Paint(object sender, PaintEventArgs e)
{
RadioButton rButton =(RadioButton)sender;
Graphics g = e.Graphics;
g.Clear(this.BackColor);
//double px = ((this.Font.Size * 3) / 4) + 0.4f;//字体pt换成像素px
int px =this.Height;//字体pt换成像素px
Rectangle radioButtonrect =new Rectangle(1,1,12,12);
if(px >14)
{
radioButtonrect =new Rectangle(1,(px -12)/2,12,12);
}
g.SmoothingMode = SmoothingMode.AntiAlias;
if(rButton.Checked)
{
radioButtonrect.Inflate(1,1);
g.FillEllipse(new SolidBrush(selectRadioForeColor), radioButtonrect);
radioButtonrect.Inflate(-4,-4);
g.FillEllipse(new SolidBrush(this.BackColor), radioButtonrect);
StringFormat sf =new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
Rectangle rectangleText =new Rectangle(8,0,this.Width,this.Height);
if(changeTextForeColor)//改变⽂字颜⾊
{
g.DrawString(Text,this.Font,new SolidBrush(selectTextForeColor), rectangleText, sf);
}
else
{
g.DrawString(Text,this.Font,new SolidBrush(ForeColor), rectangleText, sf);
}
}
else
{
if(isMouseMove)//⿏标移⼊
{
using(Pen pen =new Pen(Color.FromArgb(0X33,0X70,0XCC)))
{
g.DrawEllipse(pen, radioButtonrect);
}
}
else
{
html radio点击变颜using(Pen pen =new Pen(Color.FromArgb(0X96,0X97,0X99)))
{
g.DrawEllipse(pen, radioButtonrect);
}
}
}
StringFormat sf =new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
Rectangle rectangleText =new Rectangle(8,0,this.Width,this.Height);
g.DrawString(Text,this.Font,new SolidBrush(ForeColor), rectangleText, sf);//字体
}
//g.Dispose();
}
}
}
[样式展⽰]
[后语]
1.选择框还可以根据实际情况更改为其他样式,如正⽅形/图⽚,因时间关系在此就不⼀⼀演⽰.
2.由于⼯作原因,没有过多时间来排版和优化控件,控件较为粗糙,内部bug可能存在很多,各位朋友同仁如发现异常请轻喷并与我及时联系,以免误导他⼈.
3.写该⽂章⽬的是记录⾃⼰在开发中遇到的问题,⽅便以后遇到类似问题以便快速解决,也是为了给有相同问题的道友提供部分思路.
本章完
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论