|
could be a reference for my printer project.
|
|
|
|
|
Printing is tricky. I did an adaptation of this technique to an MFC application derived from CEditView (Edipar). This makes a small text editor to which I wished to add font display and print control. For getting text from the CEdiparDocument clas, do the following :
CString text;
CEdit& m_UserText = GetEditCtrl();
m_UserText.GetWindowText(text);
and if there is OnPrepareDC member function, be careful to remove the default call :
CEditView::OnPrepareDC(pDC, pInfo);
this will cause a severe run-time error and aborts program.
For a margin, you can define an integer
int margin = 300; / e.g.
and modify the following line
pDC->TextOut(pInfo->m_rectDraw.left+margin, y, line);
and you will get a margin.Raymond GIMILIO
CDS/ISIS-UNESCO expert
Winisis32 project
|
|
|
|
|
Hello
I need to add a page setup system for ceditview and want to apply margins while printing. Any help ?
Thanks
|
|
|
|
|
Hello,
my CString is quite big (around 1MB), and contains UNICODE control characters (LRM).
When I used this method (or something very similar), loading the print preview of the pages too took very long time(minutes).
However when I didnt tried to clip the text (GetTextExtent) it worked MUCH faster (around 2 secs).
I have tried doing print preview to my text in word and it worked much quicker!
Is there any way to speed things up?
P.S
Great article.
Sincerely yours
Y.R.
|
|
|
|
|
I am using an MFC Grid Control written by Chris Maunder. The reason for this was that I needed a quick and easy means of pupulating a grid with records from a database and then being able to print them. This grid does that job perfectly in that I can also determine progamatically control what I put into the grid.
Now I am trying to force PAGE BREAKS at certain points of the printing process. These point will be defined by a user by selection.
How do I implement this. What do I have to do. Please HELP!!!!
Omar
|
|
|
|
|
Hello,
I am trying to print multiple print job using your long string concept.
I am calling CPrintDialog from a Dialog based application and geting the CDC of
the printer form GetPrinterDC() and pass this to your long string method
to start the printing. The first print job goes through okay but anyone after that
it fails in CSize csText = GetCharSize( pDC) , crashes, any idea hot to print
multiple jobs without calling the print dialog multiple times?
thanks for any help or suggestions
vv
|
|
|
|
|
Hello,
I am constructing a long(very long) string that is 4 columns and about 60 rows.
the first col is row number second is name(characters upper and lower case) third column is
address and 4th is tel. #.
I have hard time lining up the columns because of combination of charaters upper/lower
and numbers. Is there a way to overcome this situation ?
thanks for any suggestion and advice.
vd
|
|
|
|
|
Try printing in monospace font like courier/courier new.
|
|
|
|
|
I did try your suggestion and didn't work. One of CodeProject "article contributor" suggested to
calculate width of each column and go from there.I haven't tried it yet but it sounds the right
way to do it.
|
|
|
|
|
I am using Windows XP Pro. I am using Microsoft Visual C++ .NET 7.0 (2002). I am using a computer I have built and assembled myself. I have written a program that uses CEditView as the public base class for one of my Child View classes. I used SetWindowText(text) to place text into the view, (works great). Now I want to use the Print Preview and Print features of the base class.
I have tried calling the CEditView defaults of the OnBeginPrinting(), OnPrint(), OnEndPrinting(),
and all I get is the first character of the text string in the font that I want and nothing else.
I have applied your code in the proper places. It compiles fine. I used GetWindowText(text) as well as a CString object, in the proper places. IT bombs in the Print Preview durring execution. In debugmode it is bombing at line 484 of viewedit.cpp:
UINT nPageSave = pInfo->m_nCurPage;
ASSERT(nPageSave > 1);// this is the line
ASSERT(nPageSave >= (UINT)m_aPageStart.GetSize());
I am not sure why this is as of course there might only be 1 page of print, and as I follow nPageSave in the watch window it is definatly set a 1.
I have spent many hours. I want to just see the text in the Print Preview as TEXT, not as a screen dump, or as a rendered bitmap or what ever, just the text and only the text. I don't want any background colors from the screen, JUST the TEXT.
Please help!!!!!!!!!!!!!!
Sincerely,
Donald
The World as we think we know it Has a lot more to it than meets the eye.
A Mad Scientist who has seen a wormhole for himself....
|
|
|
|
|
Simply override void OnPrepareDC(CDC* pDC, CPrintInfo* pInfo) and do not implement anything in it (also do NOT call the base class implementation) and your problem should be solved.
The reason for the problem is that the default implementation is also trying to distribute the text over several pages, but the example from this article does this on its own. As a result, the MFC "paginate" implementation does not work and also is not required. Hope this helps.
|
|
|
|
|
Hi
I used your source code in my project and the word wrap during printing did not come out right. The next line starts with a few characters of the previous line. I have made a few changes to the code and they are (in ExtractLine():
if (!pBreak && size.cx >= width)
{
// we may need to back track
if (line.Right(1) == " ")
{
// last character was a space, no need to backtrack
}
else
{
// go back until we find a space or the begining of this line (which means the word covers
// more than a whole line, so we should just break it here)
int p = pos;
i = line.GetLength() - 1 ;
while (i >= 0 && line.GetAt(i) != ' ')
{
--i ;
--pos ;
}
if (i < line.GetLength())
{
// the line is a whole word, just break at this position - 1
line = line.Left(i) ;
}
else
{
// we have trimmed to the correct position, but we should skip the space character
++pos ;
}
}
}
Hope I am not wrong!
|
|
|
|
|
Dear Roger,
I utilized your project a lot. It is simple and working. I'm looking forward to see your next work.
Aydin,
from Turkey
|
|
|
|
|
Hello,
can You tell me how to print various page number with MFC?for example,i have a variable X which user enters and i want to print X pages and every page have different text:
"This is Page %X"
Thanks.;)
|
|
|
|
|
I'm trying to print a "Hello World" to a printer, but been unsuccesful so far. Can anyone please help.
Below is an example I found at MSDN, but when compiled gives me 10 errors. I don't know which header files I'm supposed to include.
<br />
<br />
<br />
#include <afxdlgs.h><br />
<br />
void PrintStuff( HWND hWndParent )<br />
{<br />
HDC hDC;<br />
DOCINFO di;<br />
<br />
hDC = GetPrinterDC();<br />
<br />
if( !hdc)<br />
{<br />
MessageBox(NULL, "Error creating DC", "Error",<br />
MB_APPLMODAL | MB_OK );<br />
return;<br />
}<br />
<br />
if( SetAbortProc( hDC, AbortProc ) == SP_ERROR )<br />
{<br />
MessageBox( NULL, "Error setting up AbortProc",<br />
"Error", MB_APPLMODAL | MB_OK);<br />
return;<br />
}<br />
<br />
InitDocStruct( &di, "MyDoc");<br />
StartDoc( hDC, &di );<br />
<br />
StartPage( hDC );<br />
DrawStuff( hDC );<br />
EndPage( hDC );<br />
<br />
EndDoc( hDC );<br />
<br />
DeleteDC( hDC );<br />
}<br />
<br />
<br />
<br />
<br />
HDC GetPrinterDC(void)<br />
{<br />
PRINTDLG pdlg;<br />
<br />
memset( &pdlg, 0, sizeof( PRINTDLG ) );<br />
pdlg.lStructSize = sizeof( PRINTDLG );<br />
pdlg.Flags = PD_RETURNDEFAULT | PD_RETURNDC;<br />
<br />
PrintDlg( &pdlg );<br />
return pdlg.hDC;<br />
}<br />
<br />
<br />
<br />
<br />
BOOL CALLBACK AbortProc( HDC hDC, int Error )<br />
{<br />
MSG msg;<br />
while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )<br />
{<br />
TranslateMessage( &msg );<br />
DispatchMessage( &msg );<br />
}<br />
return TRUE;<br />
}<br />
<br />
<br />
<br />
<br />
void InitDocStruct( DOCINFO* di, char* docname)<br />
{<br />
memset( di, 0, sizeof( DOCINFO ) );<br />
di->cbSize = sizeof( DOCINFO );<br />
di->lpszDocName = docname;<br />
}<br />
<br />
<br />
<br />
<br />
void DrawStuff( HDC hdc)<br />
{<br />
TextOut(hdc, 0,0, "Test Printing", lstrlen( "Test Printing" ) );<br />
}
any help will be greatll appreciated.
mel
|
|
|
|
|
I would recommend looking at the article http://www.codeproject.com/printing/printing_wo_docview.asp[^] by Chris Maunder as a good starting point for learning to write printing code. The core functionality is present although its slightly different than what you have posted here. Its also probably easier to understand.
If you get really stuck on your project, and I mean really stuck, wrap it up in a ZIP file and post it to me. I don't get much free time to help people as much as I used to at the moment, so only do this is absolutely necessary!
Roger Allen
Sonork 100.10016
I have a terminal disease. Its called life!
|
|
|
|
|
 Thanks for advise Roger. I've looked at that Chris' example and saved it in my bookmark.
