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

    Compatibility for Printing

    Background:
    I have a serious problem using Printer Object for Printing a report. The report is generated dynamically that contain Boxes, Pictures, Text, Lines, so on. The printing is done by using methods of Printer Object like Line, Print, PaintPicture. When I coded the application to print report, I was also printing the same to a picture box for preview.

    The Problem:
    Now, when I am printing the same on different printers, the printout is coming on two pages instead of one with graphics on one page and text on another page. This means that the compatibility for printing is not there accross various printers (devices). Can any one please let me know how to achieve this either using VB code or a set of API's. Can I find some examples regarding this on any site ?

    This is very urgent please.

    Thanks & Regards
    B. J. Sarma


  2. #2
    Join Date
    Jul 1999
    Location
    Athens, Hellas
    Posts
    769

    Re: Compatibility for Printing

    Why don't you capture the entire form, copy it to clipboard and then just print the picture. If you are interested, I have the code for the whole procedure and I can post it here. :-)

    Michael Vlastos
    Company MODUS SA
    Development Department
    Tel: +3-01-9414900

  3. #3
    Guest

    Re: Compatibility for Printing

    Could you please post the code.

    Thanks
    Sarma


  4. #4
    Join Date
    Jul 1999
    Location
    Athens, Hellas
    Posts
    769

    Re: Compatibility for Printing

    First, you create a picture on your form (Picture1). Then you set it's visible property to false. You add the next fragment of code at the cmdPrint_click() event:


    set Picture1.Picture = CaptureForm(me)
    PrintPicture Printer, Picture2.Picture
    Printer.EndDoc




    You have also to create a module1 and add there the following fragment of code:


    option Explicit

    Global Const INVERSE = 6
    Const SOLID = 0
    Const DOT = 2

    Global HoldX as Single
    Global HoldY as Single
    Global StartX as Single
    Global StartY as Single
    Global SavedDrawStyle
    Global SavedMode


    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
    'Enough for 256 colors
    palPalEntry(255) as PALETTEENTRY
    End Type

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


    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 Type PicBmp
    Size as Long
    Type as Long
    hBmp as Long
    hPal as Long
    Reserved as Long
    End Type

    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 CreateCompatibleBitmap Lib _
    "GDI32" (byval hDC as Long, byval nWidth as Long, _
    byval nHeight as Long) as Long
    private Declare Function CreateCompatibleDC Lib "GDI32" ( _
    byval hDC as Long) as Long
    private Declare Function CreatePalette Lib "GDI32" ( _
    lpLogPalette as LOGPALETTE) as Long
    private Declare Function DeleteDC Lib "GDI32" ( _
    byval hDC as Long) as Long
    private Declare Function GetDesktopWindow Lib "USER32" () as Long
    private Declare Function GetDeviceCaps Lib "GDI32" ( _
    byval hDC as Long, byval iCapabilitiy as Long) as Long
    private Declare Function GetForegroundWindow Lib "USER32" () _
    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 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 OleCreatePictureIndirect _
    Lib "olepro32.dll" (PicDesc as PicBmp, RefIID as GUID, _
    byval fPictureOwnsHandle as Long, IPic as IPicture) as Long
    private Declare Function RealizePalette Lib "GDI32" ( _
    byval hDC as Long) as Long
    private Declare Function ReleaseDC Lib "USER32" ( _
    byval hWnd as Long, byval hDC as Long) as Long
    private Declare Function SelectObject Lib "GDI32" ( _
    byval hDC as Long, byval hObject as Long) as Long
    private Declare Function SelectPalette Lib "GDI32" ( _
    byval hDC as Long, byval hPalette as Long, _
    byval bForceBackground as Long) as Long

    public Function CaptureForm(frmSrc as Form) as Picture
    on error GoTo ErrorRoutineErr

    'Call CaptureWindow to capture the entire form
    'given it's window
    'handle and then return the resulting Picture object
    set CaptureForm = CaptureWindow(frmSrc.hWnd, 0, 0, _
    frmSrc.ScaleX(frmSrc.Width, vbTwips, vbPixels), _
    frmSrc.ScaleY(frmSrc.Height, vbTwips, vbPixels))

    ErrorRoutineResume:
    Exit Function
    ErrorRoutineErr:
    MsgBox "Project1.Module1.CaptureForm" & Err & error
    resume next
    End Function

    public Function CreateBitmapPicture(byval hBmp as Long, _
    byval hPal as Long) as Picture

    on error GoTo ErrorRoutineErr

    Dim r as Long
    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
    'Length of structure
    .Size = len(Pic)
    'Type of Picture (bitmap)
    .Type = vbPicTypeBitmap
    'Handle to bitmap
    .hBmp = hBmp
    'Handle to palette (may be null)
    .hPal = hPal
    End With

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

    'Return the new Picture object
    set CreateBitmapPicture = IPic

    ErrorRoutineResume:
    Exit Function
    ErrorRoutineErr:
    MsgBox "Project1.Module1.CreateBitmapPicture" & Err & error
    resume next
    End Function

    public Function CaptureWindow(byval hWndSrc as Long, _
    byval LeftSrc as Long, _
    byval TopSrc as Long, byval WidthSrc as Long, _
    byval HeightSrc as Long) as Picture

    on error GoTo ErrorRoutineErr

    Dim hDCMemory as Long
    Dim hBmp as Long
    Dim hBmpPrev as Long
    Dim rc 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

    Dim LogPal as LOGPALETTE

    'get device context for the window
    hDCSrc = GetWindowDC(hWndSrc)

    '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
    'Raster capabilities
    RasterCapsScrn = GetDeviceCaps(hDCSrc, RASTERCAPS)
    'Palette support
    HasPaletteScrn = RasterCapsScrn And RC_PALETTE
    'Size of palette
    PaletteSizeScrn = GetDeviceCaps(hDCSrc, SIZEPALETTE)

    'If the screen has a palette, make a copy
    If HasPaletteScrn And (PaletteSizeScrn = 256) then
    'Create a copy of the system palette
    LogPal.palVersion = &H300
    LogPal.palNumEntries = 256
    rc = 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)
    rc = RealizePalette(hDCMemory)
    End If

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

    'Remove the new copy of 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
    rc = DeleteDC(hDCMemory)
    rc = 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)

    ErrorRoutineResume:
    Exit Function
    ErrorRoutineErr:
    MsgBox "Project1.Module1.CaptureWindow" & Err & error
    resume next
    End Function

    public Sub PrintPicture(Prn as Printer, Pic as Picture)
    on error GoTo ErrorRoutineErr

    Prn.PaintPicture Pic, 0, 0

    ErrorRoutineResume:
    Exit Sub
    ErrorRoutineErr:
    MsgBox "Project1.Module1.PrintPicture" & Err & error
    resume next
    End Sub




    Michael Vlastos
    Company MODUS SA
    Development Department
    Tel: +3-01-9414900

  5. #5
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: Compatibility for Printing

    Hi

    Nice coding - do you want me to put it onto the site ?



    Chris Eastwood

    CodeGuru - the website for developers
    http://www.codeguru.com/vb

  6. #6
    Join Date
    Jul 1999
    Location
    Athens, Hellas
    Posts
    769

    Re: Compatibility for Printing

    To tell you the truth it was not my own code. I just found it on a book and customised it for my purposes. I don't know if it is legal to put it onto the site. You have MY permission, but I don't know if you have the author's. Do what you think! :-)

    Michael Vlastos
    Company MODUS SA
    Development Department
    Athens, Greece
    Tel: +3-01-9414900

  7. #7
    Join Date
    May 1999
    Location
    Oxford UK
    Posts
    1,459

    Re: Compatibility for Printing

    I think that it'll be ok - I'll try and post it today.


    Chris Eastwood

    CodeGuru - the website for developers
    http://www.codeguru.com/vb

  8. #8
    Join Date
    Jul 1999
    Location
    Athens, Hellas
    Posts
    769

    Re: Compatibility for Printing

    I would really appreciate if you could send me (by email possibly) the link after you post it. Thanx!

    Michael Vlastos
    Company MODUS SA
    Development Department
    Athens, Greece
    Tel: +3-01-9414900

  9. #9
    Guest

    Re: Compatibility for Printing

    Thanks for the code Dr_Michael. It is really very useful.

    Sarma


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