You are not logged in. You can browse in the PowerBASIC Community, but you must click Login (top right) before you can post. If this is your first visit, check out the FAQ or Sign Up.
I have loaded an image onto my main window using gdiplus, and have written over the image by drawing to the window's hdc.
Now how can I print all that out showing both the image and the drawing done to the hdc ?
You need to have a copy of the window in a Bitmap to work with (or at least the Bits in a buffer).
The appropriate API to use to draw a Bitmap (actually its bits) to a printer is:
StretchDIBits
Do not use BitBlt or StretchBlt !
Printer drivers do no support all GDI functions.
StretchDIBits is the defacto standard for drawing to printer DC's and most (if not all) printer drivers should support it).
Sorry, I haven't been able to get back to this until now...
I'm saving the window to a bitmap and I have a handle to the bitmap, but is there some kind of shortcut for saving the bitmap to a .bmp file??
Anyone used PrintWindow before?
Apparently it is part of WinXP, but I'd never heard of it before.
Unfortunately, it doesn't seem to work!
The following code should work according to Microsoft documentation:
Code:
dim ztext as asciiz*256
dim lppd as printdlgapi
dim docu as docinfo
lppd.lstructsize=len(printdlgapi)
lppd.flags=%pd_returndc or %pd_printsetup
if printdlg(lppd)=0 then
function=0
exit function
end if
hprinter&=lppd.hdc
ztext="Screenshot"
docu.cbsize=sizeof(docu)
docu.lpszdocname=varptr(ztext)
docu.lpszoutput=%null
startdoc hprinter&,docu
startpage hprinter&
'style&=%prf_children or %prf_client or %prf_owned '_
' 'or %prf_erasebkgnd
'
'sendmessage hwnd&,%wm_print,hprinter&,style&
printwindow hwnd&,hprinter&,%null ''%pw_clientonly
endpage hprinter&
enddoc hprinter&
deletedc hprinter&
Not tested, but sounds like you need to create a bitmap and select it into the DC...
Code:
'------------------------------------------------------------------------------
'
'------------------------------------------------------------------------------
FUNCTION printWindowAPI(BYVAL hWnd AS DWORD) AS LONG
DIM ztext AS ASCIIZ*256
DIM lppd AS printdlgapi
DIM docu AS docinfo
DIM PrinterName AS ASCIIZ * 256
DIM hPrinterDC AS DWORD
LOCAL rc AS RECT
LOCAL hDC AS DWORD
LOCAL hDIB AS DWORD, hOldDIB AS DWORD
DIM bmi AS BITMAPINFO
lppd.lstructsize = LEN(printdlgapi)
lppd.flags = %pd_returndc OR %pd_printsetup
IF printdlg(lppd)=0 THEN
FUNCTION=0
EXIT FUNCTION
END IF
hDC = GetWindowDC(hWnd)
GetWindowRect hWnd, rc
hPrinterDC = lppd.hdc
bmi.bmiHeader.biSize = SIZEOF( bmi.bmiHeader )
bmi.bmiHeader.biWidth = ( rc.nRight - rc.nLeft )
bmi.bmiHeader.biHeight = ( rc.nBottom - rc.nTop )
bmi.bmiHeader.biPlanes = 1
bmi.bmiHeader.biBitCount = 24
bmi.bmiHeader.biCompression = %BI_RGB
hDIB = CreateDIBSection( hDC, bmi, %DIB_RGB_COLORS, 0, 0, 0 )
hOldDIB = SelectObject( hPrinterDC, hDIB )
ztext = "Screenshot"
docu.cbsize = SIZEOF(docu)
docu.lpszdocname = VARPTR(ztext)
docu.lpszoutput = %null
startdoc hPrinterDC,docu
startpage hPrinterDC
lrt& = printwindow(hWnd, hPrinterDC, %NULL) ''%pw_clientonly
endpage hPrinterDC
enddoc hPrinterDC
SelectObject hPrinterDC, hOldDIB
ReleaseDC hWnd, hDC
DeleteDC hPrinterDC
END FUNCTION
For anyone who cares; these are the two functions I ended up with: one to create a screenshot and the other to print out the bitmap file.
Code:
function getwindowscreenshot(byval hwnd&,byval namefile$) as long
dim rc as rect
dim bm as bitmap
dim bmi as bitmapinfo
dim bmfhd as bitmapfileheader
dim bminfhd as bitmapinfoheader
getclientrect hwnd&,rc
hdc&=getdc(hwnd&)
hmemdc&=createcompatibledc(hdc&)
bmi.bmiheader.bisize=sizeof(bmi.bmiheader)
bmi.bmiheader.biwidth=(rc.nright-rc.nleft)
bmi.bmiheader.biheight=(rc.nbottom-rc.ntop)
bmi.bmiheader.biplanes=1
bmi.bmiheader.bibitcount=24
bmi.bmiheader.bicompression=%bi_rgb
hmembmp&=createdibsection(hmemdc&,bmi,%dib_rgb_colors,0,0,0)
globallock hmembmp&
selectobject hmemdc&,hmembmp&
getobject hmembmp&,sizeof(bm),bm
bitblt hmemdc&,0,0,bm.bmwidth,bm.bmheight,hdc&,rc.nleft,rc.ntop,%srccopy
bmfhd.bftype=cvi("BM")
bmfhd.bfsize=len(bmfhd)+len(bminfhd) _
+(bm.bmwidthbytes*bm.bmheight)
bmfhd.bfoffbits=54
bminfhd.bisize=40
bminfhd.biwidth=bm.bmwidth
bminfhd.biheight=bm.bmheight
bminfhd.biplanes=1
bminfhd.bibitcount=24
bminfhd.bisizeimage=bm.bmwidthbytes*bm.bmheight
ff&=freefile
open namefile$ for output as #ff&
print #ff&,bmfhd;
print #ff&,bminfhd;
print #ff&,peek$(bm.bmbits,bm.bmwidthbytes*bm.bmheight);
close #ff&
releasedc hwnd&,hdc&
deletedc hmemdc&
globalunlock hmembmp&
deleteobject hmembmp&
function=1
end function
function printbmp(byval hwnd&,byval bmpfilename$) as long
title$="Print Job"
xprint attach choose,title$
orient&=2 ''landscape
xprint set orientation orient&
xprint get size to prwidth&,prheight&
ff&=freefile
open bmpfilename$ for binary as #ff& base=0
seek #ff&,18
get$ #ff&,8,b$
close #ff&
bmwidth&=cvl(left$(b$,4))
bmheight&=cvl(right$(b$,4))
graphic bitmap load bmpfilename$,nwidth&,nheight& to hbmp&
x&=prwidth&-10
y&=bmheight&*(x&/bmwidth&)
xprint stretch hbmp&,0, _
(0,0)-(bmwidth&,bmheight&) to (5,5)-(x&,y&), _
%mix_copysrc
xprint close
graphic bitmap end
function=1
end function
I think printWindow() is just a snapshot of the window, it doesn't render. So I believe you still need to render it on the Printer DC. Not sure if a DIB is necessary, or could use a standard bitmap.
Could you test this? I'm without a printer... Thx.
Code:
'------------------------------------------------------------------------------
'
'------------------------------------------------------------------------------
FUNCTION printWindowAPI(BYVAL hWnd AS DWORD) AS LONG
DIM ztext AS ASCIIZ*256
DIM lppd AS printdlgapi
DIM docu AS docinfo
DIM PrinterName AS ASCIIZ * 256
DIM hPrinterDC AS DWORD
LOCAL rc AS RECT, nWidth AS LONG, nHeight AS LONG
LOCAL hDC AS DWORD
LOCAL hDIB AS DWORD, hOldDIB AS DWORD
DIM bmi AS BITMAPINFO
lppd.lstructsize = LEN(printdlgapi)
lppd.flags = %pd_returndc OR %pd_printsetup
IF printdlg(lppd)=0 THEN
FUNCTION=0
EXIT FUNCTION
END IF
hDC = GetWindowDC(hWnd)
GetWindowRect hWnd, rc
nWidth = ( rc.nRight - rc.nLeft )
nHeight = ( rc.nBottom - rc.nTop )
bmi.bmiHeader.biSize = SIZEOF( bmi.bmiHeader )
bmi.bmiHeader.biWidth = nWidth
bmi.bmiHeader.biHeight = nHeight
bmi.bmiHeader.biPlanes = 1
bmi.bmiHeader.biBitCount = 24
bmi.bmiHeader.biCompression = %BI_RGB
hDIB = CreateDIBSection( hDC, bmi, %DIB_RGB_COLORS, 0, 0, 0 )
hOldDIB = SelectObject( hDC, hDIB )
lrt& = printwindow(hWnd, hDC, %NULL) ''%pw_clientonly
hPrinterDC = lppd.hdc
ztext = "Screenshot"
docu.cbsize = SIZEOF(docu)
docu.lpszdocname = VARPTR(ztext)
docu.lpszoutput = %null
startdoc hPrinterDC,docu
startpage hPrinterDC
bitblt hPrinterDC,0,0,nWidth,nHeight,hDC,0,0,%srccopy
endpage hPrinterDC
enddoc hPrinterDC
SelectObject hPrinterDC, hOldDIB
ReleaseDC hWnd, hDC
DeleteDC hPrinterDC
END FUNCTION
We process personal data about users of our site, through the use of cookies and other technologies, to deliver our services, and to analyze site activity. For additional details, refer to our Privacy Policy.
By clicking "I AGREE" below, you agree to our Privacy Policy and our personal data processing and cookie practices as described therein. You also acknowledge that this forum may be hosted outside your country and you consent to the collection, storage, and processing of your data in the country where this forum is hosted.
Comment