GdipMeasureString problem
GdipMeasureString problem
GdipMeasureString problem
GdipMeasureString problem
GdipMeasureString problem
GdipMeasureString problem GdipMeasureString problem GdipMeasureString problem GdipMeasureString problem GdipMeasureString problem GdipMeasureString problem GdipMeasureString problem GdipMeasureString problem
GdipMeasureString problem GdipMeasureString problem
GdipMeasureString problem
Go Back  Xtreme Visual Basic Talk > > > GdipMeasureString problem


Reply
 
Thread Tools Display Modes
  #1  
Old 08-23-2016, 07:44 AM
mms mms is offline
Ultimate Contributor
 
Join Date: Jul 2002
Location: Hamilton, Ontario
Posts: 1,859
Default GdipMeasureString problem


Anyone have idea why the following code (using GdipMeasureString) does not return the correct size of the string's bounding box?

Code:
Option Explicit


Dim gdiplusToken As Long


Private Sub Form_Load()

    Form1.Caption = "GDI+"
    Form1.Width = Screen.TwipsPerPixelX * 600
    Form1.Height = Screen.TwipsPerPixelY * 465
    Form1.BackColor = &H8000000F
    Form1.ScaleMode = vbPixels
    
    Picture1.Appearance = 0
    Picture1.Left = 16
    Picture1.Top = 16
    Picture1.Height = 366
    Picture1.Width = 552
    Picture1.AutoRedraw = True
    
    Command1.Left = Picture1.Left
    Command1.Top = Picture1.Top + Picture1.Height + 10
    Command1.Width = Picture1.Width
    Command1.Caption = "execute"
    Command1.ZOrder (0)
    
    ' Initialize Windows GDI+
    Dim GdiplusStartupInput As GdiplusStartupInput
    GdiplusStartupInput.GdiplusVersion = 1
    GdiplusStartupInput.DebugEventCallback = 0
    GdiplusStartupInput.SuppressBackgroundThread = False
    GdiplusStartupInput.SuppressExternalCodecs = False
    Dim status As GpStatus
    status = GdiplusStartup(gdiplusToken, GdiplusStartupInput, 0)
    If status <> Ok Then
        MsgBox "Error loading GDI+!", vbCritical
        Call GdiplusShutdown(gdiplusToken)
    End If

End Sub


Private Sub Form_Unload(Cancel As Integer)
    
    ' Clean up resources used by Windows GDI+
    Call GdiplusShutdown(gdiplusToken)
    
End Sub


Private Sub Command1_Click()

    Call GdipSample
    
End Sub


Private Sub GdipSample()

    Picture1.Cls
    
    Dim stat As Long

    ' Create Graphics object
    Dim graphics As Long
    stat = GdipCreateFromHDC(Picture1.hdc, graphics)
    stat = GdipSetTextRenderingHint(graphics, TextRenderingHintAntiAlias)

    ' Create a brush
    Dim blackBrush As Long
    stat = GdipCreateSolidFill(&HFF000000, blackBrush)
    
    ' Create a pen
    Dim greenPen As Long
    stat = GdipCreatePen1(&HFF00FF00, 1, UnitPixel, greenPen)

    ' Create font family
    Dim fontFamily As Long
    stat = GdipCreateFontFamilyFromName(StrPtr("Times New Roman"), 0, fontFamily)
    
    ' Create font
    Dim font As Long
    stat = GdipCreateFont(fontFamily, 24, FontStyle.FontStyleRegular, UnitPixel, font)

    ' Define string
    Dim myString As String
    myString = "String to measure."
    
    Dim stringFormat As Long
    stat = GdipCreateStringFormat(0, 0, stringFormat)

    ' Draw string
    Dim layoutRect As RECTF
    layoutRect.Left = 100   'x
    layoutRect.Top = 50     'y
    'Leaving the Right and Bottom values at zero means there is no boundary
    layoutRect.Right = 0    'width
    layoutRect.Bottom = 0   'height

    stat = GdipDrawString(graphics, StrPtr(myString), -1, font, layoutRect, 0, blackBrush)

    ' Measure string
    Dim boundRect As RECTF
    stat = GdipMeasureString(graphics, myString, 18, font, layoutRect, stringFormat, boundRect, 0, 0)

    ' Draw a rectangle that represents the size of the string's bounding box.
    stat = GdipDrawRectangle(graphics, greenPen, boundRect.Left, boundRect.Top, boundRect.Right, boundRect.Bottom)

    ' Cleanup
    stat = GdipDeleteBrush(blackBrush)
    stat = GdipDeletePen(greenPen)
    stat = GdipDeleteFont(font)
    stat = GdipDeleteFontFamily(fontFamily)
    stat = GdipDeleteStringFormat(stringFormat)
    stat = GdipDeleteGraphics(graphics)
    
    Picture1.Refresh

End Sub
Reply With Quote
Reply

Tags
stat, dim, font, string, create, private, gdi+, mystring, greenpen, gdiplusstartupinput, blackbrush, stringformat, status, layoutrect, graphics, call, picture1.width, box, picture1.height, picture1.top, draw, bounding, windows, false, rectf


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
GdipMeasureString problem
GdipMeasureString problem
GdipMeasureString problem GdipMeasureString problem
GdipMeasureString problem
GdipMeasureString problem
GdipMeasureString problem GdipMeasureString problem GdipMeasureString problem GdipMeasureString problem GdipMeasureString problem GdipMeasureString problem GdipMeasureString problem
GdipMeasureString problem
GdipMeasureString problem
 
GdipMeasureString problem
GdipMeasureString problem
 
-->