Link to home
Start Free TrialLog in
Avatar of fbt
fbt

asked on

How to print a dialog widow

How can i print a dialog window (created with CDialog MFC class) using PrintClient function, or WM_PRINTCLIENT message, or anything else ...

Thanks
ASKER CERTIFIED SOLUTION
Avatar of galkin
galkin

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 fbt
fbt

ASKER

Thank you galkin for your answer.

I tried to print my dialog with your suggestion, by my page is
still empty.
Can you tell me where i am wrong ???

Thanks.

(CResult is a CDialog class)

void CResult::OnPrint()
{
HDC    hdcPrn ;

    // Instantiate a CPrintDialog.
    CPrintDialog *printDlg =
        new CPrintDialog(FALSE, PD_ALLPAGES | PD_RETURNDC, NULL);


    // Display Windows print dialog box.
    // Initialize some of the fields in PRINTDLG structure.
    printDlg->m_pd.nMinPage = printDlg->m_pd.nMaxPage = 1;
    printDlg->m_pd.nFromPage = printDlg->m_pd.nToPage = 1;

    printDlg->DoModal();
    // Obtain a handle to the device context.
    hdcPrn = printDlg->GetPrinterDC();

    if (hdcPrn != NULL)
    {
        CDC *pDC = new CDC;
        pDC->Attach (hdcPrn);      // attach a printer DC

      DOCINFO di;
      ::ZeroMemory (&di, sizeof (DOCINFO));
      di.cbSize = sizeof (DOCINFO);
      di.lpszDocName = _T("WNObnsk");

      pDC->StartDoc(&di); // Begin a new print job

      short cxPage = ::GetDeviceCaps (hdcPrn, HORZRES);
      short cyPage = ::GetDeviceCaps (hdcPrn, VERTRES);

      BOOL b = ::StretchBlt (hdcPrn, 0, 0, cxPage, cyPage,
            pDC->GetSafeHdc(), 0, 0, 1000, 1000, MERGEPAINT);
      AfxTrace (_T("StretchBlt : %d\n"), b);

        pDC->StartPage();          // begin a new page
      SendMessage (WM_PRINT, (WPARAM) pDC, PRF_CLIENT|PRF_NONCLIENT|PRF_ERASEBKGND|PRF_CHILDREN);
        pDC->EndPage();            // end a page

        pDC->EndDoc();             // end a print job

        pDC->Detach();             // detach the printer DC
        delete pDC;
    }

    delete printDlg;
}

WPARAM should be HDC not CDC*.
SendMessage (WM_PRINT, (WPARAM) pDC->GetSafeHdc(), PRF_CLIENT|PRF_NONCLIENT|PRF_ERASEBKGND|PRF_CHILDREN);

Avatar of fbt

ASKER

My dialog is now printing on my page, but it's still tiny.

I tried many differents values in StretchBlt, but the dialog
on the page is like a stamp !!

Can you explain me what is the problem ???

Thanks
I already mentioned you must adjust scaling factor.
Use GetDeviceCaps(LOGPIXELX) and GetDeviceCaps(LOGPIXELY) passing iHDC of screen and HDC of the printer.
Avatar of fbt

ASKER

I'm sorry about my basic questions, but even with
GetDeviceCaps (LOGPIXELSX) and GetDeviceCaps (LOGPIXELSY),
the dialog is still tiny on my print page (I watched the
values of the datas on debug and cxPage = cyPage =
cxDC = cyDC = 600, and StretchBlt status is TRUE).

This is my code (if you have enough time to read it,
or a full example, to save your time - Thanks).



void CResult::OnPrint()
{
HDC    hdcPrn ;

    // Instantiate a CPrintDialog.
    CPrintDialog *printDlg =
        new CPrintDialog(FALSE, PD_ALLPAGES | PD_RETURNDC, NULL);


    // Display Windows print dialog box.
    // Initialize some of the fields in PRINTDLG structure.
    printDlg->m_pd.nMinPage = printDlg->m_pd.nMaxPage = 1;
    printDlg->m_pd.nFromPage = printDlg->m_pd.nToPage = 1;

    printDlg->DoModal();
    // Obtain a handle to the device context.
    hdcPrn = printDlg->GetPrinterDC();

    if (hdcPrn != NULL)
    {
        CDC *pDC = new CDC;
        pDC->Attach (hdcPrn);      // attach a printer DC

      DOCINFO di;
      ::ZeroMemory (&di, sizeof (DOCINFO));
      di.cbSize = sizeof (DOCINFO);
      di.lpszDocName = _T("WNObnsk");

      pDC->StartDoc(&di); // Begin a new print job

      int cxPage = ::GetDeviceCaps (hdcPrn, LOGPIXELSX);
      int cyPage = ::GetDeviceCaps (hdcPrn, LOGPIXELSY);

      int cxDC = pDC->GetDeviceCaps (LOGPIXELSX);
      int cyDC = pDC->GetDeviceCaps (LOGPIXELSY);

      BOOL b = ::StretchBlt (hdcPrn, 0, 0, cxPage, cyPage,
            pDC->GetSafeHdc(), 0, 0, cxDC, cyDC, MERGEPAINT);
      AfxTrace (_T("StretchBlt : %d\n"), b);

        pDC->StartPage();          // begin a new page
      SendMessage (WM_PRINT, (WPARAM) pDC->GetSafeHdc(), PRF_CLIENT|PRF_NONCLIENT|PRF_ERASEBKGND|PRF_CHILDREN);
        pDC->EndPage();            // end a page
        pDC->EndDoc();             // end a print job
        pDC->Detach();             // detach the printer DC
        delete pDC;
    }

    delete printDlg;
}