Solved

How to print a dialog widow

Posted on 1998-06-23
6
232 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
Comment Utility
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
Comment Utility
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
Comment Utility
WPARAM should be HDC not CDC*.
SendMessage (WM_PRINT, (WPARAM) pDC->GetSafeHdc(), PRF_CLIENT|PRF_NONCLIENT|PRF_ERASEBKGND|PRF_CHILDREN);

0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:fbt
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
has77  challenge 9 67
Use of condition with 'serial' in ansible 2 36
changeXy challenge 13 56
mapAB Challlenge 35 84
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now