Solved

Printing Dialog Boxes

Posted on 1998-12-22
22
345 Views
Last Modified: 2013-11-20
How can I print an active dialog box. I have a Visual C++ 5.0 MFC application with dialog boxes that have a CButton called print in them.  When the user presses the button I want the dialog box to be printed.  Code please.

HURRY!!!!!
0
Comment
Question by:scs1
  • 15
  • 7
22 Comments
 
LVL 23

Accepted Solution

by:
chensu earned 100 total points
ID: 1326601
Call the CWnd::Print function on the dialog box. You can get the default printer DC by using the following code if you don't want users to choose a printer. Remember to delete the DC by calling DeleteDC after using.

// Return a HDC for the default printer.
   HDC GetPrinterDC(void)
   {
      PRINTDLG pdlg;

      memset( &pdlg, 0, sizeof( PRINTDLG ) );
      pdlg.lStructSize = sizeof( PRINTDLG );
      pdlg.Flags = PD_RETURNDEFAULT | PD_RETURNDC;
      PrintDlg( &pdlg );
      return pdlg.hDC;
   }


0
 

Author Comment

by:scs1
ID: 1326602
I tried the CWnd::Print function but it I could not get it to work.  The print button in the dialog boxes is message-mapped to a function in the dialog box's .cpp file.  Will that cause a problem since it the function is not in the CView file?

Can you give me an example of how to use the Print function.  I want to use the Windows File Print dialog box, too.
0
 

Author Comment

by:scs1
ID: 1326603
I tried the CWnd::Print function but it I could not get it to work.  The print button in the dialog boxes is message-mapped to a function in the dialog box's .cpp file.  Will that cause a problem since it the function is not in the CView file?

Can you give me an example of how to use the Print function.  I want to use the Windows File Print dialog box, too.
0
 

Author Comment

by:scs1
ID: 1326604
I tried the CWnd::Print function but it I could not get it to work.  The print button in the dialog boxes is message-mapped to a function in the dialog box's .cpp file.  Will that cause a problem since it the function is not in the CView file?

Can you give me an example of how to use the Print function.  I want to use the Windows File Print dialog box, too.
0
 

Author Comment

by:scs1
ID: 1326605
I tried the CWnd::Print function but it I could not get it to work.  The print button in the dialog boxes is message-mapped to a function in the dialog box's .cpp file.  Will that cause a problem since it the function is not in the CView file?

Can you give me an example of how to use the Print function.  I want to use the Windows File Print dialog box, too.
0
 
LVL 23

Expert Comment

by:chensu
ID: 1326606
Use CPrintDialog class to display the Print dialog box and get the printer DC. Pass the DC to Print function. In your dialog box member function, simply call this->Print().
0
 

Author Comment

by:scs1
ID: 1326607
I tried the CWnd::Print function but it I could not get it to work.  The print button in the dialog boxes is message-mapped to a function in the dialog box's .cpp file.  Will that cause a problem since it the function is not in the CView file?

Can you give me an example of how to use the Print function.  I want to use the Windows File Print dialog box, too.
0
 

Author Comment

by:scs1
ID: 1326608
I need some more info on how to use the print function (specific syntax).  I do not have any help files, so I am having a lot of trouble.  Also, please show me how to get the printer dc.
0
 

Author Comment

by:scs1
ID: 1326609
I need some more info on how to use the print function (specific syntax).  I do not have any help files, so I am having a lot of trouble.  Also, please show me how to get the printer dc.
0
 

Author Comment

by:scs1
ID: 1326610
I need some more info on how to use the print function (specific syntax).  I do not have any help files, so I am having a lot of trouble.  Also, please show me how to get the printer dc.
0
 

Author Comment

by:scs1
ID: 1326611
I need some more info on how to use the print function (specific syntax).  I do not have any help files, so I am having a lot of trouble.  Also, please show me how to get the printer dc.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 23

Expert Comment

by:chensu
ID: 1326612
CWnd::Print
void Print( CDC* pDC, DWORD dwFlags ) const;

Parameters

pDC

A pointer to a device context.

dwFlags

Specifies the drawing options. This parameter can be one or more of these flags:

PRF_CHECKVISIBLE   Draw the window only if it is visible.

PRF_CHILDREN   Draw all visible children windows.

PRF_CLIENT   Draw the client area of the window.

PRF_ERASEBKGND   Erase the background before drawing the window.

PRF_NONCLIENT   Draw the nonclient area of the window.

PRF_OWNED   Draw all owned windows.

Remarks

Call this member function to draw the current window in the specified device context, which is most commonly in a printer device context.

CWnd::DefWindowProc function processes this message based on which drawing option is specified:

If PRF_CHECKVISIBLE is specified and the window is not visible, do nothing.

If PRF_NONCLIENT is specified, draw the nonclient area in the given device context.

If PRF_ERASEBKGND is specified, send the window a WM_ERASEBKGND message.

If PRF_PRINTCLIENT is specified, send the window a WM_PRINTCLIENT message.

If PRF_PRINTCHILDREN is set, send each visible child window a WM_PRINT message.

If PRF_OWNED is set, send each visible owned window a WM_PRINT message.


