-
September 17th, 2021, 03:22 AM
#1
Printing in Windows
Hi
I am trying to learn how to print the content of a window. By now I've ended up with the following code (it should just draw a line as a test print). When executing it, the printer starts working but gives out an empty page. If I choose deskPDF Creator instead of a printer, a pdf file is created, however when trying to open it the pdf reader reports an "internal error".
What am I missing here?
Code:
PRINTDLG pd;
DOCINFO di = {0};
HPEN hpen;
.......
di.cbSize = sizeof(DOCINFO);
di.lpszDocName = "Printout";
hpen = CreatePen(PS_SOLID, 2, RGB(256,0,0));
// Initialize PRINTDLG
ZeroMemory(&pd, sizeof(pd));
pd.lStructSize = sizeof(pd);
pd.hwndOwner = hwnd;
pd.hDevMode = NULL; // Don't forget to free or store hDevMode.
pd.hDevNames = NULL; // Don't forget to free or store hDevNames.
pd.Flags = PD_USEDEVMODECOPIESANDCOLLATE | PD_RETURNDC;
pd.nCopies = 1;
pd.nFromPage = 1;
pd.nToPage = 1;
pd.nMinPage = 1;
pd.nMaxPage = 1;
PrintDlg(&pd);
SelectObject(pd.hDC, hpen);
StartDoc(pd.hDC, &di);
StartPage(pd.hDC);
MoveToEx(pd.hDC, 10,100,NULL);
LineTo(pd.hDC,20,200);
EndPage(pd.hDC);
EndDoc(pd.hDC);
DeleteObject(hpen);
-
September 20th, 2021, 02:11 AM
#2
Re: Printing in Windows
Are you sure the points (10, 100) and (20, 200) are within the printing area?
Victor Nijegorodov
-
September 20th, 2021, 02:25 AM
#3
Re: Printing in Windows
I guess so, all printers have much higher resolution so I would expect this line to be very short and within the printed area.
I also tried copying the whole window into a virtual bitmap and then copying that bitmap onto the printer's DC using BitBlt. The result is the same. If it was a problem of resolution mismatch I would expect at least a part of the window to be printed and there would not be "internal error" in the created pdf file.
-
September 20th, 2021, 02:57 AM
#4
Re: Printing in Windows
Did you debug your code?
What do all these functions:
PrintDlg
StartDoc
StartPage
EndPage
EndDoc
return?
Victor Nijegorodov
-
September 20th, 2021, 03:19 AM
#5
Re: Printing in Windows
I've done it now, they all return TRUE.
It looks like all these function do exactly what is expected - the correct printer gets selected and it does print a page (an empty one though). So it looks like the problem is with drawing to the printer's DC
-
September 20th, 2021, 03:42 AM
#6
Re: Printing in Windows
Then try to use OpenPrinter function rather than the PrintDlg
Victor Nijegorodov
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|