麻烦看下程序咋改才能保证有空格产生啊。
打印代码如下
[C++] 纯文本查看 复制代码 this->UpdateData();
CString strMessage;
CString strPrintDevice;
this->GetDlgItem(IDC_EDIT1)->GetWindowTextW(strMessage);
strMessage += _T("\r\n"); //添加结尾,方便后面循环读取打印数据
this->GetDlgItem(IDC_COMBO1)->GetWindowTextW(strPrintDevice);
DWORD dwFlag = PD_ALLPAGES | PD_NOPAGENUMS | PD_USEDEVMODECOPIES | PD_HIDEPRINTTOFILE; //打印配置界面的按钮可用性
CPrintDialog pPrintdlg(FALSE, dwFlag, this); //CPrintDialog实例化
HGLOBAL hDevMode = NULL;
HGLOBAL hDevNames = NULL;
if (GetPrinterDevice(strPrintDevice.GetBuffer(0), &hDevNames, &hDevMode)) //获得指定打印机的配置、名字
AfxGetApp()->SelectPrinter(hDevNames, hDevMode);
else
AfxMessageBox(_T("Failed to select custom printer"));
strPrintDevice.ReleaseBuffer();
pPrintdlg.m_pd.hDevMode = hDevMode; //让pPrintdlg使用我们指定的打印机
pPrintdlg.m_pd.hDevNames = hDevNames;
CDC dc;
dc.Attach(pPrintdlg.CreatePrinterDC()); //后台打印创建法,如果需要弹出打印对话框,请用DoModal
DOCINFO di; //下面的内容网上很多,就不解释了
di.cbSize = sizeof(DOCINFO);
di.lpszDocName = _T("打印测试");
di.lpszDatatype = NULL;
di.lpszOutput = NULL;
di.fwType = 0;
dc.StartDocW(&di);
dc.StartPage();
dc.SetMapMode(MM_TEXT);
CRect recPrint(0, 0, dc.GetDeviceCaps(LOGPIXELSX), dc.GetDeviceCaps(LOGPIXELSY));
dc.DPtoLP(&recPrint);
dc.SetWindowOrg(0, 0);
CFont newFont;
VERIFY(newFont.CreatePointFont(120, _T("宋体"), &dc));
CFont* oldFont = dc.SelectObject(&newFont);
dc.SetTextAlign(TA_TOP | TA_LEFT);
CString strPrint;
int nIndex = 0;
int x = 50;
int y = 50;
CSize textSize;
textSize = dc.GetTextExtent(_T("00"), 2); //根据当前字体的宽、高,后面以此高度为行高
while ((nIndex = strMessage.Find(_T("\r\n"))) > -1) //将IDC_EDIT1编辑框中内容打印,支持换行,一次换行等于'\r\n',所以在开头strMessage += _T("\r\n")
{
strPrint = strMessage.Left(nIndex);
strMessage = strMessage.Mid(nIndex + 2);
dc.TextOutW(x, y, strPrint);
y += textSize.cy; //下移一行,行高为字体高度
}
dc.SelectObject(oldFont);
newFont.DeleteObject();
dc.EndPage();
dc.EndDoc();
DeleteDC(dc.Detach()); |