前段时间刚在CSDN上传了一个美化的Radionbutton和CheckButton,但是加入imagelist后,显示不出来,在这里我对代码做了一些更改,希望对大家的学习有帮助。
控件效果图如下:
1. 在VS2017环境下新建一个自定义用户控件
2. 更改父类为RadionButton
3. 更改一下MyRadionButton.Designer.cs
private void InitializeComponent()
{
components = new System.ComponentModel.Container();
}
4. MyRadionButton.cs的主要代码如下:
using System;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Drawing;
using System.Drawing.Drawing2D;
usingSystem.Data;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
usingSystem.Windows.Forms;
//usingSystem.Windows.Media;
namespaceWpfCustomControlLibrary1
{
public partial class myRadiobutton :RadioButton
{
private bool FontASPECT = false; //判断字体的方向
private int Measurement = 255; //渐变的初始值
LinearGradientBrush periphery_br; //外圆的颜色
LinearGradientBrush central_br; //鼠标移入控件时中圆的颜色
LinearGradientBrush nocentral_br; //无操作时中圆的颜色
LinearGradientBrush stipple_br; //内圆选中的颜色
LinearGradientBrush nostipple_br; //内圆无选中的颜色
#region -------属性----------
private Color periphercolor = Color.Red;
[Description("设置外椭圆的渐变色"),Category("自定义属性")]
public Color Periphercolor
{
get { return this.periphercolor; }
set
{
this.periphercolor = value;
this.Invalidate();
}
}
private Color centralcolor = Color.Red;
[Description("设置中间椭圆选中时的渐变色"),Category("自定义属性")]
public Color Centdralcolor
{
get { return this.centralcolor; }
set
{
this.centralcolor = value;
this.Invalidate();
}
}
private Color nocentralcolor =Color.Red;
[Description("设置中间椭圆无操作时的渐变色"), Category("自定义属性")]
public Color Nocentdralcolor
{
get { return this.nocentralcolor; }
set
{
this.nocentralcolor = value;
this.Invalidate();
}
}
private Color stipplecolor = Color.Red;
[Description("设置内椭圆选中时的渐变色"),Category("自定义属性")]
public Color Stipplecolor
{
get { return this.stipplecolor; }
set
{
this.stipplecolor = value;
this.Invalidate();
}
}
private Color nostipplecolor =Color.Red;
[Description("设置内椭圆无操作时的渐变色"),Category("自定义属性")]
public Color Nostipplecolor
{
get { return this.nostipplecolor; }
set
{
this.nostipplecolor = value;
this.Invalidate();
}
}
#endregion
public myRadiobutton()
{
// SetStyles();
InitializeComponent();
}
private void SetStyles()
{
base.SetStyle(
ControlStyles.UserPaint |
ControlStyles.OptimizedDoubleBuffer|
ControlStyles.AllPaintingInWmPaint|
ControlStyles.ResizeRedraw |
ControlStyles.SupportsTransparentBackColor, true);
base.UpdateStyles();
}
///<summary>
///控件在重绘时触发
///</summary>
///
protected override voidOnPaint(PaintEventArgs e)
{
base.OnPaint(e);
//e.Graphics.Clip = new Region(newRectangle(e.ClipRectangle.X, e.ClipRectangle.Y, e.ClipRectangle.Width,e.ClipRectangle.Height + this.Image.Height + 20));
e.Graphics.FillRectangle(SystemBrushes.Control, e.ClipRectangle);
e.Graphics.SmoothingMode =System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
Rectangle boxrect = newRectangle(e.ClipRectangle.X, e.ClipRectangle.Y,SystemInformation.SmallIconSize.Width, e.ClipRectangle.Height);
Point imagpoint = newPoint(e.ClipRectangle.X , e.ClipRectangle.Y);
Rectangle strrect = newRectangle(e.ClipRectangle.X + SystemInformation.SmallIconSize.Width+2,e.ClipRectangle.Y, e.ClipRectangle.Width + 2 - SystemInformation.SmallIconSize.Width,e.ClipRectangle.Height);
if (this.Image != null)
{ switch (this.TextImageRelation)
{
caseSystem.Windows.Forms.TextImageRelation.TextAboveImage:
imagpoint = new Point(e.ClipRectangle.X +SystemInformation.SmallIconSize.Width + 2 + (strrect.Width - Image.Width) / 2,e.ClipRectangle.Y+Font.Height+2);
strrect = newRectangle(e.ClipRectangle.X + SystemInformation.SmallIconSize.Width + 2,e.ClipRectangle.Y-Font.Height, e.ClipRectangle.Width + 2 -SystemInformation.SmallIconSize.Width, e.ClipRectangle.Height);
break;
caseSystem.Windows.Forms.TextImageRelation.ImageAboveText:
imagpoint = newPoint(e.ClipRectangle.X + SystemInformation.SmallIconSize.Width + 2 +(strrect.Width - Image.Width) / 2, e.ClipRectangle.Y);
strrect = newRectangle(e.ClipRectangle.X + SystemInformation.SmallIconSize.Width + 2,e.ClipRectangle.Y+Font.Height, e.ClipRectangle.Width + 2 -SystemInformation.SmallIconSize.Width, e.ClipRectangle.Height);
break;
caseSystem.Windows.Forms.TextImageRelation.ImageBeforeText:
imagpoint = newPoint(e.ClipRectangle.X + SystemInformation.SmallIconSize.Width + 2,e.ClipRectangle.Y);
strrect = newRectangle(e.ClipRectangle.X + SystemInformation.SmallIconSize.Width +4+Image.Width, e.ClipRectangle.Y, e.ClipRectangle.Width + 2 -SystemInformation.SmallIconSize.Width, e.ClipRectangle.Height);
break;
caseSystem.Windows.Forms.TextImageRelation.TextBeforeImage:
SizeF sizeF = e.Graphics.MeasureString(this.Text,this.Font);
imagpoint = newPoint(e.ClipRectangle.X + SystemInformation.SmallIconSize.Width +2+(int)sizeF.Width, e.ClipRectangle.Y);
strrect = newRectangle(e.ClipRectangle.X + SystemInformation.SmallIconSize.Width + 2,e.ClipRectangle.Y, e.ClipRectangle.Width + 2 -SystemInformation.SmallIconSize.Width, e.ClipRectangle.Height);
break;
default:
imagpoint = newPoint(e.ClipRectangle.X + SystemInformation.SmallIconSize.Width + 2 +(strrect.Width - Image.Width) / 2,e.ClipRectangle.Y);
strrect = newRectangle(e.ClipRectangle.X + SystemInformation.SmallIconSize.Width + 2,e.ClipRectangle.Y, e.ClipRectangle.Width + 2 -SystemInformation.SmallIconSize.Width, e.ClipRectangle.Height);
break;
}
drawimage(e.Graphics,imagpoint);//绘制图像
}
if (FontASPECT=getaspect())
{
boxrect.X = e.ClipRectangle.X +e.ClipRectangle.Width - SystemInformation.SmallIconSize.Width;
strrect.X = e.ClipRectangle.X;
}
Point mousepos =this.PointToClient(Control.MousePosition);
bool above =e.ClipRectangle.Contains(mousepos);
drawbox(e.Graphics,boxrect,above);//绘制选择框
drawtext(e.Graphics,strrect); //绘制文本
if (!Enabled)
e.Graphics.FillRectangle(newSolidBrush(System.Drawing.Color.FromArgb(127, SystemColors.Control)),e.ClipRectangle);
}
///<summary>
///绘制单选控件的图案
/// </summary>
///<param g="Graphics">封装一个绘图的对象</param>
///<paramrect="Rectangel">单选图案的绘制区域</param>
///<param Above="bool">判断鼠标是否在控件上</param>
private void drawbox(Graphicsg,Rectangle rect,bool Above)
{
int opactiy = Measurement;
periphery_br = newLinearGradientBrush(rect, Color.FromArgb(opactiy / 2,periphercolor),Color.FromArgb(opactiy,periphercolor),LinearGradientMode.ForwardDiagonal);
opactiy = (int)(.4f * opactiy +.5f);
central_br = newLinearGradientBrush(rect,Color.FromArgb(opactiy/10,centralcolor),Color.FromArgb(opactiy,centralcolor),LinearGradientMode.ForwardDiagonal);
opactiy = (int)(.4f * opactiy +.5f);
nocentral_br = new LinearGradientBrush(rect,Color.FromArgb(opactiy / 10, nocentralcolor), Color.FromArgb(opactiy,nocentralcolor), LinearGradientMode.ForwardDiagonal);
opactiy = Measurement;
stipple_br = newLinearGradientBrush(rect, Color.FromArgb(opactiy / 10, stipplecolor),Color.FromArgb(opactiy, stipplecolor), LinearGradientMode.BackwardDiagonal);
opactiy = (int)(.4f * opactiy +.5f);
nostipple_br = newLinearGradientBrush(rect, Color.FromArgb(opactiy / 10, nostipplecolor),Color.FromArgb(opactiy, nostipplecolor), LinearGradientMode.ForwardDiagonal);
int size = this.Font.Height;
Rectangle box = newRectangle(rect.X+((rect.Width-size)/2),rect.Y+((rect.Height-size)/2),size-2,size-2);
Rectangle glyph = newRectangle(box.X+3,box.Y+3,box.Width-6,box.Height-6);
g.FillEllipse(newSolidBrush(SystemColors.Window),box);
if(Above && this.Enabled)
{
g.DrawEllipse(newPen(central_br,(float)(3)),newRectangle(box.X+2,box.Y+2,box.Width-4,box.Height-4));
}
else
{
g.DrawEllipse(newPen(nocentral_br, (float)(3)), new Rectangle(box.X + 2, box.Y + 2, box.Width -4, box.Height - 4));
}
g.DrawEllipse(newPen(periphery_br,(float)(1.5)),box);
g.FillEllipse(newSolidBrush(SystemColors.Window), glyph);
if (this.Checked)
g.FillEllipse(stipple_br,glyph);
else
g.FillEllipse(nostipple_br,glyph);
}
///<summary>
///绘制文本
///</summary>
///<paramg="Grdaphics">封装一个绘图对象</param>
///<paramrect="Rectangle">绘制文本的区域</param>
private void drawtext(Graphicsg,Rectangle rect)
{
StringFormat tem_stringf = newStringFormat();
tem_stringf.Alignment =StringAlignment.Near;
tem_stringf.LineAlignment =StringAlignment.Center;
if (FontASPECT)
tem_stringf.FormatFlags =StringFormatFlags.DirectionRightToLeft;
if (!FontASPECT)
g.DrawString(this.Text,this.Font, new SolidBrush(this.ForeColor), rect, tem_stringf);
else
{
rect.X = rect.X -SystemInformation.SmallIconSize.Width / 2 + 2;
g.DrawString(this.Text,this.Font, new SolidBrush(this.ForeColor),rect,tem_stringf);
}
}
/// <summary>
/// 绘制imagelist图标
/// </summary>
/// <paramname="g"></param>
/// <paramname="rect"></param>
private void drawimage(Graphics g,Pointimagpoint)
{
g.DrawImage(this.Image,imagpoint);
// ImageList.Draw(g, new Point(rect.X,rect.Y),this.ImageIndex);
}
///<summary>
///获取文本读取方向
///</summary>
///<return>布尔型</return>
private bool getaspect()
{
bool tem_aspect = false;
if (this.RightToLeft == RightToLeft.Yes)
tem_aspect = true;
if (this.RightToLeft ==RightToLeft.No)
tem_aspect = false;
return tem_aspect;
}
}
}