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 = 1
%Portrait = 0
%UseDefault = -1
'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, hBMP_Dialog, hDC_Graphic, 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 %Landscape
Case %Portrait : XPrint Set Orientation %Portrait
Case %UseDefault 'no action. use whatever printer dialog was set for
End Select
'Hidden Graphic
Dialog Get Size hDialog To wImg,hImg
hDC_Dialog = GetWindowDC (hDialog)
Graphic Bitmap New wImg,hImg To hBMP_Dialog
Graphic Attach hBMP_Dialog, 0
Graphic Get DC To hDC_Graphic
BitBlt hDC_Graphic, 0,0,wImg,hImg, hDC_Dialog, 0,0, %SRCCopy
ReleaseDC hDialog, hDC_Dialog 'PowerBASIC handles release of hDC_Graphic
'Get Screen PPI
deskDC = GetDC(%HWND_Desktop)
gXppi = GetDeviceCaps(deskDC, %LogPixelsX)
gYppi = GetDeviceCaps(deskDC, %LogPixelsX)
ReleaseDC %HWND_Desktop, deskDC
'Calculate Render Size
XPrint Get Client To wCont, hCont
XPrint Get PPI To pXppi, pYppi
Select Case RenderSize
Case 1 'print natural size
wNew = wImg * pXppi/gXppi
hNew = hImg * pYppi/gYppi
Case 0 'resize to fill paper
'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 Stretch hBMP_Dialog, 0, (0,0)-(wImg-1,hImg-1) To (x,y)-(x+wNew-1,y+hNew-1) 'copy (resized) from memory bitmap to visible image
XPrint Close
End Sub
'gbs_01357
'Date: 05-11-2013
http://www.garybeene.com/sw/gbsnippets.htm