GetTextExtentPointA





VB.Net


Imports System.Runtime.InteropServices

Module GetTextExtent
    <DllImport("gdi32")>
    Private Function GetTextExtentPointA(ByVal hdc As IntPtr, ByVal lpString As String, ByVal nCount As Integer, ByRef lpSize As Integer) As System.Drawing.SizeF
    End Function

    Sub GetTextExtent_Main()
        ' Create a new graphics object
        Dim g As Graphics = Graphics.FromHwnd(IntPtr.Zero)

        ' Set the font to use
        Dim font As New Font("Arial", 12)

        ' Get the handle to the graphics device context
        Dim hdc As IntPtr = g.GetHdc()

        ' Create a string to test
        Dim text As String = "Hello, World!"

        ' Call GetTextExtent to get the dimensions of the text
        Dim size2 As Integer
        Dim textSize As New System.Drawing.SizeF(GetTextExtentPointA(hdc, text, text.Length, size2))

        ' Release the graphics device context
        g.ReleaseHdc(hdc)

        ' Print the results
        Console.WriteLine("Text size: {0}", size2)
    End Sub
End Module