windows⾃定义控件开发-圆⾓TextBox
winform中的TextBox控件没有Radius属性,⽆法设置⽂本款为圆⾓。然后就想⾃⼰写⼀个⾃定义的圆⾓控件。⼀开始想,既然要写那就从头开始写,于是就开始写了起来。刚开始⼀切都很顺利,等我把界⾯画完之后,⽼天跟我开了⼀个⼤玩笑,我发现直接从Control类继承写不下去了,太复杂我要去研究⼀下源码,然后发现源码中的TextBox是⼀个mfc控件。好吧,这么⿇烦,算了。换个思路,后来我就想我只需要画个带弧度的框然后再在⾥⾯放⼀个TextBox这不就实现了吗?想到就做,没想到还真的成功了。下⾯是我的源码。
public partial class FengTextBoxEx : Control
{
TextBox textBox = new TextBox();
border radius什么意思public override string Text
{
get
{
return textBox.Text;
}
set
{
textBox.Text = value;
}
}
private Color borderColor = Color.Black;
/// <summary>
/// 边框颜⾊
/
// </summary>
public Color BorderColor
{
get
{
return this.borderColor;
}
set
{
this.borderColor = value;
}
}
private int borderThickness = 1;
/// <summary>
/// 边框粗细
/// </summary>
public int BorderThickness
{
get
{
return this.borderThickness;
}
set
{
this.borderThickness = value;
}
}
private int borderRadius = 0;
/// <summary>
/// 边框半径
/// </summary>
public int BorderRadius
{
get
{
return this.borderRadius;
}
set
set
{
this.borderRadius = value;
}
}
public FengTextBoxEx()
{
InitializeComponent();
this.SetStyle(ControlStyles.DoubleBuffer |
ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.SupportsTransparentBackColor,
true);
this.UpdateStyles();
textBox.BorderStyle = BorderStyle.None;
this.Controls.Add(textBox);
}
protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
{
height = textBox.Height + borderThickness * 2 + 2;
base.SetBoundsCore(x, y, width, height, specified);
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
pevent.Graphics.Clear(Parent.BackColor);
if (Color.Transparent.ToArgb() == BackColor.ToArgb())
textBox.BackColor = Color.White;
else
textBox.BackColor = BackColor;
textBox.ForeColor = ForeColor;
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
Graphics g = pe.Graphics;
g.SmoothingMode = SmoothingMode.AntiAlias;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;
g.CompositingQuality = CompositingQuality.HighQuality;
if (borderThickness <= 0)
return;
Pen pen = new Pen(borderColor, borderThickness);
if (borderRadius <= 0)
{
g.DrawRectangle(pen, 0, 0, this.Width - 1, this.Height - 1);
g.FillRectangle(new SolidBrush(BackColor), 0, 0, this.Width - 1, this.Height - 1);
return;
}
// 要实现圆⾓化的矩形
Rectangle rect = new Rectangle(0, 0, this.Width - 1, this.Height - 1);
// 指定图形路径,有⼀系列直线/曲线组成
GraphicsPath borderPath = new GraphicsPath();
borderPath.StartFigure();
borderPath.AddArc(new Rectangle(new Point(rect.X, rect.Y), new Size(2 * borderRadius, 2 * borderRadius)), 180, 90);
borderPath.AddLine(new Point(rect.X + borderRadius, rect.Y), new Point(rect.Right - borderRadius, rect.Y));
borderPath.AddArc(new Rectangle(new Point(rect.Right - 2 * borderRadius, rect.Y), new Size(2 * borderRadius, 2 * borderRadius)), 270, 90);
borderPath.AddLine(new Point(rect.Right, rect.Y + borderRadius), new Point(rect.Right, rect.Bottom - borderRadius));
borderPath.AddArc(new Rectangle(new Point(rect.Right - 2 * borderRadius, rect.Bottom - 2 * borderRadius), new Size(2 * borderRadius, 2 * border Radius)), 0, 90);
borderPath.AddLine(new Point(rect.Right - borderRadius, rect.Bottom), new Point(rect.X + borderRadius, rect.Bottom));
borderPath.AddArc(new Rectangle(new Point(rect.X, rect.Bottom - 2 * borderRadius), new Size(2 * borderRadius, 2 * borderRadius)), 90, 90);
borderPath.AddArc(new Rectangle(new Point(rect.X, rect.Bottom - 2 * borderRadius), new Size(2 * borderRadius, 2 * borderRadius)), 90, 90);
borderPath.AddLine(new Point(rect.X, rect.Bottom - borderRadius), new Point(rect.X, rect.Y + borderRadius));
borderPath.CloseFigure();
g.DrawPath(pen, borderPath);
g.FillPath(new SolidBrush(BackColor), borderPath);
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
int y = Height - textBox.Height - borderThickness;
textBox.Location = new Point(borderThickness + borderRadius, y);
textBox.Size = new Size(this.Width - borderThickness * 2 - borderRadius * 2, this.Height - borderThickness);
}
}
这个只是⼀个初步的代码,应该还有优化的空间,后⾯等我优化完了,再放上来,效果如下:
放上来希望可以帮助那些,和我⼀样需要的⼈。我看csdn上也有这样的带圆⾓的TextBox源码,不过都要积分才能下载。也不知道他们是怎么实现的。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论