如下代碼運(yùn)行的環(huán)境簡(jiǎn)述:
MFC的對(duì)話框界面上創(chuàng)建一個(gè)按鈕藕咏,點(diǎn)擊這個(gè)按鈕打印界面上CListCtrl控件上現(xiàn)實(shí)的內(nèi)容。
void CMaxValueDlg::OnBnClickedButton5()
{
// TODO: 在此添加控件通知處理程序代碼
// 獲取默認(rèn)電腦默認(rèn)打印機(jī)的名稱(chēng)實(shí)現(xiàn)
CString strPrintDevice;
TCHAR szBuffer[1024] = { 0 };
DWORD length = 1024;
int ret = ::GetDefaultPrinter(szBuffer, &length);
if (ret == FALSE)
ret = ::GetLastError();
else
{
strPrintDevice= szBuffer;
}
if (ret == ERROR_INSUFFICIENT_BUFFER)
{
CString temp;
temp.Format(_T("%d"), length);
AfxMessageBox(CString(_T("ERROR_INSUFFICIENT_BUFFER")) + _T(" the real size is ") + temp);
}
else if (ret == ERROR_FILE_NOT_FOUND)
AfxMessageBox(_T("ERROR_FILE_NOT_FOUND"));
else
{
//CString strRet;
//strRet.Format(_T("%d"), ret);
//AfxMessageBox(strRet);
}
CString startTm = m_date_s.Format("%Y-%m-%d %H:%M:%S");
CString endTm = m_date_e.Format("%Y-%m-%d %H:%M:%S");
CString idx;idx.Format("%d", i_id);
CString wndTm; wndTm.Format("%d", m_UnitWnd);
CString lockStatus;
if (m_bNeedLockCheck)
lockStatus = "有效";
else
lockStatus = "無(wú)效";
//this->UpdateData();
CString strMessage ;
strMessage.AppendFormat("%s \t\t\r\n\r\n\r\n", m_printStr);
strMessage.AppendFormat("起始日期 %s \t\t\r\n", startTm);
strMessage.AppendFormat("截止日期 %s \t\t\r\n", endTm);
strMessage.AppendFormat("單元數(shù)據(jù)窗 %s \t\t\r\n", idx);
strMessage.AppendFormat("最大值數(shù)量 %s \t\t\r\n", wndTm);
strMessage.AppendFormat("是否閉鎖 %s \t\t\r\n\r\n\r\n\r\n", lockStatus);
strMessage.Append("序號(hào) 極大值 項(xiàng)別 不平衡度 記錄時(shí)間 是否有效\r\n");
CString buf[10000][6];
int count = c_list.GetItemCount();
int colm = c_list.GetHeaderCtrl()->GetItemCount();
for (int i = 0; i < count; i++) {
for (int j = 0; j < colm; j++) {
buf[i][j] = c_list.GetItemText(i, j);
}
strMessage.AppendFormat("%s %s %s %s %s %s \r\n", buf[i][0], buf[i][1], buf[i][2], buf[i][3], buf[i][4], buf[i][5]);
}
DWORD dwFlag = PD_ALLPAGES | PD_NOPAGENUMS | PD_USEDEVMODECOPIES | PD_HIDEPRINTTOFILE; //打印配置界面的按鈕可用性贸铜,因?yàn)楹笈_(tái)打印堡纬,其實(shí)這個(gè)配置沒(méi)什么意義
CPrintDialog pPrintdlg(FALSE, dwFlag, this); //CPrintDialog實(shí)例化,因?yàn)镸FC的打印設(shè)備無(wú)關(guān)性蒿秦,可以理解為這就是一臺(tái)打印機(jī)
HGLOBAL hDevMode = NULL;
HGLOBAL hDevNames = NULL;
if (GetPrinterDevice(strPrintDevice.GetBuffer(0), &hDevNames, &hDevMode)) //獲得指定打印機(jī)的配置烤镐、名字
AfxGetApp()->SelectPrinter(hDevNames, hDevMode);
else
AfxMessageBox(_T("Failed to select custom printer"));
strPrintDevice.ReleaseBuffer();
pPrintdlg.m_pd.hDevMode = hDevMode; //讓pPrintdlg使用我們指定的打印機(jī)
pPrintdlg.m_pd.hDevNames = hDevNames;
CDC dc;
dc.Attach(pPrintdlg.CreatePrinterDC()); //后臺(tái)打印創(chuàng)建法,如果需要彈出打印對(duì)話框棍鳖,用DoModal
DOCINFO di;
di.cbSize = sizeof(DOCINFO);
di.lpszDocName = _T("有驅(qū)打印測(cè)試");
di.lpszDatatype = NULL;
di.lpszOutput = NULL;
di.fwType = 0;
dc.StartDocA(&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); //根據(jù)當(dāng)前字體的寬炮叶、高,后面以此高度為行高
while ((nIndex = strMessage.Find(_T("\r\n"))) > -1) //將IDC_EDIT1編輯框中內(nèi)容打印渡处,支持換行镜悉,一次換行等于'\r\n',所以在開(kāi)頭strMessage += _T("\r\n")
{
strPrint = strMessage.Left(nIndex);
strMessage = strMessage.Mid(nIndex + 2);
// dc.TextOutW(x, y, strPrint);
dc.TextOutA(x, y, strPrint);
y += textSize.cy; //下移一行医瘫,行高為字體高度
}
dc.SelectObject(oldFont);
newFont.DeleteObject();
dc.EndPage();
dc.EndDoc();
DeleteDC(dc.Detach());
}
調(diào)用打印機(jī)侣肄,打印內(nèi)容
bool CMaxValueDlg::GetPrinterDevice(LPTSTR pszPrinterName, HGLOBAL* phDevNames, HGLOBAL* phDevMode)
{
//TODO: 在此處添加實(shí)現(xiàn)代碼.
//if NULL is passed, then assume we are setting app object's
//devmode and devnames
if (phDevMode == NULL || phDevNames == NULL)
return FALSE;
// Open printer
HANDLE hPrinter;
if (OpenPrinter(pszPrinterName, &hPrinter, NULL) == FALSE)
return FALSE;
// obtain PRINTER_INFO_2 structure and close printer
DWORD dwBytesReturned, dwBytesNeeded;
GetPrinter(hPrinter, 2, NULL, 0, &dwBytesNeeded);
PRINTER_INFO_2* p2 = (PRINTER_INFO_2*)GlobalAlloc(GPTR,
dwBytesNeeded);
if (GetPrinter(hPrinter, 2, (LPBYTE)p2, dwBytesNeeded,
&dwBytesReturned) == 0) {
GlobalFree(p2);
ClosePrinter(hPrinter);
return FALSE;
}
ClosePrinter(hPrinter);
// Allocate a global handle for DEVMODE
HGLOBAL hDevMode = GlobalAlloc(GHND, sizeof(*p2->pDevMode) +
p2->pDevMode->dmDriverExtra);
ASSERT(hDevMode);
DEVMODE* pDevMode = (DEVMODE*)GlobalLock(hDevMode);
ASSERT(pDevMode);
// copy DEVMODE data from PRINTER_INFO_2::pDevMode
memcpy(pDevMode, p2->pDevMode, sizeof(*p2->pDevMode) +
p2->pDevMode->dmDriverExtra);
GlobalUnlock(hDevMode);
// Compute size of DEVNAMES structure from PRINTER_INFO_2's data
DWORD drvNameLen = lstrlen(p2->pDriverName) + 1; // driver name
DWORD ptrNameLen = lstrlen(p2->pPrinterName) + 1; // printer name
DWORD porNameLen = lstrlen(p2->pPortName) + 1; // port name
// Allocate a global handle big enough to hold DEVNAMES.
HGLOBAL hDevNames = GlobalAlloc(GHND,
sizeof(DEVNAMES) +
(drvNameLen + ptrNameLen + porNameLen) * sizeof(TCHAR));
ASSERT(hDevNames);
DEVNAMES* pDevNames = (DEVNAMES*)GlobalLock(hDevNames);
ASSERT(pDevNames);
// Copy the DEVNAMES information from PRINTER_INFO_2
// tcOffset = TCHAR Offset into structure
int tcOffset = sizeof(DEVNAMES) / sizeof(TCHAR);
ASSERT(sizeof(DEVNAMES) == tcOffset * sizeof(TCHAR));
pDevNames->wDriverOffset = tcOffset;
memcpy((LPTSTR)pDevNames + tcOffset, p2->pDriverName,
drvNameLen * sizeof(TCHAR));
tcOffset += drvNameLen;
pDevNames->wDeviceOffset = tcOffset;
memcpy((LPTSTR)pDevNames + tcOffset, p2->pPrinterName,
ptrNameLen * sizeof(TCHAR));
tcOffset += ptrNameLen;
pDevNames->wOutputOffset = tcOffset;
memcpy((LPTSTR)pDevNames + tcOffset, p2->pPortName,
porNameLen * sizeof(TCHAR));
pDevNames->wDefault = 0;
GlobalUnlock(hDevNames);
GlobalFree(p2); // free PRINTER_INFO_2
// set the new hDevMode and hDevNames
*phDevMode = hDevMode;
*phDevNames = hDevNames;
return TRUE;
}