⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 0515001.htm

📁 VC知识库5_chm_decompile_20040520_210715
💻 HTM
字号:
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title></title>
<link rel="stylesheet" type="text/css" href="../../vckbase.css">
</head>

<body>

<div align="justify">
  <table border="0" width="100%" class="font" height="57">
    <tr>
      <td width="27%" height="6" class="bigfont" bgcolor="#B8CFE7" align="center" bordercolor="#800080">
      <font color="#800080">VC知识库(五)</font>
      </td>
      <td width="73%" height="6" class="bigfont" bgcolor="#B8CFE7" align="center" bordercolor="#800080">
      <font color="#800080">www.vckbase.com</font>
      </td>
    </tr>
    <tr>
      <td width="100%" height="4" class="header" valign="top" align="center" colspan="2">
      <hr>
      </td>
    </tr>
    <tr>
      <td width="100%" height="17" class="header" valign="top" align="center" colspan="2">
      直接打印,不需要文档/视
      </td>
    </tr>
    <tr>
      <td width="100%" height="17" class="info" align="center" colspan="2">
      <small align="center">闻怡洋 译  
 
 
 
<a href="mailto:wyy_cq@188.net">wyy_cq@188.net</a> 
 
<a href="http://vchelp.163.net">http://vchelp.163.net</a> </small> 
      </td>   
    </tr>   
    <tr>  
      <td width="100%" height="22" class="font" colspan="2"> 
        <hr> 
      </td>   
    </tr>  
    <tr>  
      <td width="100%" height="5" class="font" colspan="2">  
 
 
 
<p><small><font face="宋体">This article was contributed by </font><a 
 
href="mailto:chrismaunder@codeguru.com"><font face="宋体">Chris 
 
Maunder</font></a><font face="宋体">. </font></small> 
 
</p><p> 
 
<small><font face="宋体">如果你需要在没有使用文档/视图的应用中使用打印功能(比如说在对话框中)你将无法利用MFC提供的功能,而且对于某些新手来讲有些困难。</font></small> 
 
 
 
<small><font face="宋体">下面的的代码可以供你在对话框中如果在DOC/View中一样使用打印功能。你必须定义OnBeginPrinting, 
 
OnEndPrinting 和 OnPrint三个函数,其原型和功能与在VIEW中一样。</font></small> 
 
</p><p> 
 
<font face="宋体" size="3"><tt><font color="#0000FF">void</font><font

color="#990000" face="宋体"> CMyDialog::Print() <br> 
 
{<br> 
 
CDC dc;<br> 
 
CPrintDialog printDlg(FALSE);<br> 
 
//利用CPrintDialog 生成打印机设备环境<br> 
 
</font><font color="#0000FF" face="宋体">if</font><font 
 
color="#990000" face="宋体"> (printDlg.DoModal() == 
 
IDCANCEL) </font><font color="#009900" face="宋体">// 
 
Get printer settings from user</font><font color="#990000" 
 
face="宋体"> 让用户选择打印纸张等<br> 
 
</font><font color="#0000FF" face="宋体">return</font><font 
 
color="#990000" face="宋体">;<br> 
 
<br> 
 
dc.Attach(printDlg.GetPrinterDC()); </font><font

color="#009900" face="宋体">// Attach a printer DC</font><font 
 
color="#990000" face="宋体"> 让HANDLE连接到dc上<br> 
 
</font><font color="#0000FF" face="宋体">dc.m_bPrinting 
 
= TRUE;<br> 
 
<br> 
 
CString strTitle; // Get the application title ?<br> 
 
strTitle.LoadString(AFX_IDS_APP_TITLE);<br> 
 
<br> 
 
DOCINFO di; // Initialise print document details DOCINFO中有相关的打印信息<br> 
 
::ZeroMemory (&amp;di, sizeof (DOCINFO));<br> 
 
di.cbSize = sizeof (DOCINFO);<br> 
 
di.lpszDocName = strTitle;//设置标题<br> 
 
<br> 
 
BOOL bPrintingOK = dc.StartDoc(&amp;di); // Begin a new print job 
 
开始打印<br> 
 
<br> 
 
// Get the printing extents and store in the m_rectDraw field of 
 
a <br> 
 
// CPrintInfo object<br> 
 
CPrintInfo Info;//<br> 
 
Info.m_rectDraw.SetRect(0,0, <br> 
 
dc.GetDeviceCaps(HORZRES), <br> 
 
dc.GetDeviceCaps(VERTRES));//设置范围<br> 
 
<br> 
 
</font><font color="#990000" face="宋体">OnBeginPrinting(&amp;dc, 
 
&amp;Info); </font><font color="#009900" face="宋体">// 
 
调用你自己定义的初始化功能</font><font 
 
color="#990000" face="宋体"><br> 
 
</font><font color="#0000FF" face="宋体">for</font><font 
 
color="#990000" face="宋体"> (UINT page = 
 
Info.GetMinPage(); page&lt;Info.GetMaxPage()&amp;&amp; 
 
bPrintOK;page++)</font><br> 
 
{Info.m_nCurPage = page;<br> 
 
OnPrint(&amp;dc, &amp;Info); <font color="#009900"

face="宋体">// Call your &quot;Print page&quot; function</font><br> 
 
bPrintingOK = (dc.EndPage() &gt; 0); <font

color="#009900" face="宋体">// end page</font><br> 
 
}<br> 
 
OnEndPrinting(&amp;dc, &amp;Info); <font

color="#009900" face="宋体">// 结束打印</font><br> 
 
<br> 
 
<font color="#0000FF" face="宋体">if</font> (bPrintingOK)<br> 
 
dc.EndDoc(); <font color="#009900" face="宋体">// 
 
end a print job</font><br> 
 
<font color="#0000FF" face="宋体">else</font><br> 
 
dc.AbortDoc(); <font color="#009900" face="宋体">// 
 
abort job.</font><br> 
 
<br> 
 
dc.Detach(); <font color="#009900" face="宋体">// 
 
detach the printer DC</font><br> 
 
}</tt></font> 
 
 
 
</p> 
 
<p> 
 
<small><font face="宋体">[译者:其实在WINDOWS环境中是设备无关的。只要有了DC,你可以使用各种GDI函数,而不需要理会是在屏幕或是在打印机上绘图。在Windows3.X一般使用CreateDC创建打印环境,在WIN32下好象并不是很兼容,使用CPrintDialog产生打印DC是个不错的方法。你只要看看MFC的源代码就能搞清楚PrintDialog是怎么产生DC的了。</font></small> 
 
 
 
<small><font face="宋体">蓝色的代码是讲诉如何初始化打印的参数,而其他的参数你可以在OnBeginPrint中进行设置]</font></small> 
 
 
 
</p> 
 
      </td>     
    </tr>    
    <tr> 
      <td width="100%" height="12" class="font" colspan="2">  
      </td>     
    </tr> 
    <tr> 
      <td width="100%" height="6" class="font" colspan="2">  
      </td>     
    </tr> 
    <tr> 
      <td width="100%" height="8" class="font" colspan="2">  
      </td>     
    </tr> 
    <tr>    
      <td width="100%" height="17" class="font" colspan="2"></td>     
    </tr>    
  </table>     
</div>     
     
</body>     
     
</html>     

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -