Solved

Print in a dialog box of Visual C++

Posted on 1998-04-22
8
477 Views
Last Modified: 2008-02-01
I am developing in Visual C++ 5.0 with MFC.
My application is dialog based and composed of static text and entry field that I fill (CEdit). How can I print the content of this dialog, that's the static text and the CEdit ? Is it possible ? There is an alternative method (without changing the application obviously) ?
Thanks  in advance.
Nino
0
Comment
Question by:grimaldi
  • 5
  • 2
8 Comments
 
LVL 10

Accepted Solution

by:
RONSLOW earned 120 total points
ID: 1163142
You can send yourself a WM_PRINT message
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1163143
Something like this...

void CMyDialog::PrintMe() {
  // Instantiate a CPrintDialog.
  CPrintDialog printDlg(FALSE, PD_ALLPAGES | PD_RETURNDC, NULL);
  // 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;
  // Display Windows print dialog box.
  printDlg.DoModal();
  // Obtain a handle to the device context.
  HDC hdcPrn = printDlg.GetPrinterDC();
  if (hdcPrn != NULL) {
    CDC *pDC = new CDC;
    pDC->Attach (hdcPrn);      // attach a printer DC
    pDC->StartDoc("job");      // begin a new print job
    pDC->StartPage();          // begin a new page
    Print(pDC,PRF_CHILDREN|PRF_CLIENT|PRF_ERASEBKGND);
    pDC->EndPage();            // end a page
    pDC->EndDoc();             // end a print job
    pDC->Detach();             // detach the printer DC
    delete pDC;
  }
}

The CWnd::Print function does the printing by sending WM_PRINT to all the controls (to print each control);  and sends WM_PRINTCLIENT to itself (to print the dialog itself)

0
 

Author Comment

by:grimaldi
ID: 1163144
Thank for your code. It is good but the problem is that it does not printing the static text that I inserted into dialog box by palette, while it's printing the other controls.
How is it possible ?
I have another problem: I set a dialog property: vertical scroll-bar, but when I resize in execution the dialog box the scroll-bar does not activate. Why ?
Thanks in advance.
Nino
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1163145
Ah .. forgot that.  Statics for some reason known only to M$, won't respond to the printing (by default).

You need to derive your own CStatic derived class that resonds to the WM_PRINT message by printing itself.  I'll see if I can dig up some code for this.  It shouldn't be TOO hard.  Perhaps just calling Default() will do it? .. I'll see what I can find.

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 10

Expert Comment

by:RONSLOW
ID: 1163146
Try deriving from CStatic and handline WM_PRINT by sending a WM_PAINT with the same wParam.  It might work.

Of course, then you need to subclasss you static's in the dialog (pretty disgusting).

Another way my be to put in a loop in your printme that goes thru all the child controls, checks if they are statics and then sends them the approriate WM_PRINT .. but that is tricky.

I'm still looking for a better solution for printing the statics for you.
0
 

Author Comment

by:grimaldi
ID: 1163147
Thanks for your advice, but I found a solution for my problem, maybe not the best.
In fact I'm printing static text by TextOut() method of the device context  In this case the problem is that I have to write my text in a file and not diretcly in the resource CStatic text and I have to write 5 or 6 pages !!!
But there is a problem with TextOut(),  because I don't know why the newline doesn't work
in printing. Do you Know why ?
Thanks
Nino
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1163148
You'd need to post some code.

Also I don't understand what you mean by "to write my text in a file and not diretcly in the resource CStatic text" ??
0
 

Expert Comment

by:skater6188
ID: 4615215
Thanks alot the code works perfect, but it print out in a 1" by 1" size. Is there any way to increase the size?

Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

910 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