<HTML>
<HEAD>
<TITLE>JavaScript利用IE内置打印控件IEWebBrowser进行打印/打印页面设置/打印预览/控制分页打印</TITLE>
<META http-equiv=Content-Type content="text/html; charset=utf-8" />
<SCRIPT type="text/javascript">
var hkey_root,hkey_path,hkey_key;
hkey_root="HKEY_CURRENT_USER";
<!--地址的写法很严格的用双斜杠-->
hkey_path="\\Software\\Microsoft\\Internet Explorer\\PageSetup";
hkey_MainPath="\\Software\\Microsoft\\Internet Explorer\\Main";
hkey_BackgroundKey="\\Print_Background"; //设置打印背景色 值为 yes或no
//设置网页打印的页眉页脚为空
function pagesetup_null(){
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,"");
//不打印页面背景
RegWsh.RegWrite(hkey_root+hkey_MainPath+hkey_BackgroundKey,"no");
}catch(e){}
}
//设置网页打印的页眉页脚为默认值
function pagesetup_default(){
try{
var RegWsh = new ActiveXObject("WScript.Shell");
hkey_key="\\header" ;
RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"&w&b,&p/&P");
hkey_key="\\footer";
RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"&u&b&d");
//打印页面背景
RegWsh.RegWrite(hkey_root+hkey_MainPath+hkey_BackgroundKey,"yes");
}catch(e){}
}
function printsetup(){
wb.execwb(8,1); // 打印页面设置
}
function printpreview(){
wb.execwb(7,1);// 打印页面预览
}
function printit() {
if (confirm('确定打印吗?')) {
wb.execwb(6,1);
}
}
</SCRIPT>
<!--media=print 这个属性说明可以在打印时有效-->
<!--希望打印时不显示的内容设置class="Noprint"样式-->
<!--希望人为设置分页的位置设置class="PageNext"样式-->
<style media="print">
<!--
.Noprint{display:none;}
.PageNext{page-break-after:always;}
-->
</style>
<style type="text/css">
<!--
.STYLE1 {font-size: 12px}
-->
</style>
</HEAD>
<BODY>
<!--IE内置打印控件IEWebBrowser-->
<OBJECT id=wb height=0 width=0 classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 name=wb></OBJECT>
<form>
<table width="600" border="1" align="center" cellpadding="0" cellspacing="0" class="STYLE1">
<tr>
<td height="17" colspan="3" bgcolor="#0099CC">>>监控信息>></td>
</tr>
<tr>
<td height="27" align="center" bgcolor="#C6C4DF">序号</td>
<td align="center" bgcolor="#C6C4DF">学校代码</td>
<td align="center" bgcolor="#C6C4DF">设备状态</td>
</tr>
<tr>
<td height="17" align="center">1</td>
<td height="17" align="center">1001</td>
<td height="17" align="center">正常</td>
</tr>
<tr class="PageNext"><!--希望人为设置分页的位置设置class="PageNext"样式-->
<td height="17" align="center">2</td>
<td height="17" align="center">1002</td>
<td height="17" align="center">正常</td>
</tr>
<tr>
<td height="17" align="center">3</td>
<td height="17" align="center">1003</td>
<td height="17" align="center">正常</td>
</tr>
</table>
<DIV align=center>
<!--希望打印时不显示的内容设置class="Noprint"样式-->
<INPUT onclick=javascript:printit() type=button value=打印 name=button_print class="Noprint"/>
<INPUT onclick=javascript:printsetup(); type=button value=打印页面设置 name=button_setup class="Noprint" />
<INPUT onclick=javascript:printpreview(); type=button value=打印预览 name=button_show class="Noprint" />
<input type="button" value="清空页码" onclick=javascript:pagesetup_null() class="Noprint">
<input type="button" value="恢复页码" onclick=javascript:pagesetup_default() class="Noprint">
</DIV>
</form>
</BODY>
</HTML>
该Object "wb" 其实就是IE内置打印控件IEWebBrowser,设定 Width 和 Height 为0,在界面上就不显示控件的形状。关于这个组件还有其它用法,列举如下:
WebBrowser.ExecWB(1,1) 打开
Web.ExecWB(2,1) 关闭现在所有的IE窗口,并打开一个新窗口
Web.ExecWB(4,1) 保存网页
Web.ExecWB(6,1) 打印
Web.ExecWB(6,6) 直接打印
Web.ExecWB(7,1) 打印预览
Web.ExecWB(8,1) 打印页面设置
Web.ExecWB(10,1) 查看页面属性
Web.ExecWB(17,1) 全选
Web.ExecWB(22,1) 刷新
Web.ExecWB(45,1) 关闭窗体无提示
代码示例:2
<OBJECT classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 id=WebBrowser width=0></OBJECT>
<input name=Button onClick=document.all.WebBrowser.ExecWB(1,1) type=button value=打开>
<input name=Button onClick=document.all.WebBrowser.ExecWB(2,1) type=button value=关闭所有>
<input name=Button onClick=document.all.WebBrowser.ExecWB(4,1) type=button value=另存为>
<input name=Button onClick=document.all.WebBrowser.ExecWB(6,1) type=button value=打印>
<input name=Button onClick=document.all.WebBrowser.ExecWB(6,6) type=button value=直接打印>
<input name=Button onClick=document.all.WebBrowser.ExecWB(7,1) type=button value=打印预览>
<input name=Button onClick=document.all.WebBrowser.ExecWB(8,1) type=button value=页面设置>
<input name=Button onClick=document.all.WebBrowser.ExecWB(10,1) type=button value=属性>
<input name=Button onClick=document.all.WebBrowser.ExecWB(17,1) type=button value=全选>
<input name=Button onClick=document.all.WebBrowser.ExecWB(22,1) type=button value=刷新>
<input name=Button onClick=document.all.WebBrowser.ExecWB(45,1) type=button value=关闭>
<xmp>
关键字 IEWebBrowser﹑execWB﹑打印
IEWebBrowser这个组件的execWB方法整理
1. <object id="WebBrowser" width=0 height=0 classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></object>
2. 调用方法。
WebBrowser.ExecWB nCmdID, nCmdExecOpt, [pvaIn], [pvaOut]
3. 参数说明。
(a).nCmdID
OLECMDID_OPEN = 1,
OLECMDID_NEW = 2,
OLECMDID_SAVE = 3,
OLECMDID_SAVEAS = 4,
OLECMDID_SAVECOPYAS = 5,
OLECMDID_PRINT = 6,
OLECMDID_PRINTPREVIEW = 7,
OLECMDID_PAGESETUP = 8,
OLECMDID_SPELL = 9,
OLECMDID_PROPERTIES = 10,
OLECMDID_CUT = 11,
OLECMDID_COPY = 12,
OLECMDID_PASTE = 13,
OLECMDID_PASTESPECIAL = 14,
OLECMDID_UNDO = 15,
OLECMDID_REDO = 16,
OLECMDID_SELECTALL = 17,
OLECMDID_CLEARSELECTION = 18,
OLECMDID_ZOOM = 19,
OLECMDID_GETZOOMRANGE = 20
OLECMDID_UPDATECOMMANDS = 21
OLECMDID_REFRESH = 22
OLECMDID_STOP = 23
OLECMDID_HIDETOOLBARS = 24
OLECMDID_SETPROGRESSMAX = 25
OLECMDID_SETPROGRESSPOS = 26
OLECMDID_SETPROGRESSTEXT = 27
OLECMDID_SETTITLE = 28
OLECMDID_SETDOWNLOADSTATE = 29
OLECMDID_STOPDOWNLOAD = 30
上面的关键词都可以在浏览器的菜单里面找到对应的选项﹐大家一看就明白的﹗
(b).nCmdExecOpt
OLECMDEXECOPT_DODEFAULT = 0,
OLECMDEXECOPT_PROMPTUSER = 1,
LECMDEXECOPT_DONTPROMPTUSER = 2,
OLECMDEXECOPT_SHOWHELP = 3
对于这个参数﹐一般来说﹐选1就可以了。
这是调用IE的”另存为”功能的示例﹕
<object id="WebBrowser" width=0 height=0 classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></object>
<A href="javascript:WebBrowser.ExecWB(4,1);">Save-存储</A>
</xmp>
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/yuhun23/archive/2008/03/13/2175813.aspx