Date: 02-16-2022
Return to Index
created by gbSnippets
'Compilable Example: (Jose Includes)
#Compiler PBWin 10
#Compile Exe
#Dim All
%Unicode = 1
#Include "Win32API.inc"
Enum Equates Singular
IDC_ButtonA
IDC_ButtonB
IDC_ButtonC
IDC_ButtonD
IDC_Graphic
End Enum
'Orientation Equates
%LandScape = 2
%Portrait = 1
%UseDefault = 0
'DefaultPrinter Equates
%ChoosePrinter = 0
%DefaultPrinter = 1
'RenderSize Equates
%SizeToFit = 0
%NaturalSize = 1
'Position Equates
%UseXYInchCoordinates = 0
%CenterOnPage = 1
Global hDlg As Dword
Function PBMain() As Long
Dialog New Pixels, 0, "Dialog Print Example",300,300,320,450, %WS_OverlappedWindow To hDlg
Control Add Button, hDlg, %IDC_ButtonA,"Print Dialog (landscape.defprinter.sizetofit.centered)", 10,10,260,20
Control Add Button, hDlg, %IDC_ButtonB,"Print Dialog (landscape.defprinter.naturalsize.centered)", 10,35,260,20
Control Add Button, hDlg, %IDC_ButtonC,"Print Dialog (portrait.defprinter.sizetofit.use0,0)", 10,60,260,20
Control Add Button, hDlg, %IDC_ButtonD,"Print Dialog (portrait.defprinter.naturalsize.use1.0-1.0)", 10,85,260,20
Control Add Graphic, hDlg, %IDC_Graphic,"", 10,125,300,300
Graphic Attach hDlg, %IDC_Graphic
Graphic Render Bitmap "chess.bmp",(0,0)-(299,299)
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Select Case Cb.Msg
Case %WM_Command
Select Case Cb.Ctl
Case %IDC_ButtonA : PrintDialog hDlg, %DefaultPrinter, %Landscape, %SizeToFit, %CenterOnPage, 0, 0
Case %IDC_ButtonB : PrintDialog hDlg, %DefaultPrinter, %Landscape, %NaturalSize, %CenterOnPage, 0, 0
Case %IDC_ButtonC : PrintDialog hDlg, %DefaultPrinter, %Portrait, %SizeToFit, %UseXYInchCoordinates, 0, 0
Case %IDC_ButtonD : PrintDialog hDlg, %DefaultPrinter, %Portrait, %NaturalSize, %UseXYInchCoordinates, 1.0,1.0
End Select
End Select
End Function
Sub PrintDialog(hDialog As Dword, PrinterSelection As Long, PrinterOrientation As Long, RenderSize As Long, _
Location As Long, x0 As Single, y0 As Single)
'Orientation Equates
'%LandScape = 2
'%Portrait = 1
'%UseDefault = 0
'DefaultPrinter Equates
'%ChoosePrinter = 0
'%DefaultPrinter = 1
'RenderSize Equates
'%SizeToFit = 0
'%NaturalSize = 1
'Location Equates
'%UseXYInchCoordinates = 0
'%CenterOnPage = 1
Local x,y,wNew,hNew,wImg,hImg,wCont,hCont,iResult As Long
Local hDC_Dialog, hDC_Printer, deskDC As Dword
Local gXppi, gyPPI, pXppi, pYppi As Long
If RenderSize = %SizeToFit Then Location = %CenterOnPage
'Select Printer
Select Case PrinterSelection
Case %DefaultPrinter : XPrint Attach Default
Case %ChoosePrinter : XPrint Attach Choose
End Select
If Len(XPrint$)=0 Then Exit Sub
'Set Printer Orientation
Select Case PrinterOrientation
Case %Landscape : XPrint Set Orientation 2
Case %Portrait : XPrint Set Orientation 1
Case %UseDefault 'no action. use whatever printer dialog was set for
End Select
'Get Printer Properties
XPrint Get Client To wCont, hCont
XPrint Get PPI To pXppi, pYppi
XPrint Get DC To hDC_Printer
'Get Screen PPI
deskDC = GetDC(%HWND_Desktop)
gXppi = GetDeviceCaps(deskDC, %LogPixelsX)
gYppi = GetDeviceCaps(deskDC, %LogPixelsX)
ReleaseDC %HWND_Desktop, deskDC
'Calculate Print Size
Dialog Get Size hDialog To wImg,hImg
hDC_Dialog = GetWindowDC (hDialog)
Select Case RenderSize
Case %NaturalSize
wNew = wImg * pXppi/gXppi
hNew = hImg * pYppi/gYppi
Case %SizeToFit
'wCont,hCont = container size : hImg,wImg = original image size : wNew,hNew = image size to fit in container
wNew = wImg / Max(wImg / wCont, hImg / hCont)
hNew = hImg / Max(wImg / wCont, hImg / hCont)
End Select
'Calculate Upper/Left Corner
Select Case Location
Case %CenterOnPage
x = (wCont-wNew)/2 : y = (hCont-hNew)/2 'upper/left position so resized image is centered
Case %UseXYInchCoordinates
x = x0 * pXppi : y = y0 * pYppi
End Select
'Print
XPrint Get Orientation To PrinterOrientation
Select Case PrinterOrientation
Case %Landscape
ReDim Pt(0 To 2) As PointAPI
Pt(0).x = y : Pt(0).y = x + wNew -1
Pt(1).x = y : Pt(1).y = x
Pt(2).x = y+hNew-1 : Pt(2).y = x + wNew -1
PlgBlt(hDC_Printer, Pt(0), hDC_Dialog, 0, 0, wImg, hImg, 0, 0, 0) ' Draw rotated image
Case %Portrait
StretchBlt hDC_Printer,x,y,wNew,hNew, hDC_Dialog,0,0,wImg,hImg,%SRCCopy
End Select
ReleaseDC hDialog, hDC_Dialog
XPrint Close
End Sub
'gbs_01358
'Date: 05-11-2013
http://www.garybeene.com/sw/gbsnippets.htm