WebBrowser控件 WebBrowser ActiveX 控件提供托管的包装。 托管的包装便可以在 Windows 窗体客户端应用程序中显示网页。 你可以使用WebBrowser控件重复中你的应用程序或你的 Internet Explorer Web 浏览功能可以禁用默认 Internet 资源管理器功能和控件用作简单的 HTML 文档查看器。 你可以使用控件以将基于 DHTML 的用户界面元素添加到你的窗体和隐藏它们是否承载在事实WebBrowser控件。 这种方法,可以无缝组合与单个应用程序中的 Windows 窗体控件的 Web 控件。
1.调用 WebBrowser 控件代码,其中style="display: none;" 是因为打印时如果没隐藏会出现空白,将下面代码放入body下
<OBJECT id=WebBrowser style="display: none;" classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 width=0 ></OBJECT>
2.设置打印页眉页脚边距等等参数
//设置打印页眉页脚等
function pageSetup() {
var hkey_root, hkey_path, hkey_key;
hkey_root = "HKEY_CURRENT_USER";
hkey_path = "\\Software\\Microsoft\\Internet Explorer\\PageSetup\\";
try {
var RegWsh = new ActiveXObject("WScript.Shell");
hkey_key = "header"; //页眉
RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "");
hkey_key = "footer"; //页脚
RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "");
hkey_key = "margin_left"; //左边距
RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "0");
hkey_key = "margin_bottom"; //下边距
RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "0");
hkey_key = "margin_top"; //上边距
RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "0");
hkey_key = "margin_right"; //右边距
RegWsh.RegWrite(hkey_root + hkey_path + hkey_key, "0");
} catch (e) {
}
}
3.打印选定部分设置CSS
<style media="screen">
.noVisable{display:none;}
</style>
<style media="print">
.noPrint{display:none;}
</style>
4.使用CSS方法
//页面不显示-打印显示:class='noVisable' , 如果想又显示又打印,去掉CLASS属性即可
<center class='noVisable'>
<input type=button class='noPrint' value=打印
onclick=document.all.WebBrowser.ExecWB(6,1)>
<input type=button class='noPrint' value=直接打印
onclick=document.all.WebBrowser.ExecWB(6,6) />
<input type=button class='noPrint' value=页面设置
onclick=document.all.WebBrowser.ExecWB(8,1) />
<input type=button class='noPrint' value=打印预览
onclick=document.all.WebBrowser.ExecWB(7,1) />
</center>
//页面显示-打印隐藏:class='noPrint'
<center class='noPrint'>
<input type=button class='noPrint' value=打印(选择打印机)
onclick=document.all.WebBrowser.ExecWB(6,1)>
<input type=button class='noPrint' value=直接打印
onclick=document.all.WebBrowser.ExecWB(6,6) />
<input type=button class='noPrint' value=页面设置
onclick=document.all.WebBrowser.ExecWB(8,1) />
<input type=button class='noPrint' value=打印预览
onclick=document.all.WebBrowser.ExecWB(7,1) />
</center>
5.分页打印,加入以下标签。 或者写成CSS引用,方法很多看自己喜欢
<p style='page-break-before:always;'>