CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Guest

    Print Problem.. Help me..

    Hi...
    I am a beginner... So.. Please help me..
    I want to print out the screen that we can see..
    Print as the screen...



  2. #2
    Join Date
    May 1999
    Location
    Omika, Japan
    Posts
    729

    Re: Print Problem.. Help me..

    Hi,

    Since i dont know what exactly is your requirement, i am posting here a complete set of
    printing routines that i had got from somewhere -
    I dont remember from where, otherwise i dont mind acknowledging them here!
    Ravi

    Check how you can use it.

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '
    ' Visual Basic 4.0 16/32 Capture Routines '
    ' This module contains several routines for capturing windows into a
    ' picture. All the routines work on both 16 and 32 bit Windows platforms.
    ' The routines also have palette support.
    '
    ' CreateBitmapPicture - Creates a picture object from a bitmap and palette
    ' CaptureWindow - Captures any window given a window handle
    ' CaptureActiveWindow - Captures the active window on the desktop
    ' CaptureForm - Captures the entire form
    ' CaptureClient - Captures the client area of a form
    ' CaptureScreen - Captures the entire screen
    ' PrintPictureToFitPage - prints any picture as big as possible on the page
    '
    ' NOTES
    ' - No error trapping is included in these routines

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '
    option Explicit
    option Base 0


    private Type PALETTEENTRY
    peRed as Byte
    peGreen as Byte
    peBlue as Byte
    peFlags as Byte
    End Type

    private Type LOGPALETTE
    palVersion as Integer
    palNumEntries as Integer
    palPalEntry(255) as PALETTEENTRY ' Enough for 256 colors
    End Type

    private Type GUID
    Data1 as Long
    Data2 as Integer
    Data3 as Integer
    Data4(7) as Byte
    End Type

    #If Win32 then
    private Const RASTERCAPS as Long = 38
    private Const RC_PALETTE as Long = &H100
    private Const SIZEPALETTE as Long = 104

    private Type RECT
    Left as Long
    Top as Long
    Right as Long
    Bottom as Long
    End Type

    private Declare Function CreateCompatibleDC Lib "GDI32" (byval hDC as Long) as Long
    private Declare Function CreateCompatibleBitmap Lib "GDI32" ( _
    byval hDC as Long, byval nWidth as Long, byval nHeight as Long) as Long
    private Declare Function GetDeviceCaps Lib "GDI32" (byval hDC as Long, _
    byval iCapabilitiy as Long) as Long
    private Declare Function GetSystemPaletteEntries Lib "GDI32" ( _
    byval hDC as Long, byval wStartIndex as Long, _
    byval wNumEntries as Long, lpPaletteEntries as PALETTEENTRY) as Long
    private Declare Function CreatePalette Lib "GDI32" (lpLogPalette as LOGPALETTE) as Long
    private Declare Function SelectObject Lib "GDI32" (byval hDC as Long, _
    byval hObject as Long) as Long
    private Declare Function BitBlt Lib "GDI32" (byval hDCDest as Long, _
    byval XDest as Long, byval YDest as Long, byval nWidth as Long, _
    byval nHeight as Long, byval hDCSrc as Long, byval XSrc as Long, _
    byval YSrc as Long, byval dwRop as Long) as Long
    private Declare Function DeleteDC Lib "GDI32" (byval hDC as Long) as Long
    private Declare Function GetForegroundWindow Lib "USER32" () as Long
    private Declare Function SelectPalette Lib "GDI32" (byval hDC as Long, _
    byval hPalette as Long, byval bForceBackground as Long) as Long
    private Declare Function RealizePalette Lib "GDI32" (byval hDC as Long) as Long
    private Declare Function GetWindowDC Lib "USER32" (byval hWnd as Long) as Long
    private Declare Function GetDC Lib "USER32" (byval hWnd as Long) as Long
    private Declare Function GetWindowRect Lib "USER32" (byval hWnd as Long, _
    lpRect as RECT) as Long
    private Declare Function ReleaseDC Lib "USER32" (byval hWnd as Long, _
    byval hDC as Long) as Long
    private Declare Function GetDesktopWindow Lib "USER32" () as Long

    private Type PicBmp
    Size as Long
    Type as Long
    hBmp as Long
    hPal as Long
    Reserved as Long
    End Type

    private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" ( _
    PicDesc as PicBmp, RefIID as GUID, byval fPictureOwnsHandle as Long, _
    IPic as IPicture) as Long

    #ElseIf Win16 then
    private Const RASTERCAPS as Integer = 38
    private Const RC_PALETTE as Integer = &H100
    private Const SIZEPALETTE as Integer = 104

    private Type RECT
    Left as Integer
    Top as Integer
    Right as Integer
    Bottom as Integer
    End Type

    private Declare Function CreateCompatibleDC Lib "GDI" (byval hDC as Integer) as Integer
    private Declare Function CreateCompatibleBitmap Lib "GDI" ( _
    byval hDC as Integer, byval nWidth as Integer, byval nHeight as Integer) as Integer
    private Declare Function GetDeviceCaps Lib "GDI" ( _
    byval hDC as Integer, byval iCapabilitiy as Integer) as Integer
    private Declare Function GetSystemPaletteEntries Lib "GDI" ( _
    byval hDC as Integer, byval wStartIndex as Integer, _
    byval wNumEntries as Integer, _
    lpPaletteEntries as PALETTEENTRY) as Integer
    private Declare Function CreatePalette Lib "GDI" ( _
    lpLogPalette as LOGPALETTE) as Integer
    private Declare Function SelectObject Lib "GDI" (byval hDC as Integer, _
    byval hObject as Integer) as Integer
    private Declare Function BitBlt Lib "GDI" (byval hDCDest as Integer, _
    byval XDest as Integer, byval YDest as Integer, _
    byval nWidth as Integer, byval nHeight as Integer, _
    byval hDCSrc as Integer, byval XSrc as Integer, _
    byval YSrc as Integer, byval dwRop as Long) as Integer
    private Declare Function DeleteDC Lib "GDI" (byval hDC as Integer) as Integer
    private Declare Function GetForegroundWindow Lib "USER" _
    Alias "GetActiveWindow" () as Integer
    private Declare Function SelectPalette Lib "USER" (byval hDC as Integer, _
    byval hPalette as Integer, byval bForceBackground as Integer) as Integer
    private Declare Function RealizePalette Lib "USER" (byval hDC as Integer) as Integer
    private Declare Function GetWindowDC Lib "USER" (byval hWnd as Integer) as Integer
    private Declare Function GetDC Lib "USER" (byval hWnd as Integer) as Integer
    private Declare Function GetWindowRect Lib "USER" ( _
    byval hWnd as Integer, lpRect as RECT) as Integer
    private Declare Function ReleaseDC Lib "USER" (byval hWnd as Integer, _
    byval hDC as Integer) as Integer
    private Declare Function GetDesktopWindow Lib "USER" () as Integer

    private Type PicBmp
    Size as Integer
    Type as Integer
    hBmp as Integer
    hPal as Integer
    Reserved as Integer
    End Type

    private Declare Function OleCreatePictureIndirect Lib "oc25.dll" ( _
    PictDesc as PicBmp, RefIID as GUID, _
    byval fPictureOwnsHandle as Integer, IPic as IPicture) as Integer
    #End If

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '
    ' CreateBitmapPicture
    ' - Creates a bitmap type Picture object from a bitmap and palette
    ' ' hBmp
    ' - Handle to a bitmap
    ' ' hPal
    ' - Handle to a Palette
    ' - Can be null if the bitmap doesn't use a palette
    ' ' Returns
    ' - Returns a Picture object containing the bitmap
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '
    #If Win32 then
    public Function CreateBitmapPicture(byval hBmp as Long, _
    byval hPal as Long) as Picture
    Dim r as Long
    #ElseIf Win16 then
    public Function CreateBitmapPicture(byval hBmp as Integer, _
    byval hPal as Integer) as Picture

    Dim r as Integer
    #End If
    Dim Pic as PicBmp
    ' IPicture requires a reference to "Standard OLE Types"
    Dim IPic as IPicture
    Dim IID_IDispatch as GUID

    ' Fill in with IDispatch Interface ID
    With IID_IDispatch
    .Data1 = &H20400
    .Data4(0) = &HC0
    .Data4(7) = &H46
    End With

    ' Fill Pic with necessary parts
    With Pic
    .Size = len(Pic) ' Length of structure
    .Type = vbPicTypeBitmap ' Type of Picture (bitmap)
    .hBmp = hBmp ' Handle to bitmap
    .hPal = hPal ' Handle to palette (may be null)
    End With

    ' Create Picture object
    r = OleCreatePictureIndirect(Pic, IID_IDispatch, 1, IPic)

    ' Return the new Picture object
    set CreateBitmapPicture = IPic

    End Function

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '
    ' CaptureWindow
    ' - Captures any portion of a window
    ' ' hWndSrc
    ' - Handle to the window to be captured
    ' ' Client
    ' - If true CaptureWindow captures from the client area of the window
    ' - If false CaptureWindow captures from the entire window
    ' ' LeftSrc, TopSrc, WidthSrc, HeightSrc
    ' - Specify the portion of the window to capture
    ' - Dimensions need to be specified in pixels
    ' ' Returns
    ' - Returns a Picture object containing a bitmap of the specified
    ' portion of the window that was captured
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '
    #If Win32 then
    public Function CaptureWindow(byval hWndSrc as Long, _
    byval Client as Boolean, byval LeftSrc as Long, _
    byval TopSrc as Long, byval WidthSrc as Long, _
    byval HeightSrc as Long) as Picture

    Dim hDCMemory as Long
    Dim hBmp as Long
    Dim hBmpPrev as Long
    Dim r as Long
    Dim hDCSrc as Long
    Dim hPal as Long
    Dim hPalPrev as Long
    Dim RasterCapsScrn as Long
    Dim HasPaletteScrn as Long
    Dim PaletteSizeScrn as Long

    #ElseIf Win16 then
    public Function CaptureWindow(byval hWndSrc as Integer, _
    byval Client as Boolean, byval LeftSrc as Integer, _
    byval TopSrc as Integer, byval WidthSrc as Long, _
    byval HeightSrc as Long) as Picture

    Dim hDCMemory as Integer
    Dim hBmp as Integer
    Dim hBmpPrev as Integer
    Dim r as Integer
    Dim hDCSrc as Integer
    Dim hPal as Integer
    Dim hPalPrev as Integer
    Dim RasterCapsScrn as Integer
    Dim HasPaletteScrn as Integer
    Dim PaletteSizeScrn as Integer

    #End If

    Dim LogPal as LOGPALETTE

    ' Depending on the value of Client get the proper device context
    If Client then
    hDCSrc = GetDC(hWndSrc) ' get device context for client area
    else
    hDCSrc = GetWindowDC(hWndSrc) ' get device context for entire window
    End If

    ' Create a memory device context for the copy process
    hDCMemory = CreateCompatibleDC(hDCSrc)
    ' Create a bitmap and place it in the memory DC
    hBmp = CreateCompatibleBitmap(hDCSrc, WidthSrc, HeightSrc)
    hBmpPrev = SelectObject(hDCMemory, hBmp)

    ' get screen properties
    RasterCapsScrn = GetDeviceCaps(hDCSrc, RASTERCAPS) ' Raster capabilities
    HasPaletteScrn = RasterCapsScrn And RC_PALETTE ' Palette support
    PaletteSizeScrn = GetDeviceCaps(hDCSrc, SIZEPALETTE) ' Size of palette

    ' If the screen has a palette make a copy and realize it
    If HasPaletteScrn And (PaletteSizeScrn = 256) then
    ' Create a copy of the system palette
    LogPal.palVersion = &H300
    LogPal.palNumEntries = 256
    r = GetSystemPaletteEntries(hDCSrc, 0, 256, LogPal.palPalEntry(0))
    hPal = CreatePalette(LogPal)
    ' Select the new palette into the memory DC and realize it
    hPalPrev = SelectPalette(hDCMemory, hPal, 0)
    r = RealizePalette(hDCMemory)
    End If

    ' Copy the on-screen image into the memory DC
    r = BitBlt(hDCMemory, 0, 0, WidthSrc, HeightSrc, hDCSrc, _
    LeftSrc, TopSrc, vbSrcCopy)

    ' Remove the new copy of the the on-screen image
    hBmp = SelectObject(hDCMemory, hBmpPrev)

    ' If the screen has a palette get back the palette that was selected
    ' in previously
    If HasPaletteScrn And (PaletteSizeScrn = 256) then
    hPal = SelectPalette(hDCMemory, hPalPrev, 0)
    End If

    ' Release the device context resources back to the system
    r = DeleteDC(hDCMemory)
    r = ReleaseDC(hWndSrc, hDCSrc)

    ' Call CreateBitmapPicture to create a picture object from the bitmap
    ' and palette handles. then return the resulting picture object.
    set CaptureWindow = CreateBitmapPicture(hBmp, hPal)

    End Function

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '
    ' CaptureScreen
    ' - Captures the entire screen
    ' ' Returns
    ' - Returns a Picture object containing a bitmap of the screen
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '
    public Function CaptureScreen() as Picture

    #If Win32 then
    Dim hWndScreen as Long
    #ElseIf Win16 then
    Dim hWndScreen as Integer
    #End If

    ' get a handle to the desktop window
    hWndScreen = GetDesktopWindow()

    ' Call CaptureWindow to capture the entire desktop give the handle and
    ' return the resulting Picture object
    set CaptureScreen = CaptureWindow(hWndScreen, false, 0, 0, _
    Screen.Width \ Screen.TwipsPerPixelX, _
    Screen.Height \ Screen.TwipsPerPixelY)

    End Function

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' ' CaptureForm
    ' - Captures an entire form including title bar and border
    ' ' frmSrc
    ' - The Form object to capture
    ' ' Returns
    ' - Returns a Picture object containing a bitmap of the entire form
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '
    public Function CaptureForm(frmSrc as Form) as Picture
    ' Call CaptureWindow to capture the entire form given it's window
    ' handle and then return the resulting Picture object
    set CaptureForm = CaptureWindow(frmSrc.hWnd, false, 0, 0, _
    frmSrc.ScaleX(frmSrc.Width, vbTwips, vbPixels), _
    frmSrc.ScaleY(frmSrc.Height, vbTwips, vbPixels))
    End Function

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' ' CaptureClient
    ' - Captures the client area of a form
    ' ' frmSrc
    ' - The Form object to capture
    ' ' Returns
    ' - Returns a Picture object containing a bitmap of the form's client
    ' area
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '
    public Function CaptureClient(frmSrc as Form) as Picture
    ' Call CaptureWindow to capture the client area of the form given it's
    ' window handle and return the resulting Picture object
    set CaptureClient = CaptureWindow(frmSrc.hWnd, true, 0, 0, _
    frmSrc.ScaleX(frmSrc.ScaleWidth, frmSrc.ScaleMode, vbPixels), _
    frmSrc.ScaleY(frmSrc.ScaleHeight, frmSrc.ScaleMode, vbPixels))
    End Function

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' ' CaptureActiveWindow
    ' - Captures the currently active window on the screen
    ' ' Returns
    ' - Returns a Picture object containing a bitmap of the active window
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '
    public Function CaptureActiveWindow() as Picture
    #If Win32 then
    Dim hWndActive as Long
    Dim r as Long
    #ElseIf Win16 then
    Dim hWndActive as Integer
    Dim r as Integer
    #End If
    Dim RectActive as RECT

    ' get a handle to the active/foreground window
    hWndActive = GetForegroundWindow()

    ' get the dimensions of the window
    r = GetWindowRect(hWndActive, RectActive)

    ' Call CaptureWindow to capture the active window given it's handle and
    ' return the Resulting Picture object
    set CaptureActiveWindow = CaptureWindow(hWndActive, false, 0, 0, _
    RectActive.Right - RectActive.Left, _
    RectActive.Bottom - RectActive.Top)
    End Function

    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' ' PrintPictureToFitPage
    ' - Prints a Picture object as big as possible
    ' ' Prn
    ' - Destination Printer object
    ' ' Pic
    ' - Source Picture object
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    '
    public Sub PrintPictureToFitPage(Prn as Printer, Pic as Picture)
    Const vbHiMetric as Integer = 8
    Dim PicRatio as Double
    Dim PrnWidth as Double
    Dim PrnHeight as Double
    Dim PrnRatio as Double
    Dim PrnPicWidth as Double
    Dim PrnPicHeight as Double

    ' Determine if picture should be printed in landscape or portrait and
    ' set the orientation
    If Pic.Height >= Pic.Width then
    Prn.Orientation = vbPRORPortrait ' Taller than wide
    else
    Prn.Orientation = vbPRORLandscape ' Wider than tall
    End If

    ' Calculate device independent Width to Height ratio for picture
    PicRatio = Pic.Width / Pic.Height

    ' Calculate the dimentions of the printable area in HiMetric
    PrnWidth = Prn.ScaleX(Prn.ScaleWidth, Prn.ScaleMode, vbHiMetric)
    PrnHeight = Prn.ScaleY(Prn.ScaleHeight, Prn.ScaleMode, vbHiMetric)
    ' Calculate device independent Width to Height ratio for printer
    PrnRatio = PrnWidth / PrnHeight

    ' Scale the output to the printable area
    If PicRatio >= PrnRatio then
    ' Scale picture to fit full width of printable area
    PrnPicWidth = Prn.ScaleX(PrnWidth, vbHiMetric, Prn.ScaleMode)
    PrnPicHeight = Prn.ScaleY(PrnWidth / PicRatio, vbHiMetric, _
    Prn.ScaleMode)
    else
    ' Scale picture to fit full height of printable area
    PrnPicHeight = Prn.ScaleY(PrnHeight, vbHiMetric, Prn.ScaleMode)
    PrnPicWidth = Prn.ScaleX(PrnHeight * PicRatio, vbHiMetric, _
    Prn.ScaleMode)
    End If

    ' print the picture using the PaintPicture method
    Prn.PaintPicture Pic, 0, 0, PrnPicWidth, PrnPicHeight
    End Sub
    '--------------------------------------------------------------------------


    public Sub main()
    PrintPictureToFitPage Printer, CaptureScreen()
    Printer.EndDoc
    End Sub






Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured