Solved

Print in a dialog box of Visual C++

Posted on 1998-04-22
8
488 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

820 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