Solved

Print in a dialog box of Visual C++

Posted on 1998-04-22
8
473 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
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

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

757 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