Results 1 to 6 of 6

Thread: [RESOLVED] printing to printer using PaintPicture

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2002
    Location
    new zealand (kiwi)!
    Posts
    202

    Resolved [RESOLVED] printing to printer using PaintPicture

    I am trying to print a graph to a printer.
    I have a form with a Picturebox on it, scaled to the Printer.Width, .Height
    Inside this picturebox I have 3 Pictureboxes (one for the graph lines, one for the Y axis marks, one for x axis marks). Graph data and axis markers are drawn using .Line commands
    I can make a correct graph on the form. The problem is how to print it!
    -Printform has many bugs, so its no use.
    I have tried to use Printer.Paintpicture mainpicture
    but it only prints one of the three pictureboxes.

    How do I do this?
    Last edited by Hack; Jun 16th, 2008 at 07:10 AM. Reason: Added RESOLVED to thread title Last edited by donW : Today at 12:19 AM.

  2. #2
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: printing to printer using PaintPicture

    Add another PcitrueBox named PicAux, it will be invisible. Then assuming the PictureBox that contains all the drawing and controls is named Picture1..
    Code:
    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 Sub Command1_Click()
        BitBlt PicAux.hDC, 0, 0, Picture1.ScaleWidth, Picture1.ScaleWidth, _
               Picture1.hDC, 0, 0, vbSrcCopy
        Printer.PaintPicture PicAux.Image, 300, 300 'print at x=300 and y=300
        Printer.EndDoc
    End Sub
    Private Sub Form_Load()
        Picture1.AutoRedraw = False
        Picture1.ScaleMode = vbPixels
        PicAux.AutoRedraw = True
        PicAux.ScaleMode = vbPixels
        PicAux.Width = Picture1.Width
        PicAux.Height = Picture1.Height
        PicAux.Visible = False
    End Sub
    This will work as far as your main Picture (Picture1) is totally visible on screen when you call this, if there are hidden/overlapped parts then BitBlt won't be able to see it.
    Last edited by jcis; Jun 15th, 2008 at 07:12 PM.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jul 2002
    Location
    new zealand (kiwi)!
    Posts
    202

    Re: printing to printer using PaintPicture

    Thanks that works Ok
    I have one strange problem still (not printing related)
    Even though I am doing a .CLS on the form and all pictureboxes
    the previous graph does not clear!
    The pictureboxes & form are all set to Autoredraw = true.

  4. #4
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: printing to printer using PaintPicture

    That's weird, Lines drawn at runtime should always clear when using .CLS

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jul 2002
    Location
    new zealand (kiwi)!
    Posts
    202

    Re: printing to printer using PaintPicture

    found that problem ! Old code was setting picture = image

  6. #6
    PowerPoster jcis's Avatar
    Join Date
    Jan 2003
    Location
    Argentina
    Posts
    4,430

    Re: [RESOLVED] printing to printer using PaintPicture

    This Thread is from 10 days ago, but i'll post this snippet i just found, it's better because it works even when the PBox is half/totally off-screen:
    Code:
    Private Declare Function SendMessage Lib "user32" Alias _
       "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
       ByVal wParam As Long, ByVal lParam As Long) As Long
    
    Private Const WM_PAINT = &HF
    Private Const WM_PRINT = &H317
    Private Const PRF_CLIENT = &H4&    ' Draw the window's client area
    Private Const PRF_CHILDREN = &H10& ' Draw all visible child
    Private Const PRF_OWNED = &H20&    ' Draw all owned windows
    
    Private Sub Command1_Click()
        Dim rv As Long
       
       Picture2.AutoRedraw = True
       rv = SendMessage(Picture1.hwnd, WM_PAINT, Picture2.hDC, 0)
       rv = SendMessage(Picture1.hwnd, WM_PRINT, Picture2.hDC, _
            PRF_CHILDREN + PRF_CLIENT + PRF_OWNED)
       Set Picture2.Picture = Picture2.Image
       Picture2.AutoRedraw = False
       'PRINT:
       Printer.PaintPicture Picture2.Picture, 0, 0
       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