Public Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
Using _
graphics As Graphics = Me.CreateGraphics, _
font As New Font("Microsoft Sans Serif", 14, FontStyle.Bold Or FontStyle.Strikeout)
Dim text As String = "How big am I?"
Dim size As SizeF = graphics.MeasureString(text, font)
MessageBox.Show(size.ToString)
End Using
End Sub
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public class TextBoxUnderline : TextBox
{
const int WM_PAINT = 0x00F;
public TextBoxUnderline()
: base() { }
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_PAINT)
{
using (Graphics g = base.CreateGraphics())
{
DrawDoubleLine(g);
}
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
this.Invalidate();
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
this.Invalidate();
}
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
this.Invalidate();
}
private void DrawDoubleLine(Graphics g)
{
Pen pen = new Pen(this.ForeColor);
Size size = TextRenderer.MeasureText(g, this.Text, this.Font, new Size(int.MaxValue, int.MaxValue), TextFormatFlags.NoPadding);
g.DrawLine(pen, 0, size.Height, size.Width, size.Height);
g.DrawLine(pen, 0, size.Height + 2f, size.Width, size.Height + 2f);
}
}
}
Code (VB.NET)
Imports System.Drawing
Imports System.Windows.Forms
Namespace WindowsFormsApplication1
Public Class TextBoxUnderline
Inherits TextBox
Const WM_PAINT As Integer = &Hf
Public Sub New()
MyBase.New()
End Sub
Protected Overrides Sub WndProc(ByRef m As Message)
MyBase.WndProc(m)
If m.Msg = WM_PAINT Then
Using g As Graphics = MyBase.CreateGraphics()
DrawDoubleLine(g)
End Using
End If
End Sub
Protected Overrides Sub OnMouseDown(e As MouseEventArgs)
MyBase.OnMouseDown(e)
Me.Invalidate()
End Sub
Protected Overrides Sub OnMouseUp(e As MouseEventArgs)
MyBase.OnMouseUp(e)
Me.Invalidate()
End Sub
Protected Overrides Sub OnKeyDown(e As KeyEventArgs)
MyBase.OnKeyDown(e)
Me.Invalidate()
End Sub
Private Sub DrawDoubleLine(g As Graphics)
Dim pen As New Pen(Me.ForeColor)
Dim size As Size = TextRenderer.MeasureText(g, Me.Text, Me.Font, New Size(Integer.MaxValue, Integer.MaxValue), TextFormatFlags.NoPadding)
g.DrawLine(pen, 0, size.Height, size.Width, size.Height)
g.DrawLine(pen, 0, size.Height + 2F, size.Width, size.Height + 2F)
End Sub
End Class
End Namespace