I'm trying to create a custom onPaint for my textbox, it is working... it is working But when I try to type something, a textbox is rendering above the textbox.
This is my constructor:
public TextBox()
{
Font = new Font("Segoe UI", 11F, FontStyle.Regular, GraphicsUnit.Point, ((byte)(0)));
BackColor = Color.White;
BorderColor = Color.Gray;
BorderStyle = BorderStyle.None;
SetStyle(ControlStyles.UserPaint, true);
}
And the onPaint:
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
g.FillRectangle(backgroundBrush, 0, 0, this.Width, this.Height);
SizeF fontSize = g.MeasureString(Text, Font);
g.DrawString(Text, Font, new SolidBrush(ForeColor), new PointF(5, 5), cFormat);
g.DrawRectangle(borderPen, borderPen.Width / 2, borderPen.Width / 2,
this.Width - borderPen.Width, this.Height - borderPen.Width);
}
If you just want a custom TextBox
with some custom border (width and color), I have 2 solutions here:
Using ControlPaint
, this will allow you to draw border with some style and color but can't use Brush
to draw more variously (like a HatchBrush
can do):
public class CustomTextBox : TextBox
{
[DllImport("user32")]
private static extern IntPtr GetWindowDC(IntPtr hwnd);
struct RECT
{
public int left, top, right, bottom;
}
struct NCCALSIZE_PARAMS
{
public RECT newWindow;
public RECT oldWindow;
public RECT clientWindow;
IntPtr windowPos;
}
float clientPadding = 2;
float actualBorderWidth = 4;
Color borderColor = Color.Red;
protected override void WndProc(ref Message m)
{
//We have to change the clientsize to make room for borders
//if not, the border is limited in how thick it is.
if (m.Msg == 0x83) //WM_NCCALCSIZE
{
if (m.WParam == IntPtr.Zero)
{
RECT rect = (RECT)Marshal.PtrToStructure(m.LParam, typeof(RECT));
rect.left += clientPadding;
rect.right -= clientPadding;
rect.top += clientPadding;
rect.bottom -= clientPadding;
Marshal.StructureToPtr(rect, m.LParam, false);
}
else
{
NCCALSIZE_PARAMS rects = (NCCALSIZE_PARAMS)Marshal.PtrToStructure(m.LParam, typeof(NCCALSIZE_PARAMS));
rects.newWindow.left += clientPadding;
rects.newWindow.right -= clientPadding;
rects.newWindow.top += clientPadding;
rects.newWindow.bottom -= clientPadding;
Marshal.StructureToPtr(rects, m.LParam, false);
}
}
if (m.Msg == 0x85) //WM_NCPAINT
{
IntPtr wDC = GetWindowDC(Handle);
using(Graphics g = Graphics.FromHdc(wDC)){
ControlPaint.DrawBorder(g, new Rectangle(0,0,Size.Width, Size.Height), borderColor, actualBorderWidth, ButtonBorderStyle.Solid,
borderColor, actualBorderWidth, ButtonBorderStyle.Solid, borderColor, actualBorderWidth, ButtonBorderStyle.Solid,
borderColor, actualBorderWidth, ButtonBorderStyle.Solid);
}
return;
}
base.WndProc(ref m);
}
}
Here is the textbox snapshot:
using FillRegion
method of a Graphics
to paint the border with various kinds of Brush
, here I use HatchBrush
:
public class CustomTextBox : TextBox
{
[DllImport("user32")]
private static extern IntPtr GetWindowDC(IntPtr hwnd);
struct RECT
{
public int left, top, right, bottom;
}
struct NCCALSIZE_PARAMS
{
public RECT newWindow;
public RECT oldWindow;
public RECT clientWindow;
IntPtr windowPos;
}
int clientPadding = 2;
int actualBorderWidth = 4;
protected override void WndProc(ref Message m)
{
//We have to change the clientsize to make room for borders
//if not, the border is limited in how thick it is.
if (m.Msg == 0x83) //WM_NCCALCSIZE
{
if (m.WParam == IntPtr.Zero)
{
RECT rect = (RECT)Marshal.PtrToStructure(m.LParam, typeof(RECT));
rect.left += clientPadding;
rect.right -= clientPadding;
rect.top += clientPadding;
rect.bottom -= clientPadding;
Marshal.StructureToPtr(rect, m.LParam, false);
}
else
{
NCCALSIZE_PARAMS rects = (NCCALSIZE_PARAMS)Marshal.PtrToStructure(m.LParam, typeof(NCCALSIZE_PARAMS));
rects.newWindow.left += clientPadding;
rects.newWindow.right -= clientPadding;
rects.newWindow.top += clientPadding;
rects.newWindow.bottom -= clientPadding;
Marshal.StructureToPtr(rects, m.LParam, false);
}
}
if (m.Msg == 0x85) //WM_NCPAINT
{
IntPtr wDC = GetWindowDC(Handle);
using(Graphics g = Graphics.FromHdc(wDC)){
Rectangle rect = new Rectangle(0,0,Width,Height);
Rectangle inner = new Rectangle(0, 0, Width, Height);
inner.Offset(actualBorderWidth + 2, actualBorderWidth + 2);
inner.Width -= 2 * actualBorderWidth + 4;
inner.Height -= 2 * actualBorderWidth + 4;
Region r = new Region(rect);
r.Xor(inner);
using (System.Drawing.Drawing2D.HatchBrush brush = new System.Drawing.Drawing2D.HatchBrush(System.Drawing.Drawing2D.HatchStyle.SmallCheckerBoard, Color.Green, Color.Red))
{
g.FillRegion(brush, r);
}
}
return;
}
base.WndProc(ref m);
}
}
Here is the textbox snapshot:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With