dattas Level: Trainee
 Registered: 25-02-2005 Posts: 1
|
printing w/ resize & child windows
Greetings,
I have a program that pasicly needs to print a picture with some label controls on top of the piture.
i have used the code on this page: http://www.andreavb.com/tip070013.html
But when i do it just prints the top left of the page but it prints with the labels on it
The picture is 8.5 X 11 inches (for invoices)
my code looks like this:
Option Explicit
Private Declare Function SendMessage Lib "user32.dll" 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 Form_Load()
CommonDialog1.CancelError = True
Command1.Caption = "Load Picture"
Command2.Caption = "Print Preview"
Command3.Caption = "Print"
End Sub
Private Sub Command1_Click()
Dim sFileFilter As String
On Error GoTo ErrHandler
sFileFilter = "Bitmap Files (*.bmp)/*.bmp/"
sFileFilter = sFileFilter & "GIF Files (*.gif)/*.gif/"
sFileFilter = sFileFilter & "Icon Files (*.ico)/*.ico/"
sFileFilter = sFileFilter & "JPEG Files (*.jpg)/*.jpg/"
sFileFilter = sFileFilter & "Windows MetaFiles (*.wmf)/.wmf"
With CommonDialog1
.Filter = sFileFilter
.ShowOpen
If .FileName <> " " Then
Picture2.Picture = LoadPicture(.FileName)
End If
End With
ErrHandler:
Exit Sub
End Sub
Private Sub Command2_Click()
Dim dRatio As Double
dRatio = ScalePicPreviewToPrinterInches(Picture1)
PrintRoutine Picture1, dRatio
End Sub
Private Sub Command3_Click()
Printer.ScaleMode = vbInches
PrintRoutine Printer
Printer.EndDoc
End Sub
Private Function ScalePicPreviewToPrinterInches _
(picPreview As PictureBox) As Double
Dim Ratio As Double ' Ratio between Printer and Picture
Dim LRGap As Double, TBGap As Double
Dim HeightRatio As Double, WidthRatio As Double
Dim PgWidth As Double, PgHeight As Double
Dim smtemp As Long
' Get the physical page size in Inches:
PgWidth = Printer.Width / 1440
PgHeight = Printer.Height / 1440
' Find the size of the non-printable area on the printer to
' use to offset coordinates. These formulas assume the
' printable area is centered on the page:
smtemp = Printer.ScaleMode
Printer.ScaleMode = vbInches
LRGap = (PgWidth - Printer.ScaleWidth) / 2
TBGap = (PgHeight - Printer.ScaleHeight) / 2
Printer.ScaleMode = smtemp
' Scale PictureBox to Printer's printable area in Inches:
picPreview.ScaleMode = vbInches
' Compare the height and with ratios to determine the
' Ratio to use and how to size the picture box:
HeightRatio = picPreview.ScaleHeight / PgHeight
WidthRatio = picPreview.ScaleWidth / PgWidth
If HeightRatio < WidthRatio Then
Ratio = HeightRatio
smtemp = picPreview.Container.ScaleMode
picPreview.Container.ScaleMode = vbInches
picPreview.Width = PgWidth * Ratio
picPreview.Container.ScaleMode = smtemp
Else
Ratio = WidthRatio
smtemp = picPreview.Container.ScaleMode
picPreview.Container.ScaleMode = vbInches
picPreview.Height = PgHeight * Ratio
picPreview.Container.ScaleMode = smtemp
End If
' Set default properties of picture box to match printer
' There are many that you could add here:
picPreview.Scale (0, 0)-(PgWidth, PgHeight)
picPreview.Font.Name = Printer.Font.Name
picPreview.FontSize = Printer.FontSize * Ratio
picPreview.ForeColor = Printer.ForeColor
picPreview.Cls
ScalePicPreviewToPrinterInches = Ratio
End Function
Private Sub PrintRoutine(objPrint As Object, _
Optional Ratio As Double = 1)
Dim rv As Long
Dim ar As Boolean
' All dimensions in inches:
With Picture2
'Save ReDraw value
ar = .AutoRedraw
'Set persistance
.AutoRedraw = True
'Draw controls to picture box
rv = SendMessage(.hwnd, WM_PAINT, .hDC, 0)
rv = SendMessage(.hwnd, WM_PRINT, .hDC, _
PRF_CHILDREN Or PRF_CLIENT Or PRF_OWNED)
'Refresh image to picture property
.Picture = .Image
'Copy picture to Printer
objPrint.PaintPicture .Picture, 0, 0, 8, 11.5
'Restore backcolor (Re-load picture if picture was used)
Picture2.Line (0, 0)-(.ScaleWidth, .ScaleHeight), .BackColor, BF
'Restore ReDraw
.AutoRedraw = ar
End With
End Sub |
Thanks for any and all help!
|