Solved

How to print a dialog widow

Posted on 1998-06-23
6
249 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sum13 challenge 24 148
java ^ examples 8 75
Problem to open Excel file 15 261
x-bar in Google Sheets 2 126
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.

734 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