Originally posted by: Tim
First: sorry for my bad english
Problem:
SOMETIMES (only in the release NOT in the debug version) my program crashed, when i used the table-printing functions.
I "debugged" the problem in the release version, and saw, that the pTable->EndRow member wasn't at zero, but the pTable pointer was new, made with pTable = new TABLEHEADER
The fix for this problem was to add a line in the constructor of TABLEHEADER like
EndRow = 0;
btw: REALLY GREAT CLASS ;)
Tim
Originally posted by: mylonestartear
In my print.cpp I use MM_TWIPS and MM_TEXT mode,how i can transact logical unit (in MM_TWIPS) to logical unit (in MM_TEXT)?
Originally posted by: Manuel
I like to print text justified with this library?
How i can do this? or what i have to change in the source code of the library for print in this way?
thank's.
Originally posted by: ken chan
With this well-written print class, I have encountered some problems with printing in the NT and Win2K environments. Using the table functions in the print class (VC6, SP5), my application program works without any difficulties in the Win98 and ME environments. However, under Win2K and NT, the tables on the report would not show any contents at all (blank sheet other than headers and footers).
I was able to resolve the problem by initializing the table as follows:
pTable->EndCol = 7.7;
/* EndCol must be less than the printer's printable width (EndCol=7.9 was OK in Win98. Differences in the print drivers?)
*/
pTable->EndRow = 8.0;
/* EndRow was not initialized... EndRow value will be updated subsequently by Cpage depending on the size of the table.
*/
Note the following codes from Cpage.cpp
void CPage::Table(TABLEHEADER* TheTable)
{
PRTTYPE temp;
...
TheTable->EndRow=ConvertToMappedUnits(TheTable->EndRow,VERTRES);
...
// check for horizontal margins
if(TheTable->EndCol > SetRightMargin(0))
return;
// create a table printing object and attach it to the table descriptor
CPrintTable* pTable=new CPrintTable(TheTable,this);
...
}
If EndCol > SetRightMargin(0), pTable would not be initialized and therefore all subsequent calls to CPage::Print would not be performed as intended as shown on the following codes:
void CPage::Print(TABLEHEADER* TheTable,int row,int col,int PointSize,UINT TextFlags,char* fmt,...)
{
CPrintTable* pTable=TheTable->pClsTable;
if(pTable==NULL)
return;
...
}
Originally posted by:
Reply
Originally posted by: Michael Albry
The method void CPage::Table(TABLEHEADER* TheTable)
<Snip>
A pointer of type CPrintTable is allocated and never freed.
I added the following lines infront of above discussed lines of code:
if (pTable) {
with the result, that as to the comment: "if you free it early there will be a system crash" became true.
What else needs to be close the memory leak is basic:
. Check all Constructors of related classes and initialize
Sorry, currently I cannot provide my implementation of the class, it is not ready.
Thank you for your attention.
Michael Albry
Before I start critizing I thank the author of this class as it really hits a market of programmers, having trouble setting up the code themselves, for which reason ever. The reason why I was able to "love" the class is, because it is documented in a very professionel manner. I wanted to print, saw the examples and hey, it worked. However, there are memory leaks, the author was aware of and didn't fix(?) :{. Please check the following comments from the original code:
shows us the following code lines and comments:
CPrintTable* pTable=new CPrintTable(TheTable,this);
// pTable will be freed when the destructor for
Thetable runs
// if you free it early there will be a system crash
TheTable->pClsTable=pTable;
<Snip>
Hence, there are memory leaks. The conclusion of the comment is wrong!
delete pTable;
}
member variables to NULL or 0
. Check all the Destructors to delete all allocated member
variables.
. After having deleted all class members initialize them
to NULL or 0.
. After all your printing code is done, delete
CPrintTable *pClsTable in the TABLEHEADER structure, as
well as the CPage pointer.
void CPage::Table(TABLEHEADER* TheTable)
{
PRTTYPE temp;
if(TheTable->UseInches)
{
TheTable->StartRow=ConvertToMappedUnits(
TheTable->StartRow,VERTRES);
TheTable->StartCol=ConvertToMappedUnits(
TheTable->StartCol,HORZRES);
TheTable->EndCol=ConvertToMappedUnits(
TheTable->EndCol,HORZRES);
TheTable->EndRow=ConvertToMappedUnits(
TheTable->EndRow,VERTRES);
for(int y=0;y < TheTable->NumColumns;++y){
TheTable->ColDesc[y].Width=ConvertToMappedUnits(
TheTable->ColDesc[y].Width,HORZRES);
}
// check for horizontal margins
if(TheTable->EndCol > SetRightMargin(0))
return;
// create a table printing object and attach it to the table descriptor
CPrintTable* pTable=new CPrintTable(TheTable,this);
// pTable will be freed when the destructor for Thetable
runs
// if you free it early there will be a system crash
TheTable->pClsTable=pTable;
temp=m_PrtDesc;
// print rhe table
pTable->PrintTable();
if(TheTable->UseInches)
TheTable->EndRow=ConvertToInches(
(int)TheTable->EndRow,VERTRES);
m_PrtDesc=temp;
}
Originally posted by: Aldevinas
The printing works fine, but how do I add print preview capability? I tried a few times, but the app crashes or does not display table in the tables demo. Any ideas and source code?
ReplyOriginally posted by: Allen Friend
I have been using the Print Library for nearly 2 years now and for the most part it works great and provides all the needed routines to get my printing jobs done.
Over the last 2 years I have encountered a program crash during the spooling of the print job to some printers. Problem appeared more often when printing to Windows NT and or Windows 2000. I did change the print driver in some cases and the problem seemed to be resolved. It also appeared that printers that had a print driver that supports PCL or those that support Post Script seemed to be more likely to CRASH the program during the print spooling.
What has made this problem hard to resolve is that for the most part printing to the majority of printers all worked perfect, it just caused a program CRASH during the print spooling to some printers.
** THE FIX **
After reviewing all the comments that had been posted, I saw one that appeared to be a possible fix for my problem. The fix that was suggested was for fixing a second page printing problem. After looking at my code I had failed to save the printer DC before creating my page and had failed as well to restore it after printing the page.
Jim Thomas, submited this fix to resolve even numbered page printing problem.
Even numbered pages don't print correctly - FIXED!
------------------------------------------------------------To fix the even numbered pages print problem, just add the following code to your OnPrint function.
void CMyView::OnPrint(CDC* pDC, CPrintInfo* pInfo)
{
int sdc = pDC->SaveDC(); // save dc state
// do your printing here
pDC->RestoreDC(sdc); // restore dc state
return;
}
------------------------------------------------------------
Jims code would probably work perfect for most dealers to resolve this problem.
For me this didn't work, because of the way I am setting my print page... In any case simply save the printer DC prior to doing any page formating and obtaining the new CPage pointer.
** MY CODE SUGGESTION **
Put these in your View class.... or simply make these program globals.
/////////////////////
// Global Printer Saved DC varriable
int sdc;
// Global pointer to CPage, the Page to print.
CPage* psPrinterPage;
//////////////////////
Now the way I used it is....
// save dc state just prior to creating the CPage pointer.
// and before making any change to the printer DC.
sdc = pDC->SaveDC();
// set to a new page each time
psPrinterPage = new CPage(pInfo->m_rectDraw,pDC);
// Add additional code for printing like StartPage()
// Now send this formated page to the OnPrint Function
// In OnPrint I then do some additional page formating.
OnPrint(pDC, pInfo);
// Add additional code for ending printing like EndPage()
// Now you need to delete the CPage pointer
delete psPrinterPage;
// Now restore the printer DC prior to doing another page
// restore dc state
pDC->RestoreDC(sdc);
//
** My failure to save and restore the printer printer DC caused me many, many headaches, when some printers would crash during the print spool process. Saving the printer DC and restoreing it has fixed this for me. Many thanks to Jim Thomas and his code contribution. Finding that this code change would also cure my mysterious CRASH problem, was a great blessing.
Just remember to save the Printer DC prior to creating the
new CPage pointer and always reset the Printer DC after deleting the CPage printer. I have encountered no other problems with this great printing class.
Thanks
Allen Friend
Originally posted by: Erich Ruth
Any suggestions. When I print in windows 98, it works great. When I print in windows NT, it crashes?
Please, any response any one can give me will be greatly appreciated.
Sincerely,
Erich J. Ruth
Originally posted by: csoft
A nice addition to this code would be to allow user to select font before printing or previewing. This would be great to illustrate to beginners (like me) how the font selection process and device context setting for selected font works.
Thanks in advance