Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
MyBase.OnPaint(e)
Dim str As String = "测试用的字符串,再加长一些看的更明白"
Dim rect As New Rectangle(100, 10, 50, 30)
e.Graphics.DrawRectangle(SystemPens.ControlDarkDark, rect)
Dim format As New StringFormat(StringFormatFlags.NoWrap)
format.LineAlignment = StringAlignment.Center
format.Alignment = StringAlignment.Far
Dim size As SizeF = e.Graphics.MeasureString(str, Me.Font)
rect = New Rectangle(rect.Right - size.Width, rect.Top, size.Width + 4, rect.Height)
e.Graphics.DrawString(str, Me.Font, SystemBrushes.ControlDarkDark, rect, format)
End Sub
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
MyBase.OnPaint(e)
Dim str As String = "测试用的字符串"
Dim rect As New Rectangle(10, 10, 50, 30)
Dim reg As Region = e.Graphics.Clip '记录原来的剪切区域
e.Graphics.DrawRectangle(SystemPens.ControlDarkDark, rect)
Dim format As New StringFormat(StringFormatFlags.NoWrap)
format.LineAlignment = StringAlignment.Center
format.Alignment = StringAlignment.Far
'应用新的区域
e.Graphics.Clip = New Region(rect)
Dim size As SizeF = e.Graphics.MeasureString(str, Me.Font)
rect.X = (rect.X + (rect.Width - CInt(size.Width)))
rect.Width = (rect.Width - (rect.Width - CInt(size.Width)))
e.Graphics.DrawString(str, Me.Font, SystemBrushes.ControlDarkDark, rect, format)
'恢复原来的区域
e.Graphics.Clip = reg
End Sub
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
MyBase.OnPaint(e)
Dim m_Rect As New Rectangle(10, 10, 50, 30)
e.Graphics.DrawRectangle(SystemPens.ControlDarkDark, m_Rect)
TextRenderer.DrawText(e.Graphics, "测试用的字符串", Me.Font, m_Rect, Color.Red, Color.White, (TextFormatFlags.VerticalCenter Or TextFormatFlags.Right))
End Sub
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
MyBase.OnPaint(e)
Dim m_Rect As New Rectangle(10, 10, 50, 30)
e.Graphics.FillRectangle(SystemBrushes.ControlDark, m_Rect)
e.Graphics.DrawRectangle(SystemPens.ControlDarkDark, m_Rect)
Dim format As New StringFormat(StringFormatFlags.NoWrap)
format.LineAlignment = StringAlignment.Center
format.Alignment = StringAlignment.Center
TextRenderer.DrawText(e.Graphics, "测试用的字符串", Me.Font, m_Rect, Color.Red, Color.White, (TextFormatFlags.VerticalCenter Or TextFormatFlags.Right))
End Sub
---------------------------------------------------------------------------
Private Sub DrawText_Right(ByVal ev As Printing.PrintPageEventArgs, ByVal strText As String, ByVal ft As Font, ByVal br As Brush, ByVal rect As RectangleF, ByVal fm As StringFormat)
If strText <> "" Then
Dim txtSize As SizeF = ev.Graphics.MeasureString(strText, ft)
Dim reg As Region = ev.Graphics.Clip '记录原来的剪切区域
Dim sngCut As Single = txtSize.Width - rect.Width '文字长度和指定框体的差异值
txtSize = Nothing
reg.Dispose()
reg = Nothing
sngCut = Nothing
End If
End Sub
Private Sub DrawText_Center(ByVal ev As Printing.PrintPageEventArgs, ByVal strText As String, ByVal ft As Font, ByVal br As Brush, ByVal rect As RectangleF, ByVal fm As StringFormat)
If strText <> "" Then
Dim txtSize As SizeF = ev.Graphics.MeasureString(strText, ft)
Dim reg As Region = ev.Graphics.Clip '记录原来的剪切区域
Dim sngCut As Single = txtSize.Width - rect.Width '文字长度和指定框体的差异值
txtSize = Nothing
reg.Dispose()
reg = Nothing
sngCut = Nothing
End If
End Sub
---------------------------------------------------------------------------
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
MyBase.OnPaint(e)
Dim str As String = "测试用的字符串,再加长一些看的更明白"
Dim rect As New Rectangle(200, 100, 50, 30)
e.Graphics.DrawRectangle(SystemPens.ControlDarkDark, rect)
Dim format As New StringFormat(StringFormatFlags.NoWrap)
format.LineAlignment = StringAlignment.Center
format.Alignment = StringAlignment.Far
Dim size As SizeF = e.Graphics.MeasureString(str, Me.Font)
rect = New Rectangle(rect.Right - size.Width, rect.Top, size.Width + 1, rect.Height)
e.Graphics.DrawString(str, Me.Font, SystemBrushes.ControlDarkDark, rect, format)
End Sub