最近在用vc写一个记事本,写到打印功能的实现,该功能是将一个编辑框内的文本通过打印机打印出来,打印功能函数代码如下:
void PrintPage(HWND hEdit)
{
PRINTDLG pd={0};
pd.lStructSize=sizeof(PRINTDLG);
pd.hwndOwner = hEdit;
pd.hDevMode;
pd.hDevNames;
pd.hDC=0;
pd.Flags=PD_ALLPAGES|PD_SELECTION|PD_PAGENUMS| PD_RETURNDC | PD_NOSELECTION | PD_HIDEPRINTTOFILE;
pd.nFromPage;
pd.nToPage;
pd.nMinPage;
pd.nMaxPage;
pd.nCopies;
pd.hInstance;
pd.lCustData;
pd.lpfnPrintHook;
pd.lpfnSetupHook;
pd.lpPrintTemplateName;
pd.lpSetupTemplateName;
pd.hPrintTemplate;
pd.hSetupTemplate;
if(PrintDlg(&pd))
{
if (pd.hDC)
{
DOCINFO docInfo;
TEXTMETRIC tm;
TCHAR szMsg[]=_T("文档名");
TCHAR *textBuf;
TCHAR *textTemp;
int len;
int i;
RECT rc={0,0,800,560};
docInfo.cbSize = sizeof(szMsg);
docInfo.lpszDocName = szMsg;
docInfo.lpszOutput = NULL;
//开始打印
if (StartDoc(pd.hDC,&docInfo)>0 && StartPage(pd.hDC)>0 )
{
GetTextMetrics(pd.hDC,&tm);//获取字体信息
SetMapMode(pd.hDC,MM_ANISOTROPIC);
SIZE size={800,560};
//设置视口坐标映射
SetWindowExtEx(pd.hDC,size.cx,size.cy,NULL);
int logX = GetDeviceCaps(pd.hDC,LOGPIXELSX);
int logY = GetDeviceCaps(pd.hDC,LOGPIXELSY);
long xExt = (long)size.cx * logX/GetDeviceCaps(GetDC(NULL),LOGPIXELSX) ;
long yExt = (long)size.cy * logY/GetDeviceCaps(GetDC(NULL),LOGPIXELSY) ;
SetViewportExtEx(pd.hDC,xExt,yExt,NULL);
//在此处输出
long x=0,y=0;
len = GetWindowTextLength(hEdit);
textBuf = (TCHAR*)malloc(len*sizeof(TCHAR)+sizeof(TCHAR));
GetWindowText(hEdit,textBuf,len+1);
//这里如何将textBuf字符串的内容输出到打印机呢?能够自动换行和换页
free(textBuf);
//输出结束
if (EndPage(pd.hDC)>0)
EndDoc(pd.hDC);
else
AbortDoc(pd.hDC);
}
DeleteDC(pd.hDC);
}
}
}
如以上代码,如何将textBuf内的字符串输出到打印机呢?