Rightaway, I must mention that I have so far been unable to
obtain a Petzold (or similar) book -- have had the Petzold one
on order for months, but hus far no joy.
The code below is a concoction, or cocktail, or borrowed snippets
from bulletin board postings and the FILES &c.
I have pasted in the relevant bits from the WinAPI help to make
it a blow-by-blow story.
It does not bomb our or hang: it simply does not work. I'm sure
there is a twenty-line piece of code somewhere which will do the
trick -- mine turns out a pristinely clean white page with
nothing on it.
The document to be printed is a ready created dialog (wDlg&),
which I am attempting to get into a compatible bitmap and then
print, but no dice.
Can somebody help, please?
'___________________________________________________________________________________________________________
(This function is from a posting by Jozsef Hegyi in late August)
Function GetPrinterDC () As Long
Local pInfo5() As PRINTER_INFO_5,_
dwNeeded As Long,_
dwReturned As Long,_
iUpper As Long,_
pByte As Byte Ptr,_
szNull As Asciiz*0
pByte = VarPtr(szNull)
Call EnumPrinters (%PRINTER_ENUM_LOCAL, ByVal %NULL, 5, @pByte, 0, dwNeeded, dwReturned)
iUpper = dwNeeded\SizeOf(pInfo5(0))
ReDim pInfo5(0: iUpper)
pByte = VarPtr(pInfo5(0))
If EnumPrinters (%PRINTER_ENUM_DEFAULT, ByVal %NULL, 5, @pByte, _
(iUpper+1)*SizeOf(pInfo5(0)), dwNeeded, dwReturned) Then
Function = CreateDC (ByVal %NULL, pInfo5(0)[email protected], ByVal %NULL, ByVal %NULL)
Else
Function = 0
End If
End Function
'___________________________________________________________________________________________________________
Sub PrintThisScreen
Local pd As PRINTDLGAPI
Local di As DOCINFO
Local dn As Asciiz * 64
Local hWnd As Long
Local hDC As Long
Local hBmp As Long
Local hBmpDC As Long
Local r As RECT
Local x As Long
Local y As Long
Local bmp As Bitmap
Local pDC As Long
' Create a normal DC And a memory DC For the dialog. The
' normal DC provides a "snapshot" of the screen contents. The
' memory DC keeps a copy of this "snapshot" in the associated
' bitmap.
hWnd = wDlg& 'wDlg& contains the visible material to be printed)
GetWindowRect hWnd, r
hdcScreen& = GetDC(hWnd)
'To store an image temporarily, your application must call CreateCompatibleDC to create
'a DC that is compatible with the current window DC.
hdcCompatible& = CreateCompatibleDC(hdcScreen&)
'After you create a compatible DC, you create a bitmap with the appropriate dimensions
'by calling the CreateCompatibleBitmap function and then select it into this device
'context by calling the SelectObject function.
' Create a compatible bitmap For hdcScreen.
hbmScreen& = CreateCompatibleBitmap(hdcScreen&, _
GetDeviceCaps(hdcScreen&, %HORZRES), _
GetDeviceCaps(hdcScreen&, %VERTRES))
If hbmScreen& = 0 Then
MsgBox("hbmScreen& is at zero")
DeleteDC hdcScreen&
Exit Sub
End If
' Select the bitmap into the compatible DC.
Call SelectObject(hdcCompatible&, hbmScreen&)
'After the compatible device context is created and the appropriate bitmap has been
'selected into it, you can capture the image.
'The Win32 API provides the BitBlt function to capture images. This function
'performs a bit block transfer - that is, it copies data from a source bitmap
'into a destination bitmap.
'Because it copies data from bitmaps, you'd expect that two arguments to this
'function would be bitmap handles; however, this is not the case. Instead,
'BitBlt receives handles that identify two device contexts and copies the
'bitmap data from a bitmap selected into the source DC into a bitmap
'selected into the target DC. In this case, the target DC is the compatible
'DC, so when BitBlt completes the transfer, the image has been stored in memory.
' Copy data for the selected display into a
' bitmap that is selected into a compatible DC.
' Call ShowWindow (hwnd, %SW_HIDE)
GetObject hbmScreen&, SizeOf(bmp), bmp
Call BitBlt(hdcCompatible&, _
0,0, _
bmp.bmWidth, bmp.bmHeight, _
hdcScreen&, _
0,0, _
%SRCCOPY)
' pd.lStructSize = SizeOf(pd)
' pd.Flags = %PD_NOSELECTION Or %PD_NOPAGENUMS Or %PD_USEDEVMODECOPIES Or %PD_RETURNDC
' pd.hWndOwner = hWnd '%HWND_DESKTOP
pd.lStructSize = SizeOf(pd)
pd.hDevMode = %Null '(Handle) %Null
pd.hDevNames = %Null '(Handle) %Null
pd.Flags = %PD_RETURNDC
pd.hwndOwner = hwnd
pd.hDC = %Null '(HDC) %Null
pd.nFromPage = 1
pd.nToPage = 1
pd.nMinPage = 0
pd.nMaxPage = 0
pd.nCopies = 1
pd.hInstance = %Null '(Handle) %Null
' pd.lCustData = 0L
' pd.lpfnPrintHook = (LPPRINTHOOKPROC) %Null
' pd.lpfnSetupHook = (LPSETUPHOOKPROC) %Null
' pd.lpPrintTemplateName = %Null '(%LPSTR) %Null
' pd.lpSetupTemplateName = %Null '(%LPSTR) %Null
' pd.hPrintTemplate = %Null '(Handle) %Null
' pd.hSetupTemplate = %Null '(Handle) %Null
' Display the Print Dialog box.
' PrintDlg(pd)
If IsFalse PrintDlg(pd) Then
Exit Sub
End If
' hBmpDC=GetPrinterDC
' pDC = CreateCompatibleDC(hBmpDC)
dn = "Printer Test"
di.cbSize = SizeOf(di)
di.lpszDocName = VarPtr(dn)
If StartDoc(pd.hDC, di) > 0 Then
If StartPage(pd.hDC) <= 0 Then
DeleteDC hDC
DeleteDC hBmpDC
' DeleteDC pd.hDC
DeleteDC hdcScreen&
Exit Sub
End If
hBmpDC=pd.hDC 'GetPrinterDC
pDC = CreateCompatibleDC(hBmpDC)
' x = GetDeviceCaps(pd.hDC, %HORZRES)
' y = CLng((x / r.nRight) * r.nBottom)
'To redisplay the image, call BitBlt a second time, specifying the compatible DC
'as the source DC and a window (or printer) DC as the target DC.
Call BitBlt(pDC, _
0,0, _
bmp.bmWidth, bmp.bmHeight, _
hdcCompatible&, _
0,0, _
%SRCCOPY)
If EndPage(pd.hDC) <= 0 Then
DeleteDC hDC
DeleteDC hBmpDC
DeleteDC pd.hDC
DeleteDC hdcScreen&
Exit Sub
End If
End If
EndDoc pd.hDC
DeleteDC hDC
DeleteDC hBmpDC
DeleteDC pd.hDC
DeleteDC hdcScreen&
End Sub
'___________________________________________________________________________________________________________
------------------
obtain a Petzold (or similar) book -- have had the Petzold one
on order for months, but hus far no joy.
The code below is a concoction, or cocktail, or borrowed snippets
from bulletin board postings and the FILES &c.
I have pasted in the relevant bits from the WinAPI help to make
it a blow-by-blow story.
It does not bomb our or hang: it simply does not work. I'm sure
there is a twenty-line piece of code somewhere which will do the
trick -- mine turns out a pristinely clean white page with
nothing on it.
The document to be printed is a ready created dialog (wDlg&),
which I am attempting to get into a compatible bitmap and then
print, but no dice.
Can somebody help, please?
'___________________________________________________________________________________________________________
(This function is from a posting by Jozsef Hegyi in late August)
Function GetPrinterDC () As Long
Local pInfo5() As PRINTER_INFO_5,_
dwNeeded As Long,_
dwReturned As Long,_
iUpper As Long,_
pByte As Byte Ptr,_
szNull As Asciiz*0
pByte = VarPtr(szNull)
Call EnumPrinters (%PRINTER_ENUM_LOCAL, ByVal %NULL, 5, @pByte, 0, dwNeeded, dwReturned)
iUpper = dwNeeded\SizeOf(pInfo5(0))
ReDim pInfo5(0: iUpper)
pByte = VarPtr(pInfo5(0))
If EnumPrinters (%PRINTER_ENUM_DEFAULT, ByVal %NULL, 5, @pByte, _
(iUpper+1)*SizeOf(pInfo5(0)), dwNeeded, dwReturned) Then
Function = CreateDC (ByVal %NULL, pInfo5(0)[email protected], ByVal %NULL, ByVal %NULL)
Else
Function = 0
End If
End Function
'___________________________________________________________________________________________________________
Sub PrintThisScreen
Local pd As PRINTDLGAPI
Local di As DOCINFO
Local dn As Asciiz * 64
Local hWnd As Long
Local hDC As Long
Local hBmp As Long
Local hBmpDC As Long
Local r As RECT
Local x As Long
Local y As Long
Local bmp As Bitmap
Local pDC As Long
' Create a normal DC And a memory DC For the dialog. The
' normal DC provides a "snapshot" of the screen contents. The
' memory DC keeps a copy of this "snapshot" in the associated
' bitmap.
hWnd = wDlg& 'wDlg& contains the visible material to be printed)
GetWindowRect hWnd, r
hdcScreen& = GetDC(hWnd)
'To store an image temporarily, your application must call CreateCompatibleDC to create
'a DC that is compatible with the current window DC.
hdcCompatible& = CreateCompatibleDC(hdcScreen&)
'After you create a compatible DC, you create a bitmap with the appropriate dimensions
'by calling the CreateCompatibleBitmap function and then select it into this device
'context by calling the SelectObject function.
' Create a compatible bitmap For hdcScreen.
hbmScreen& = CreateCompatibleBitmap(hdcScreen&, _
GetDeviceCaps(hdcScreen&, %HORZRES), _
GetDeviceCaps(hdcScreen&, %VERTRES))
If hbmScreen& = 0 Then
MsgBox("hbmScreen& is at zero")
DeleteDC hdcScreen&
Exit Sub
End If
' Select the bitmap into the compatible DC.
Call SelectObject(hdcCompatible&, hbmScreen&)
'After the compatible device context is created and the appropriate bitmap has been
'selected into it, you can capture the image.
'The Win32 API provides the BitBlt function to capture images. This function
'performs a bit block transfer - that is, it copies data from a source bitmap
'into a destination bitmap.
'Because it copies data from bitmaps, you'd expect that two arguments to this
'function would be bitmap handles; however, this is not the case. Instead,
'BitBlt receives handles that identify two device contexts and copies the
'bitmap data from a bitmap selected into the source DC into a bitmap
'selected into the target DC. In this case, the target DC is the compatible
'DC, so when BitBlt completes the transfer, the image has been stored in memory.
' Copy data for the selected display into a
' bitmap that is selected into a compatible DC.
' Call ShowWindow (hwnd, %SW_HIDE)
GetObject hbmScreen&, SizeOf(bmp), bmp
Call BitBlt(hdcCompatible&, _
0,0, _
bmp.bmWidth, bmp.bmHeight, _
hdcScreen&, _
0,0, _
%SRCCOPY)
' pd.lStructSize = SizeOf(pd)
' pd.Flags = %PD_NOSELECTION Or %PD_NOPAGENUMS Or %PD_USEDEVMODECOPIES Or %PD_RETURNDC
' pd.hWndOwner = hWnd '%HWND_DESKTOP
pd.lStructSize = SizeOf(pd)
pd.hDevMode = %Null '(Handle) %Null
pd.hDevNames = %Null '(Handle) %Null
pd.Flags = %PD_RETURNDC
pd.hwndOwner = hwnd
pd.hDC = %Null '(HDC) %Null
pd.nFromPage = 1
pd.nToPage = 1
pd.nMinPage = 0
pd.nMaxPage = 0
pd.nCopies = 1
pd.hInstance = %Null '(Handle) %Null
' pd.lCustData = 0L
' pd.lpfnPrintHook = (LPPRINTHOOKPROC) %Null
' pd.lpfnSetupHook = (LPSETUPHOOKPROC) %Null
' pd.lpPrintTemplateName = %Null '(%LPSTR) %Null
' pd.lpSetupTemplateName = %Null '(%LPSTR) %Null
' pd.hPrintTemplate = %Null '(Handle) %Null
' pd.hSetupTemplate = %Null '(Handle) %Null
' Display the Print Dialog box.
' PrintDlg(pd)
If IsFalse PrintDlg(pd) Then
Exit Sub
End If
' hBmpDC=GetPrinterDC
' pDC = CreateCompatibleDC(hBmpDC)
dn = "Printer Test"
di.cbSize = SizeOf(di)
di.lpszDocName = VarPtr(dn)
If StartDoc(pd.hDC, di) > 0 Then
If StartPage(pd.hDC) <= 0 Then
DeleteDC hDC
DeleteDC hBmpDC
' DeleteDC pd.hDC
DeleteDC hdcScreen&
Exit Sub
End If
hBmpDC=pd.hDC 'GetPrinterDC
pDC = CreateCompatibleDC(hBmpDC)
' x = GetDeviceCaps(pd.hDC, %HORZRES)
' y = CLng((x / r.nRight) * r.nBottom)
'To redisplay the image, call BitBlt a second time, specifying the compatible DC
'as the source DC and a window (or printer) DC as the target DC.
Call BitBlt(pDC, _
0,0, _
bmp.bmWidth, bmp.bmHeight, _
hdcCompatible&, _
0,0, _
%SRCCOPY)
If EndPage(pd.hDC) <= 0 Then
DeleteDC hDC
DeleteDC hBmpDC
DeleteDC pd.hDC
DeleteDC hdcScreen&
Exit Sub
End If
End If
EndDoc pd.hDC
DeleteDC hDC
DeleteDC hBmpDC
DeleteDC pd.hDC
DeleteDC hdcScreen&
End Sub
'___________________________________________________________________________________________________________
------------------
Comment