Solved

How to print a dialog widow

Posted on 1998-06-23
6
247 Views
Last Modified: 2013-11-20
How can i print a dialog window (created with CDialog MFC class) using PrintClient function, or WM_PRINTCLIENT message, or anything else ...

Thanks
0
Comment
Question by:fbt
  • 3
  • 3
6 Comments
 
LVL 7

Accepted Solution

by:
galkin earned 100 total points
ID: 1318329
Use WM_PRINT with PRF_CLIENT|PRF_NONCLIENT|PRF_ERASEBKGND|PRF_CHILDREN flag.
Take into account that you must perform appropriate scaling and offset because of difference in printer resolution. i suggest you create memory DC, sending WM_PRINT message passing memory DC as WPARAM and then use StretchBlt to stretch memory DC on printer DC.
0
 

Author Comment

by:fbt
ID: 1318330
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;
}

0
 
LVL 7

Expert Comment

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

0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 

Author Comment

by:fbt
ID: 1318332
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
0
 
LVL 7

Expert Comment

by:galkin
ID: 1318333
I already mentioned you must adjust scaling factor.
Use GetDeviceCaps(LOGPIXELX) and GetDeviceCaps(LOGPIXELY) passing iHDC of screen and HDC of the printer.
0
 

Author Comment

by:fbt
ID: 1318334
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;
}

0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question