📄 subject_35474.htm
字号:
<p>
序号:35474 发表者:崔瑞 发表日期:2003-04-07 10:59:08
<br>主题:请教(关于打ClistCtrl中的内容)
<br>内容:如何打印clistctrl中的内容
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:Anu 回复日期:2003-04-07 11:37:20
<br>内容:{转贴http://kahn.xj.cninfo.net/lffym/VC/vc2.htm]<BR>如何在VC++中使用API直接打印 <BR><BR>(说明:)这一段代码,用以演示《如何在VC++中使用API直接打印》。并且该段代码可以直接嵌入各种工程中,有实际使用的价值。<BR><BR>(用途:)在Visual C++中,应用程序通常是使用CView中提供的打印功能,在OnPrint()或OnDraw()中向打印机输出。但是对于对话框中的数据,或基于对话框的程序,打印成了一件繁琐的工作。<BR><BR>该段代码向用户提供了PrintListCtrl()函数,用于打印用户在对话框或FormView中的CListCtrl(控件必须是Report View 形式的)控件中的内容。在打印过程中,根据控件中每列标题的宽度计算打印输出时各列的宽度,并根据数据的行数自动分页。在本代码的基础上稍作修改,就可以适应各种数据的输出。<BR><BR>(用法:)该段代码使用Visual C++ 6.0, 使用Windows API来完成所需功能,使用时将本文本作为头文件使用。打印时直接调用PrintListCtrl(),函数的参数为所要打印的ListCtrl。?联系方法:lff@mail.wl.xj.cn<BR><BR>*///该结构用于存储各列的信息<BR>typedef struct tagColAtt<BR>{<BR>int nColIndex;<BR>CString strColText;<BR>int nPrintX;<BR>int nSubItemIndex;<BR>}<BR>COLATT;<BR><BR>BOOL PrintListCtrl(CListCtrl &list)<BR>{<BR>PRINTDLG pd;<BR>pd.lStructSize = sizeof(PRINTDLG);<BR>pd.Flags = PD_RETURNDC;<BR>pd.hDC = NULL;<BR>pd.hwndOwner = NULL;<BR>pd.hInstance = NULL;<BR>pd.nMaxPage = 1;<BR>pd.nMinPage = 1;<BR>pd.nFromPage = 1;<BR>pd.nToPage = 1;<BR>pd.nCopies = 1;<BR>pd.hDevMode = NULL;<BR>pd.hDevNames = NULL;<BR>//显示打印对话框,由用户来设定纸张大小等。<BR>if(!PrintDlg(&pd)) return FALSE;<BR>ASSERT(pd.hDC!=NULL);<BR>int nHorRes = GetDeviceCaps(pd.hDC, HORZRES);<BR>int nVerRes = GetDeviceCaps(pd.hDC, VERTRES);<BR>int nXMargin = 2;<BR>int nYMargin = 2;<BR>TEXTMETRIC tm;<BR>GetTextMetrics(pd.hDC, &tm);<BR>int nCharHeight = tm.tmHeight;<BR>int nCharWidth = tm.tmAveCharWidth;<BR>CHeaderCtrl* pHeader = list.GetHeaderCtrl();<BR>//获得行,列的个数<BR>int nColCount = pHeader->GetItemCount();<BR>int nLineCount = list.GetItemCount();<BR>int ColOrderArray[100];<BR>COLATT ca[100];<BR>list.GetColumnOrderArray(ColOrderArray, nColCount);<BR>int nColX =nXMargin*nCharWidth;<BR>//检索各列的信息,确定列标题的内容长度。<BR>for(int i =0 ; i< nColCount; i++)<BR>{<BR>ca[i].nColIndex = ColOrderArray[i];<BR>LVCOLUMN lvc;<BR>char text[100];<BR>lvc.mask = LVCF_TEXT|LVCF_SUBITEM;<BR>lvc.pszText = text;<BR>lvc.cchTextMax = 100;<BR>list.GetColumn(ca[i].nColIndex, &lvc);<BR>ca[i].strColText = lvc.pszText;<BR>ca[i].nSubItemIndex = lvc.iSubItem;<BR>ca[i].nPrintX = nColX;<BR>nColX += nCharWidth * strlen(ca[i].strColText);<BR>if(nColX > nHorRes)<BR>{<BR>DeleteDC(pd.hDC);<BR>AfxMessageBox("字段太多,无法在一行内打印,请试用较大的纸,或横向打印。");<BR>return FALSE;<BR>}<BR>}<BR>DOCINFO di;<BR>di.cbSize = sizeof(DOCINFO);<BR>di.lpszDocName = "ListCtrl Data Printing";<BR>di.lpszOutput = (LPTSTR) NULL;<BR>di.lpszDatatype = (LPTSTR) NULL;<BR>di.fwType = 0;<BR>StartDoc(pd.hDC, &di);<BR>StartPage(pd.hDC);<BR>//调整各列的宽度,以使各列在后面的打印输出时更均匀的打印在纸上。<BR>int space = (nHorRes-nXMargin*nCharWidth-nColX) / (nColCount -1);<BR>for(i =1; i<nColCount; i++)<BR>{<BR>ca[i].nPrintX += i*space;<BR>}<BR>//输出列标题<BR>for(i =0; i<nColCount; i++)<BR>TextOut(pd.hDC, ca[i].nPrintX, nYMargin,<BR>ca[i].strColText, strlen(ca[i].strColText));<BR>int nMaxLinePerPage = nVerRes/nCharHeight -3;<BR>int nCurPage =1;<BR>//输出各列的数据<BR>for(i =0; i<nLineCount; i++)<BR>{<BR>for(int j =0; j<nColCount; j++)<BR>{<BR>if(i+1-(nCurPage-1)*nMaxLinePerPage > nMaxLinePerPage)<BR>{<BR>//新的一页<BR>EndPage(pd.hDC);<BR>StartPage(pd.hDC);<BR>nCurPage ++;<BR>}<BR>CString subitem = list.GetItemText(i, ca[j].nSubItemIndex);<BR>TextOut(pd.hDC, ca[j].nPrintX,nYMargin+(i+1-(nCurPage-1)*nMaxLinePerPage)*nCharHeight,subitem, strlen(subitem));<BR>}<BR>}<BR>EndPage(pd.hDC);<BR>EndDoc(pd.hDC);<BR>//打印结束<BR>DeleteDC(pd.hDC);<BR>return TRUE;<BR><BR>
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -