Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitlePrint a centered image of a form in Landscape mode
DescriptionThis example shows how to print a centered image of a form in Landscape mode in Visual Basic 6.
KeywordsPrintForm, print form
CategoriesGraphics
 
The program simulates Alt-PrntScrn to copy an image of the form to the clipboard. It pastes the image into a PictureBox and uses the Printer's PaintPicture method to copy the image to the printer suitably scaled and centered.
 
Private Sub CmdPrint_Click()
Dim xmin As Single
Dim ymin As Single
Dim wid As Single
Dim hgt As Single
Dim aspect As Single

    MousePointer = vbHourglass
    DoEvents
    
    ' ***************************************
    ' Copy the form's image to the clipboard.
    ' ***************************************
    ' Press Alt.
    keybd_event VK_MENU, 0, 0, 0
    DoEvents
    
    ' Press Print Scrn.
    keybd_event VK_SNAPSHOT, 1, 0, 0
    DoEvents

    ' Release Alt.
    keybd_event VK_MENU, 0, KEYEVENTF_KEYUP, 0
    DoEvents
    
    ' Copy the image into the hidden PictureBox.
    HiddenPict.Picture = Clipboard.GetData(vbCFBitmap)
    
    ' Print the image.
    Printer.Orientation = vbPRORLandscape
    
    If NormalOption.Value Then
        ' Center the image.
        wid = Printer.ScaleX(HiddenPict.ScaleWidth, _
            ScaleMode, Printer.ScaleMode)
        hgt = Printer.ScaleY(HiddenPict.ScaleHeight, _
            ScaleMode, Printer.ScaleMode)
        xmin = (Printer.ScaleWidth - wid) / 2
        ymin = (Printer.ScaleHeight - hgt) / 2
    Else
        ' Make the image as large as possible
        ' without distortion.
        aspect = HiddenPict.ScaleHeight / _
            HiddenPict.ScaleWidth
        wid = Printer.ScaleWidth
        hgt = Printer.ScaleHeight
        If hgt / wid > aspect Then
            hgt = aspect * wid
            xmin = Printer.ScaleLeft
            ymin = (Printer.ScaleHeight - hgt) / 2
        Else
            wid = hgt / aspect
            xmin = (Printer.ScaleWidth - wid) / 2
            ymin = Printer.ScaleTop
        End If
    End If
    
    Printer.PaintPicture HiddenPict.Picture, _
        xmin, ymin, wid, hgt
    Printer.EndDoc

    MousePointer = vbDefault
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated