RichEdit有多页文本时也只能打印一页问题,贴出代码请高手帮忙。
BOOL CReportForm::PrintRich(const CString& Title)
{
CDC ThePrintDC; //create a device context to use
CPrintDialog PrintDialog(FALSE); //make a print dialog too
if(PrintDialog.DoModal()==IDOK) //pressed the ok button on the print dialog?
{
ThePrintDC.Attach(PrintDialog.GetPrinterDC());//if so get settings
}
else
return FALSE; //leave this procedure, before anything bad happens
long CharRange=0;
long LastChar=0;//will store document length
DOCINFO di; //make a docinfo structure
::ZeroMemory(&di, sizeof(DOCINFO));
di.cbSize=sizeof(DOCINFO); //set size member
di.lpszDocName="aa";//set doc name, was passed via another funtion
FORMATRANGE fr; //format range structure
::ZeroMemory(&fr, sizeof(FORMATRANGE));
HDC hdc=ThePrintDC.GetSafeHdc(); //get handle to DC we are using
fr.hdc=hdc; //Set meber in FormatRange struct
fr.hdcTarget=hdc; //Set member in FormatRange struct
//This bit here will get all the dimentions of the printer setup
int nHorizRes = ThePrintDC.GetDeviceCaps(HORZRES), //width P in MM
nVertRes = ThePrintDC.GetDeviceCaps(VERTRES), //hight in raster lines
nLogPixelsX = ThePrintDC.GetDeviceCaps(LOGPIXELSX), //pixels per inch along x
nLogPixelsY = ThePrintDC.GetDeviceCaps(LOGPIXELSY); //pixels per inch along y
//set the printable area of printer in the FormatRange struct
fr.rcPage.left = 0; //these 2 mean top left
fr.rcPage.top = 0;
fr.rcPage.right = (nHorizRes/nLogPixelsX) * 1440;//these 2 mean bottom right
fr.rcPage.bottom = (nVertRes/nLogPixelsY) * 1440; //equation changes pixel to TWIPS
// Set up some margins all around. Make them one inch
//results vary on printers depending on setup
fr.rc.left = fr.rcPage.left + 720; // 1440 TWIPS = 1 inch.
fr.rc.top = fr.rcPage.top + 720;
fr.rc.right = fr.rcPage.right - 720;
fr.rc.bottom = fr.rcPage.bottom - 720;
//select all text for printing
CHARRANGE cr;
cr.cpMin=0;
cr.cpMax=-1;//-1 selects all
fr.chrg=cr;//set this in FormatRange struct
//get length of document, used for more than one page
CharRange=c_richedit.GetTextLength();
int ErrorStatus=0;
//create cancel dialog
//CPrintAbortDialog* pAbort=new CPrintAbortDialog();
//pAbort->Create(IDD_ABORTPRINT, this);
//pAbort->ShowWindow(SW_SHOW);
//Start Printing
//ThePrintDC.SetAbortProc(&AbortProc(hdc, )); //dont know how callbacks works yet
//ThePrintDC.StartDoc(&di); //start printing document
StartDoc(ThePrintDC,&di); //start printing document
do
{
//pAbort->Increment();//show it on print abort dialog
ThePrintDC.StartPage(); //start the page
LastChar = c_richedit.FormatRange( &fr, TRUE );//send text to DC, and record index
//of last fitting char
ErrorStatus=ThePrintDC.EndPage();//end this page, and record status
cr.cpMin=LastChar;//Change charrange struct for next page
cr.cpMax=CharRange; //using last char printed
fr.chrg=cr; //set that in formatrange struct
}while(LastChar=0); //while there is stuff to print ot there
//is not an error(error is -No)
//if there is an error or printing has finished
//have to make sure AbortDoc is called instead of EndDoc if there has been a problem
switch(ErrorStatus)
{
case SP_ERROR:
{
//pAbort->DestroyWindow();
//delete pAbort;
ThePrintDC.AbortDoc();
AfxMessageBox("There was a general printing error, please check printer is working properly, connected, on line etc.", MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
case SP_APPABORT:
{
//pAbort->DestroyWindow();
//delete pAbort;
ThePrintDC.AbortDoc();
return FALSE;
}
case SP_USERABORT:
{
//pAbort->DestroyWindow();
//delete pAbort;
ThePrintDC.AbortDoc();
return FALSE;
}
case SP_OUTOFDISK:
{
//pAbort->DestroyWindow();
//delete pAbort;
ThePrintDC.AbortDoc();
AfxMessageBox("The print spooler is out of disk space, free up some disk space and try again.", MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
case SP_OUTOFMEMORY:
{
//pAbort->DestroyWindow();
//delete pAbort;
ThePrintDC.AbortDoc();
AfxMessageBox("Your computer is out of memory, shut down some applications and/or some windows and try again.", MB_OK | MB_ICONEXCLAMATION);
return FALSE;
}
default:
{
//pAbort->DestroyWindow();
//delete pAbort;
ThePrintDC.EndDoc();
return TRUE;
}
}
}
[解决办法]
求高手帮忙。
[解决办法]
都没人回答啊。