CPrintDialog::CPrintDialog
CPrintDialog( BOOL bPrintSetupOnly, DWORD dwFlags = PD_ALLPAGES | PD_USEDEVMODECOPIES | PD_NOPAGENUMS | PD_HIDEPRINTTOFILE | PD_NOSELECTION, CWnd* pParentWnd = NULL );

Parameters

bPrintSetupOnly

Specifies whether the standard Windows Print dialog box or Print Setup dialog box is displayed. Set this parameter to TRUE to display the standard Windows Print Setup dialog box. Set it to FALSE to display the Windows Print dialog box. If bPrintSetupOnly is FALSE, a Print Setup option button is still displayed in the Print dialog box.

dwFlags

One or more flags you can use to customize the settings of the dialog box, combined using the bitwise OR operator. For example, the PD_ALLPAGES flag sets the default print range to all pages of the document. See the PRINTDLG structure in the Win32 SDK documentation for more information on these flags.

pParentWnd

A pointer to the dialog box’s parent or owner window.

Remarks

Constructs either a Windows Print or Print Setup dialog object. This member function only constructs the object. Use the DoModal member function to display the dialog box.

Note that when you call the constructor with bPrintSetupOnly set to FALSE, the PD_RETURNDC flag is automatically used. After calling DoModal, GetDefaults, or GetPrinterDC, a printer DC will be returned in m_pd.hDC. This DC must be freed by the caller of CPrintDialog.


For example, in one of the member function of the dialog box,

CPrintDialog PrnDlg(FALSE, PD_ALLPAGES | PD_USEDEVMODECOPIES | PD_NOPAGENUMS | PD_HIDEPRINTTOFILE | PD_NOSELECTION, this);
if (PrnDlg.DoModal() == IDOK)
{
    HDC hdc = PrnDlg.GetPrinterDC();
    if (hdc != NULL)
    {
        this->Print(CDC::FromHandle(hdc), PRF_CHILDREN | PRF_CLIENT | PRF_NONCLIENT);
        ::DeleteDC(hdc);
    }
}

0
 

Author Comment

by:scs1
ID: 1326613
Sorry I have taken so long to get back to you.

I am using the following code in the function for my dialog box that is message mapped to the print button I made in the dialog box.  The function is located in the dialog box's cpp file.

CPrintDialog PrnDlg(FALSE, PD_ALLPAGES | PD_USEDEVMODECOPIES |                                 PD_NOPAGENUMS | PD_HIDEPRINTTOFILE | PD_NOSELECTION, this);
if (PrnDlg.DoModal() == IDOK)
{
   HDC hdc = PrnDlg.GetPrinterDC();
   if (hdc != NULL)
   {
  this->Print(CDC::FromHandle(hdc), PRF_CHILDREN | PRF_CLIENT |PRF_NONCLIENT);
      ::DeleteDC(hdc);
    }
 }



When the function is called the dialog box for the printers is displayed.  I select one of the network printers and click OK on the printer dialog box.  However, the hdc variable is set to NULL.  Can you tell me why the printer is not being selected?


0
 
LVL 23

Expert Comment

by:chensu
ID: 1326614
Sorry, I missed out the PD_RETURNDC flag.

CPrintDialog PrnDlg(FALSE, PD_ALLPAGES | PD_USEDEVMODECOPIES | PD_NOPAGENUMS | PD_HIDEPRINTTOFILE | PD_NOSELECTION | PD_RETURNDC, this);
0
 

Author Comment

by:scs1
ID: 1326615
I changed it to what you said and it still remained NULL
0
 
LVL 23

Expert Comment

by:chensu
ID: 1326616
Try selecting other printers.
0
 

Author Comment

by:scs1
ID: 1326617
I tried selecting other printers and that does make hdc not NULL.  The function executes as if it works, but nothing is actually printed.  this was done usig network printers.

I tried the application on a computer with a direct connection to a printer and it locks up the program.
0
 

Author Comment

by:scs1
ID: 1326618
When debugging the code, the following is given for hdc variable just after performing the Print function but before deleting the hdc:

CXX0030: Error: expression cannot be evaluated

Does this mean that the printer is not being selected properly?  

the this variable contains the dialog box when I debug, so I don't think that is the problem.  For some reason it just won't send it to the printer.
0
 
LVL 23

Expert Comment

by:chensu
ID: 1326619
Is hdc the same as PrnDlg.m_pd.hDC?
0
 

Author Comment

by:scs1
ID: 1326620
When debugging the code, the following is given for hdc variable just after performing the Print function but before deleting the hdc:

CXX0030: Error: expression cannot be evaluated

Does this mean that the printer is not being selected properly?  

the this variable contains the dialog box when I debug, so I don't think that is the problem.  For some reason it just won't send it to the printer.
0
 

Author Comment

by:scs1
ID: 1326621
Is hdc the same as PrnDlg.m_pd.hDC?

I don't know because I am not familiar with this.
0
 
LVL 23

Expert Comment

by:chensu
ID: 1326622
Debug it and check their value, I mean.
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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.

762 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

21 Experts available now in Live!

Get 1:1 Help Now