Solved

Adding a print option for a dialog box.

Posted on 1998-01-08
11
331 Views
Last Modified: 2013-11-20
I have a dialog box with  tree ctrl, List ctrl, List box on it. I want to print the whole dialog box as it is. How do i do that?
0
Comment
Question by:gurushree
  • 5
  • 3
  • 3
11 Comments
 
LVL 23

Expert Comment

by:chensu
ID: 1313649
Use CWnd::Print() function. Since CDialog is derived from CWnd, it is also a member function of CDialog.

void CMyDialog::OnPrintCommand()
{
    this->Print(pPrintDC, PRF_NONCLIENT | PRF_CHILDREN | PRF_CLIENT);  // you may need to change the flags.
}


0
 

Author Comment

by:gurushree
ID: 1313650
Hi,

I tried the solution you had given. BUt the problem is getting the printer DC to pass in Print() function. I tried getting the peinter attributes by calling , CWinApp::GetPrinterDeviceDefaults(). This function returns  a PRINTDLG structure from ehich one can get the HDC value. I passed this HDC value to Print() function, still it did not work. So can you suggest  a solution to this so that I can get complete solution for my question.

Thanks ,
Gurushree
0
 
LVL 23

Expert Comment

by:chensu
ID: 1313651
You can get the printer dc by using the following code.

CDC PrnDC;
PRINTDLG PrintDlg;
BOOL bSucc = ::AfxGetApp()->GetPrinterDeviceDefaults(&PrintDlg) && ::AfxGetApp()->CreatePrinterDC(PrnDC);
if (bSucc)
    ....
else
    ....

0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:gurushree
ID: 1313652
Hi,

Again I am having problem with printing the dialog box contents. I tried different combinations . In my dialog box I am having a print button. On clicking the print button , I am doing the following,

CDC PrintDC ;
PRINTDLG PrintDlg ;
BOOL bRes = AfxGetApp()->GetPrinterDeviceDefaults(&PrintDlg) && ::AfxGetApp()->CreatePrinterDC(PrintDC) ;
      if ( bRes )
      {
            this->Print(&PrintDC, PRF_CHECKVISIBLE) ;
            this->Print(&PrintDC, PRF_CLIENT) ;
            this->Print(&PrintDC, PRF_NONCLIENT) ;
            this->Print(&PrintDC, PRF_CHILDREN) ;
            this->Print(&PrintDC, PRF_CLIENT) ;
            this->Print(&PrintDC, PRF_ERASEBKGND) ;
      }
      ReleaseDC(&PrintDC) ;

bRes is TRUE, still the print function does not work. Can u correct me where i am wrong.

Thanks,
Gurushree

0
 
LVL 23

Expert Comment

by:chensu
ID: 1313653
First, PrintDC is an object, the DC will be released when the destructor is executed. So, no need to ReleaseDC(&PrintDC).

Second, you should figure out which part does not work. For example, use TextOut to test whether it prints out anything. Or, use CPrintDialog to get the printer DC.

Third, I don't think keeping Print wiht different flags is a good way to test it. Some flags need to be combined.
0
 
LVL 7

Accepted Solution

by:
galkin earned 20 total points
ID: 1313654
If you have class derived from CView you can take advantage from ID_FILE_PRINT_DIRECT. CView meessage handler creates printer DC without display print dialog and then calls CView::OnPrint. This is virtual function so you must override it. You must sore pointer to your dialog in view class and place code in OnPrint.
CYourview::OnPrint( CDC* pDC, CPrintInfo* pInfo )
{
pDialog->SendMessage(WM_PRINT, (WPARAM)pDC->GetSafeHdc(), PRF_NONCLIENT | PRF_CHILDREN | PRF_CLIENT | PRF_ERASEBKGND);
}
0
 

Author Comment

by:gurushree
ID: 1313655
Hi,

Again the problem is getting the printer DC. I have a button in dialog box called "Print", so when i click on that button , it should print the dialog box along with the controls. In the handler of Print button, i am having the following code :
{
      
                         CPrintDialog dlg(FALSE);
      dlg.GetDefaults();
      SendMessage(WM_PRINT, (WPARAM)(dlg.m_pd.hDC), PRF_NONCLIENT | PRF_CHILDREN | PRF_CLIENT );       
}


If i pass the display device context , by calling GetDC(), Clicking on print  button, prints the dialog on display. But nothing happens if i pass printer dc as shown above. I also tried displaying CPrintDialog and calling  GetPrinterDC(),  Hope i have made my problem clear.

Thanks,
Gurushree

0
 
LVL 7

Expert Comment

by:galkin
ID: 1313656
Did you read my answer? I wrote use ID_FILE_PRINT_DIRECT message to print directly without displaying print dialog. CView class has this message handler which creates printer DC and the calls OnPrint. If you have dialog based application without view you can borrow code from this message handler how to create printer DC. pDC passed to OnPrint is either screen DC in the case of print preview or printer DC if actual printing is being processed.
0
 

Author Comment

by:gurushree
ID: 1313657
Hi,
I have a an SDI application and view class derived from CView. In one of the menu handler of the application, i am creating this dialog box, which has print button.  so, in print button handler, do i have to send ID_FILE_PRINT_DIRECT, if so i tried the follwing in Print button handler:
{
SendMessage(ID_FILE_PRINT_DIRECT);
}
and OnPrint has the code which u suggested. but still no luck for me. Please correct me where i am wrong.

Thanks,
Gurushree


0
 
LVL 7

Expert Comment

by:galkin
ID: 1313658
You must send command message with wParam == ID_FILE_PRINT_DIRECT.


From CView deribed class menu handler:
CYourView::OnFilePrint()
{
SendMessage(WM_COMMAND, WPARAM(ID_FILE_PRINT_DIRECT));
}

or dialog button handler

CYourDialog::OnClickPrintBtn
{
GetParent()->SendMessage(WM_COMMAND, WPARAM(ID_FILE_PRINT_DIRECT));
}
0
 

Author Comment

by:gurushree
ID: 1313659
Hi,

Thanks for your answer. but i have one more problem, the view class which is owning the dialog, has some document displayed in it. When i print the dialog, the view contents are also getting printed. Also, the size of dialog is very muvh reduced. i guess i need to set up some mapping mode. since i am new to these device context, printer functions, can u guide me?

Thanks again,
Gurushree

0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ffmpeg - "rtsp://...... Operation not permitted" 4 80
countEvens challenge 2 99
Annoying "thing" blocks my view 4 99
x-bar in Google Sheets 2 52
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

776 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