Mega Code Archive

 
Categories / VB.Net Tutorial / 2D Graphics
 

Graphics MeasureString

Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Windows.Forms Imports System.Math public class MeasureString    public Shared Sub Main         Application.Run(New Form1)    End Sub End class public class Form1   Inherits System.Windows.Forms.Form   Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)         Dim the_font As New Font("Times New Roman", 40, FontStyle.Bold, GraphicsUnit.Pixel)         Dim the_string As String = "MeasureString"         Dim string_size As SizeF = e.Graphics.MeasureString("MeasureString", the_font)         Dim x As Integer = (Me.ClientSize.Width - CInt(string_size.Width)) \ 2         Dim y As Integer = (Me.ClientSize.Height - CInt(string_size.Height)) \ 2         e.Graphics.DrawString(the_string, the_font, Brushes.Black, x, y)         Dim string_rect As New Rectangle(x, y, CInt(string_size.Width), CInt(string_size.Height))         e.Graphics.DrawRectangle(Pens.Black, string_rect)   End Sub   Public Sub New()         MyBase.New()     Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)     Me.ClientSize = New System.Drawing.Size(292, 273)     Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen   End Sub End Class