提问人:Ozan BAYRAM 提问时间:11/6/2023 更新时间:11/6/2023 访问量:56
圆角文本框边框双边框
Rounded textbox border double borders
问:
我正在尝试创建一个带有圆角的自定义文本框控件。但不知何故,我无法覆盖默认边框并得到如下结果:
代码如下:
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace Desktop.CustomControls
{
public class CustomTextBox : TextBox
{
private const int WM_NCPAINT = 0x85;
private const int WM_PAINT = 0xF;
private Color borderColor = Color.FromArgb(((int)(((byte)(115)))), ((int)(((byte)(115)))), ((int)(((byte)(115)))));
private int borderSize = 1;
private int borderRadius = 8;
public Color BorderColor
{
get { return borderColor; }
set
{
borderColor = value;
Invalidate();
}
}
public CustomTextBox()
{
this.BorderStyle = BorderStyle.None; // Remove the default border style
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_PAINT) // WM_PAINT message
{
using (Graphics g = this.CreateGraphics())
{
g.SmoothingMode = SmoothingMode.AntiAlias;
using (Pen borderPen = new Pen(borderColor, borderSize))
{
g.DrawRoundedRectangle(borderPen
, 0, 0, this.Width - 1, this.Height - 1
, borderRadius);
}
}
}
}
}
public static class GraphicsExtensions
{
public static void DrawRoundedRectangle(this Graphics g, Pen pen, float x, float y, float width, float height, float radius)
{
float diameter = radius * 2;
SizeF sizeF = new SizeF(diameter, diameter);
RectangleF arc = new RectangleF(x, y, sizeF.Width, sizeF.Height);
// Top-left arc
g.DrawArc(pen, arc, 180, 90);
// Top-right arc
arc.X = x + width - diameter;
g.DrawArc(pen, arc, 270, 90);
// Bottom-right arc
arc.Y = y + height - diameter;
g.DrawArc(pen, arc, 0, 90);
// Bottom-left arc
arc.X = x;
g.DrawArc(pen, arc, 90, 90);
}
}
}
我做错了什么或错过了什么?提前致谢。
答: 暂无答案
下一个:文本框和文本块不会对齐
评论
WndProc
base.WndProc(ref m);
WM_NCCALSIZE
WM_NCPAINT
GetDcEx()