Link to home
Start Free TrialLog in
Avatar of UandI
UandI

asked on

Print screen of active dialog by pressing a button

I want to print a dialog by pressing a button on this dialog called 'print dialog'. Is there a simple way to do this?
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of nietod
nietod

I just realizes I'm assuming this is for windows, is it?   (I thought this was the windows topic area, but it is the C++ topic area.)

The WM_PRINT messages is used to cause the window to paint its contents to a DC that you specify rather than to the screen.  So to use this you will need to obtain a DC for the printer (Probably using PrintDlg().) then you send a WM_PRINT message to the dialog window using the printer DC .  The window will then draw to the DC, and therefore to the printer.

If you want the window's contents to appear, but not its frame (title bar), then use the WM_PRINTCLIENT instead.  

Note that this approach does not always work perfectly.  Some windows will not responds to this message.  The dialog window will, but it is possible that some of the dialog window's child windows (controls) might not, if that is the case, then they won't appear in the printout.  IOf that happens (its not too likely) let me know .

Let me know if you have any questions.
Avatar of UandI

ASKER

Thank you for your help so far. I just tried what you told me but somehow there's still no printing.
How exactly do I have to end a WM_PRINT message to the dialog window using the printer DC? Do you have a code fragment?
Do I need an OnPrint() method or is this all available. All I want is just to print the active dialog (similar to the printscreen button, but direct to the printer.
I appreciate your help very much!
You would just need to do

SendMessage(hDialog,WM_PRINT,hDC,PRF_CLIENT | PRF_NONCLIENT | PRF_CHILDREN);

You will need to get the handle to the dialog (hDialog) and the DC (hDC).
Avatar of UandI

ASKER

I don't have a method SendMessage that takes 4 parameters. I'd be happy to find some more details.
>> I don't have a method SendMessage
That's a windows API function.
Avatar of UandI

ASKER

I'm now using the API function SendMessage. Unfortunately I have to type cast the hDC parameter with (WPARAM). It seems that something is going wrong with this type cast, since there still is no output beeing sent to the printer.
Any suggestions?
Not with that little bit of info.  How are you getting the DC, are you calling StartDoc(), StartPage(), EndPage(), EndDoc()?  Do you release the DC?