From: ybc on
Hi Joe,

The following is my code:
CPrintDialog prtDlg(false, PD_ALLPAGES);
HDC hdc = prtDlg.CreatePrinterDC();
CDC dc;
dc.Attach (hdc);
LPDOCINFO docInfo;
int rtn = dc.StartDoc(docInfo);
There's an error:
Program:myapp.exe
File:afxwin1.inl
Debug points to line in afxwin1.inl:
_AFXWIN_INLINE int CDC::StartDoc(LPDOCINFO lpDocInfo)
{ ASSERT(m_hDC != NULL); return ::StartDoc(m_hDC, lpDocInfo); }
I wonder docInfo which points to aDOCINFO structure should initialize
lpszDocName. But I'm not print document from a file, but the formated data
stored in CStringArray. What can I do?
Do I need to access CPrintInfo Class Members while printing and how?
Thanks,
ybc


"Joseph M. Newcomer" wrote:

> You can directly invoke the Print Dialog using the flags to return the printer DC for the
> default printer (no UI pops up); this is one of the most expedient ways of getting the
> printer DC. Just give it a parent window handle of NULL. After that, it's just
> programming. Use a console app, enable MFC in it if you want to use CString (for VS.NET,
> I've not looked into what is involved to get just the CString components; maybe someone
> can post an answer that avoids bringing the whole MFC runtime in...)
> joe
>

From: Joseph M. Newcomer on
Presumably that degree of robustness is left as an Exercise For The Reader. The problem
is that the proper response in this situation is application-specific, so the ASSERT is a
placeholder for the application-appropriate error handling. This is a common pedagogical
technique for example code.
joe

On Mon, 31 Jul 2006 01:56:58 GMT, Geoff <geoff(a)invalid.invalid> wrote:

>On Sun, 30 Jul 2006 20:43:16 -0400, Joseph M. Newcomer <newcomer(a)flounder.com>
>wrote:
>
>>Example
>>
>>CPrintDialog dlg(FALSE);
>>if (dlg.DoModal() == IDOK)
>>{
>> // Get a handle to the printer device context (DC).
>> HDC hdc = dlg.GetPrinterDC();
>> ASSERT(hdc);
>>
>> // Do something with the HDC...
>>
>> // Clean up.
>> CDC::FromHandle(hdc)->DeleteDC();
>>}
>
>
>... and what happens if GetPrinterDC() fails in production (non-debug) code?
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm