HOWTO: Print Rotated Text Using Win32 APIID: Q154515
|
By using the CreateFont and CreateFontIndirect Win32 API calls, it is possible to rotate text on a Device context such as a picture box, a form, or the Printer Object. This article provides a Win32 Code sample demonstrating how this can be done.
Option Explicit
Private Declare Function CreateFontIndirect Lib "gdi32" Alias _
"CreateFontIndirectA" (lpLogFont As LOGFONT) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc _
As Long, ByVal hObject As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal _
hObject As Long) As Long
Private Const LF_FACESIZE = 32
Private Type LOGFONT
lfHeight As Long
lfWidth As Long
lfEscapement As Long
lfOrientation As Long
lfWeight As Long
lfItalic As Byte
lfUnderline As Byte
lfStrikeOut As Byte
lfCharSet As Byte
lfOutPrecision As Byte
lfClipPrecision As Byte
lfQuality As Byte
lfPitchAndFamily As Byte
lfFaceName as String * LF_FACESIZE
End Type
Sub Command1_Click()
Dim font As LOGFONT
Dim prevFont As Long, hFont As Long, ret As Long
Const FONTSIZE = 10 ' Desired point size of font
font.lfEscapement = 1800 ' 180-degree rotation
font.lfFaceName = "Arial" & Chr$(0) 'Null character at end
' Windows expects the font size to be in pixels and to
' be negative if you are specifying the character height
' you want.
font.lfHeight = (FONTSIZE * -20) / Screen.TwipsPerPixelY
hFont = CreateFontIndirect(font)
prevFont = SelectObject(Picture1.hdc, hFont)
Picture1.CurrentX = Picture1.Left + Picture1.Width / 2
Picture1.CurrentY = Picture1.ScaleHeight / 2
Picture1.Print "Rotated Text"
' Clean up by restoring original font.
ret = SelectObject(Picture1.hdc, prevFont)
ret = DeleteObject(hFont)
Picture1.CurrentY = Picture1.ScaleHeight / 2
Picture1.Print "Normal Text"
End Sub
For additional information about how to properly rotate text using a printer, please see the following article in the Microsoft Knowledge Base:
Q175535 PRB: Problems Printing Rotated Text on Visual Basic 5.0 SP2For additional information about the 16-bit equivalent of this sample, please see the following article(s) in the Microsoft Knowledge Base:
Q119673 How to Print with Rotated Text
Additional query words:
Keywords : kbAPI kbSDKWin32 kbVBp kbVBp400 kbVBp600 kbGrpVB kbDSupport
Version : WINDOWS:4.0,6.0
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: July 19, 1999