Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 16044

Rotate Text Using GDI+

$
0
0
Hello,

A while back dilettante and Schmidt helped me with precise positioning of text on a baseline.
http://www.vbforums.com/showthread.p...ing&highlight=

The text is positioned on a marker , and can be left, center, or right aligned.

Now I have a need to rotate this text, and I would like to rotate about a center point ,
and send this point to the DrawTextRotated function.

Any help would be appreciated.

Code:

Option Explicit


Dim gdiplusToken As Long


Private Enum TextJustify
    JustifyCenter
    JustifyLeft
    JustifyRight
End Enum


Dim stat 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
   
    Command1.Width = Form1.ScaleWidth
    Command1.Height = 25
    Command1.Left = 0
    Command1.Top = Form1.ScaleHeight - Command1.Height
    Command1.Caption = "execute"
    Command1.ZOrder (0)
   
    Picture1.Appearance = 0
    Picture1.Left = 20
    Picture1.Top = 20
    Picture1.Height = 366
    Picture1.Width = 552
    Picture1.AutoRedraw = True
   
    ' 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)

    Dim fontFamily As Long
    stat = GdipCreateFontFamilyFromName(StrPtr("Times New Roman"), 0, fontFamily)

   
    stat = GdipSetTextRenderingHint(graphics, TextRenderingHintAntiAlias)
   
   
   
   
    Call DrawTextRotated(graphics, _
                        "Whirligig", _
                        275, 195, _
                        JustifyCenter, _
                        fontFamily, FontStyleRegular, 96, _
                        45)
   
   


    ' Cleanup
    stat = GdipDeleteFontFamily(fontFamily)
    stat = GdipDeleteGraphics(graphics)
   
    Picture1.Refresh
   
End Sub


Private Sub DrawTextRotated(G As Long, _
                            text As String, _
                            x As Single, y As Single, _
                            justify As TextJustify, _
                            fontFamily As Long, FontStyle As FontStyle, fontSize As Single, _
                            rotationAngle As Single)

    '-------------------------------------------------------------------------------------------
    ' Get some font metrics
   
    Dim fontEmSize As Single
    fontEmSize = fontSize

    Dim EmHeight As Integer
    stat = GdipGetEmHeight(fontFamily, FontStyle, EmHeight)

    Dim CellAscent As Integer
    stat = GdipGetCellAscent(fontFamily, FontStyle, CellAscent)

    Dim CellDescent As Integer
    stat = GdipGetCellDescent(fontFamily, FontStyle, CellDescent)

    Dim fontAscentInPixels As Single
    fontAscentInPixels = (fontEmSize * CellAscent / EmHeight)
   
    Dim fontDescentInPixels As Single
    fontDescentInPixels = (fontEmSize * CellDescent / EmHeight)
    '-------------------------------------------------------------------------------------------
   

    '-------------------------------------------------------------------------------------------
    ' Set text baseline

    Dim baselineX As Single
    Dim baselineY As Single
   
    baselineX = x
    baselineY = y
    baselineX = Int(baselineX)
    baselineY = Int(baselineY)
    '-------------------------------------------------------------------------------------------
   
   
    '-------------------------------------------------------------------------------------------
    ' Set the text layout rect

    Dim txtRect As RECTF
    txtRect.Right = 0                                              'width  (0 means no boundary)
    txtRect.Bottom = 0                                            'height (0 means no boundary)
    txtRect.Left = baselineX                                      'x
    txtRect.Top = baselineY - fontAscentInPixels                  'y
    '-------------------------------------------------------------------------------------------

   
    '-------------------------------------------------------------------------------------------
    ' Initialize GDI+ objects
   
    Dim redPen As Long
    stat = GdipCreatePen1(&HFFFF0000, 1, UnitPixel, redPen)

    Dim bluePen As Long
    stat = GdipCreatePen1(&HFF0000FF, 1, UnitPixel, bluePen)
   
    Dim blackBrush As Long
    stat = GdipCreateSolidFill(&HFF000000, blackBrush)
   
    Dim txtFont As Long
    stat = GdipCreateFont(fontFamily, fontEmSize, FontStyle, UnitPixel, txtFont)
   
    Dim txtStringFormat As Long
    stat = GdipStringFormatGetGenericTypographic(txtStringFormat)
    '-------------------------------------------------------------------------------------------
   

    '-------------------------------------------------------------------------------------------
    ' Adjust for Alignment

    Select Case justify
        Case JustifyCenter
            stat = GdipSetStringFormatAlign(txtStringFormat, StringAlignmentCenter)
        Case JustifyLeft
            stat = GdipSetStringFormatAlign(txtStringFormat, StringAlignmentNear)
        Case JustifyRight
            stat = GdipSetStringFormatAlign(txtStringFormat, StringAlignmentFar)
    End Select
    '-------------------------------------------------------------------------------------------
   
   
    '-------------------------------------------------------------------------------------------
    ' Draw Text

    stat = GdipDrawString(G, StrPtr(text), -1, txtFont, txtRect, txtStringFormat, blackBrush)
    '-------------------------------------------------------------------------------------------
   
   
    '-------------------------------------------------------------------------------------------
    ' Draw position marker for the text

    ' horz baseline
    stat = GdipDrawLine(G, redPen, _
                        baselineX - (fontSize / 2), baselineY, _
                        baselineX + (fontSize / 2), baselineY)
   
    ' vert alignment marker
    stat = GdipDrawLine(G, redPen, _
                        baselineX, baselineY, _
                        baselineX, baselineY - fontAscentInPixels + fontDescentInPixels)
    '-------------------------------------------------------------------------------------------

                           
    '-------------------------------------------------------------------------------------------
    ' Draw rotation point
   
    ' horz line
    stat = GdipDrawLine(G, bluePen, _
                        baselineX - 10, _
                        baselineY - ((baselineY - _
                            (baselineY - fontAscentInPixels + fontDescentInPixels)) / 2), _
                        baselineX + 10, _
                        baselineY - ((baselineY - _
                            (baselineY - fontAscentInPixels + fontDescentInPixels)) / 2))
    ' vert line
    stat = GdipDrawLine(G, bluePen, _
                        baselineX, _
                        baselineY - (((baselineY - _
                            (baselineY - fontAscentInPixels + fontDescentInPixels)) / 2) _
                                - 10), _
                        baselineX, _
                        baselineY - (((baselineY - _
                            (baselineY - fontAscentInPixels + fontDescentInPixels)) / 2) _
                                + 10))
    '-------------------------------------------------------------------------------------------
   
   
    '-------------------------------------------------------------------------------------------
    ' Clean-up GDI+ objects

    stat = GdipDeletePen(redPen)
    stat = GdipDeletePen(bluePen)
    stat = GdipDeleteBrush(blackBrush)
    stat = GdipDeleteFont(txtFont)
    stat = GdipDeleteStringFormat(txtStringFormat)
    '-------------------------------------------------------------------------------------------
 
End Sub


Viewing all articles
Browse latest Browse all 16044

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>