16,472
社区成员
发帖
与我相关
我的任务
分享
CDC ThePrintDC; //create a device context to use
CPrintDialog PrintDialog(FALSE); //make a print dialog too
// if(!PrintDialog.GetDefaults())
// return FALSE;
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=Title;//set doc name, was passed via another funtion
// MessageBox(di.lpszDocName);
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 + 1440; // 1440 TWIPS = 1 inch.
fr.rc.top = fr.rcPage.top + 1440;
fr.rc.right = fr.rcPage.right - 1440;
fr.rc.bottom = fr.rcPage.bottom - 1440;
//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=m_Title.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
int a= ThePrintDC.StartDoc(&di); //start printing document
DOCINFO di; //make a docinfo structure
::ZeroMemory(&di, sizeof(DOCINFO));
di.cbSize=sizeof(DOCINFO); //set size member
di.lpszDocName=Title;//set doc name, was passed via another funtion
...
...
省略(省略代码主要是设置页面大小)
int a= ThePrintDC.StartDoc(&di); //start printing document
do
{
// pAbort->Increment();//show it on print abort dialog
ThePrintDC.StartPage(); //start the page
LastChar = m_Title.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