[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?
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.
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.
Re: printing to printer using PaintPicture
That's weird, Lines drawn at runtime should always clear when using .CLS
Re: printing to printer using PaintPicture
found that problem ! Old code was setting picture = image
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