Microsoft KB Archive/119673

From BetaArchive Wiki
Knowledge Base


How To Print with Rotated Text

Article ID: 119673

Article Last Modified on 2/24/2005



APPLIES TO

  • Microsoft Visual Basic 3.0 Professional Edition



This article was previously published under Q119673

SUMMARY

You can use the CreateFont or CreateFontIndirect Windows API calls to obtain a rotated font. This allows printing of rotated text to a Visual Basic form, picture control, or the printer object.

NOTE: This technique may not work under Windows version 3.0 or when you use a non-TrueType font.

MORE INFORMATION

You can specify the rotation for a font when you request a font from Windows with the CreateFont or CreateFontIndirect Windows API calls; you do this by setting the escapement of the desired font. The escapement is expressed in tenths of degrees of rotation, so an escapement of 450 would be a 45-degree rotation; an 1800 escapement would be 180 degrees of rotation.

Once you have obtained a handle to the desired font with CreateFont/CreateFontIndirect, you can then use the SelectObject Windows API call to select the font for the device context (DC) of the Visual Basic printer object or for a Visual Basic form or picture control.

The following is a sample program that demonstrates printing rotated text in a Visual Basic picture control:

  1. Start a new project in Visual Basic. Form1 is created by default.
  2. Add a command button (Command1) and a picture box (Picture1) to the form.
  3. Create a new module and add the following code to the General declarations section of the module (NOTE: Each declaration must be placed on one line.):

    Declare Function CreateFontIndirect Lib "GDI" (lpLogFont As Any)
                                                  As Integer
    Declare Function SelectObject Lib "GDI" (ByVal hDC As Integer,
                                             ByVal hgdiObj As Integer)
                                            As Integer Declare Function  
    DeleteObject Lib "GDI" (ByVal hgdiObj As Integer)
                                            As Integer
                                            
    Type LOGFONT_TYPE
        lfHeight As Integer
        lfWidth As Integer
        lfEscapement As Integer
        lfOrientation As Integer
        lfWeight As Integer
        lfItalic As String * 1
        lfUnderline As String * 1
        lfStrikeOut As String * 1
        lfCharSet As String * 1
        lfOutPrecision As String * 1
        lfClipPrecision As String * 1
        lfQuality As String * 1
        lfPitchAndFamily As String * 1
        lfFaceName As String * 32
    End Type
    
                        
  4. Add the following code to the Command1_Click event:

    Sub Command1_Click ()
      Dim font As LOGFONT_TYPE
      Dim prevFont As Integer, hFont As Integer, ret As Integer
    
      Const FONTSIZE = 12   ' Desired point size of font
    
      font.lfEscapement = 1800    ' 180-degree rotation
      font.lfFaceName = "Arial" + Chr$(0)
    
      ' 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.Width \ 2
      Picture1.CurrentY = Picture1.Height \ 2
      Picture1.Print "Rotated Text"
    
      ' Clean up by restoring original font.
      ret = SelectObject(Picture1.hDC, prevFont)
      ret = DeleteObject(hFont)
    
      Picture1.Print "Normal Text"
    End Sub
    
                        
  5. Press F5 to run the program. Choose the new command button and the string "Rotated Text" will be printed with a rotation of 180 degrees on the picture control.


Keywords: kbhowto kbprint KB119673