Measure string and draw rectangle around string : Measure String « 2D Graphics « VB.Net Tutorial

Home
VB.Net Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statements
5.Date Time
6.Class Module
7.Development
8.Collections
9.Generics
10.Attributes
11.Event
12.LINQ
13.Stream File
14.GUI
15.GUI Applications
16.Windows Presentation Foundation
17.2D Graphics
18.I18N Internationlization
19.Reflection
20.Regular Expressions
21.Security
22.Socket Network
23.Thread
24.Windows
25.XML
26.Database ADO.net
27.Design Patterns
VB.Net
VB.Net by API
VB.Net Tutorial » 2D Graphics » Measure String 
17.29.2.Measure string and draw rectangle around string
Measure string and draw rectangle around string
Imports System
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Globalization


public class GraphicsMeasureStringRectangle
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class

Public Class Form1
    Inherits System.Windows.Forms.Form

    Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgsHandles MyBase.Paint
        Dim As Graphics = e.Graphics

        Dim As String = "a multi-line string:" & vbCrLf & "line 2" & vbCrLf & "line 3"

        Dim As Single = 0
        Dim arrPens As Pen() = New Pen() {Pens.Red, Pens.Green, Pens.Blue}
        Dim line As String
        For Each line In s.Split(vbCrLf)
            Dim width As Single = ClientRectangle.Width
            Dim height As Single = ClientRectangle.Height - y
            Dim layoutRect As RectangleF = New RectangleF(0, y, width, height)
            Dim format As StringFormat = New StringFormat(StringFormatFlags.NoWrap Or StringFormatFlags.DisplayFormatControl)
            g.DrawString(line, Me.Font, Brushes.Black, layoutRect, format)
            Dim size As SizeF = g.MeasureString(line, Me.Font, layoutRect.Size, format)
            g.DrawRectangle(arrPens(CInt(y / Me.Font.GetHeight(g)))0, y, size.Width, size.Height)
            y = y + Me.Font.GetHeight(g)
        Next

    End Sub


    Public Sub New()
        MyBase.New()
        InitializeComponent()
    End Sub

    Private components As System.ComponentModel.IContainer

    <System.Diagnostics.DebuggerStepThrough()Private Sub InitializeComponent()
        Me.AutoScaleBaseSize = New System.Drawing.Size(513)
        Me.ClientSize = New System.Drawing.Size(2060)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.Font = New System.Drawing.Font("Microsoft Sans Serif"14.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
    End Sub

End Class
17.29.Measure String
17.29.1.Graphics MeasureStringGraphics MeasureString
17.29.2.Measure string and draw rectangle around stringMeasure string and draw rectangle around string
17.29.3.Measure string and draw stringMeasure string and draw string
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.