I really appreciate your offer to try help me when I can't figure out something. I'll ask for help only after I spend some days on a single problem.
Finally, with my supervisor's help, I printed out to a printer a "test printing" message.
All we changed was as follows:
we used only #include <windows.h> as header file.
declared GetPrinterDC in the first line of code as HDC GetPrinterDC(void).
and, made PrintStuff function to take no parameters, as void PrintStuff(/*HWND hWndParent*/)
I'll include the complete code below in the case somone else may need it.
<br />
<br />
<br />
#include <Windows.h><br />
<br />
<br />
<br />
<br />
<br />
HDC GetPrinterDC(void)<br />
<br />
{<br />
PRINTDLG pdlg;<br />
<br />
memset( &pdlg, 0, sizeof( PRINTDLG ) );<br />
pdlg.lStructSize = sizeof( PRINTDLG );<br />
pdlg.Flags = PD_RETURNDEFAULT | PD_RETURNDC;<br />
<br />
PrintDlg( &pdlg );<br />
return pdlg.hDC;<br />
}<br />
<br />
<br />
<br />
<br />
BOOL CALLBACK AbortProc( HDC hDC, int Error )<br />
{<br />
MSG msg;<br />
while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )<br />
{<br />
TranslateMessage( &msg );<br />
DispatchMessage( &msg );<br />
}<br />
return TRUE;<br />
}<br />
<br />
<br />
<br />
<br />
void InitDocStruct( DOCINFO* di, char* docname)<br />
{<br />
memset( di, 0, sizeof( DOCINFO ) );<br />
di->cbSize = sizeof( DOCINFO );<br />
di->lpszDocName = docname;<br />
}<br />
<br />
<br />
<br />
<br />
void DrawStuff( HDC hdc)<br />
{<br />
TextOut(hdc, 0,0, "Test Printing", lstrlen( "Test Printing" ) );<br />
} <br />
void PrintStuff( )<br />
{<br />
HDC hDC;<br />
DOCINFO di;<br />
<br />
hDC = GetPrinterDC();<br />
<br />
if( !hDC)<br />
{<br />
MessageBox(NULL, "Error creating DC", "Error",<br />
MB_APPLMODAL | MB_OK );<br />
return;<br />
}<br />
<br />
if( SetAbortProc( hDC, AbortProc ) == SP_ERROR )<br />
{<br />
MessageBox( NULL, "Error setting up AbortProc",<br />
"Error", MB_APPLMODAL | MB_OK);<br />
return;<br />
}<br />
<br />
InitDocStruct( &di, "MyDoc");<br />
StartDoc( hDC, &di );<br />
<br />
StartPage( hDC );<br />
DrawStuff( hDC );<br />
EndPage( hDC );<br />
<br />
EndDoc( hDC );<br />
<br />
DeleteDC( hDC );<br />
}<br />
<br />
int main(int argc, char * argv[])<br />
{<br />
PrintStuff();<br />
return 0;<br />
}
melshehu
|
|
|
|
|
Thanks Roger.
This is just what I was looking for! Hopefully you will be knocking out more articles on a similar basis? "hint, hint"
Dylan Kenneally
London, UK
|
|
|
|
|
I have a couple of print articles planned. 1 on monitoring a printer status etc, but I have to do all the research first!
I will also hopefully be expanding the "Tips and tricks" article soon.
Roger Allen
Sonork 100.10016
If I had a quote, it would be a very good one.
|
|
|
|
|
It's amazing how many articles on printing stop just short of this...
...actually, it's amazing how few good articles on printing their are period! You'd think it was some sort of black art.
So do you plan on doing any more printing articles?
--------
The real problem is that you don't have to be religious to be a religious fanatic,
you just have to be a fanatic. -- Stan Shannon, the lounge
|
|
|
|
|
I've got a few ideas that need expanding a bit. Maybe a printing tips and tricks article I think.
Roger Allen
Sonork 100.10016
If I had a quote, it would be a very good one.
|
|
|
|
|