ScriptX打印控件的使用
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<SCRIPT>
var hkey_root,hkey_path,hkey_key
hkey_root="HKEY_CURRENT_USER"
hkey_path="\\Software\\Microsoft\\Internet Explorer\\PageSetup\\"
//设置网页打印的页眉页脚为空
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,"")
}catch(e){}
}
//用于设置打印参数
function printBase() {
factory.printing.header = "" //页眉
factory.printing.footer = "" //页脚
factory.printing.portrait = true //true为纵向打印,false为横向打印
factory.printing.leftMargin = 0.5
factory.printing.topMargin = 1.5
factory.printing.rightMargin = 0.5
factory.printing.bottomMargin = 1.5
}
//用于调用设置打印参数的方法和显示预览界面
function printReport(){
printBase();
pagesetup_null();
document.all("button").style.display = "none";//隐藏按钮
factory.printing.Preview();
}
//使界面最大化
maxWin();
function maxWin()
{
var aw = screen.availWidth;
var ah = screen.availHeight;
window.moveTo(0, 0);
window.resizeTo(aw, ah);
}
function printTure()
{
printBase();
document.all("button").style.display = "none";//隐藏按钮
//factory.printing.Preview();
factory.printing.Print(false);
document.all("button").style.display = "block";//显示按钮
}
</SCRIPT>
</HEAD>
<BODY>
<OBJECT id="factory" codeBase="ScriptX.cab#Version=6,3,434,26"
height="0" width="0" classid="clsid:1663ed61-23eb-11d2-b92f-008048fdd814" viewastext>
</OBJECT>
<div id="button" style="PADDING-RIGHT: 0px; PADDING-LEFT: 0px; PADDING-BOTTOM: 0px; MARGIN: 0px; WIDTH: 98%; PADDING-TOP: 0px">
<table height="15" cellSpacing="1" cellPadding="4" width="100%" bgColor="#ddf3ff" border="0">
<tr>
<td align="center" bgColor="#ddf3ff">
<input class="bginput" onclick="printTure()" type="button" value="打印">
<input id="idPrint2" type="button" value="页面设置" onclick="factory.printing.PageSetup()">
<input id="idPrint3" type="button" value="打印预览"
onclick="factory.printing.Preview()">
</td>
</tr>
</table>
</div>
</BODY>
</HTML>
===============================介绍B===================================
1.需先下载smsx.exe插件进行安装
2.将如下代码拷贝到一个scriptXprint.js文件中保存:
function setPrintBase(headerText,footerText,rootUrl) {
// -- advanced features
//factory.printing.SetMarginMeasure(2); // measure margins in inches
//factory.SetPageRange(false, 1, 3);// need pages from 1 to 3
//factory.printing.printer = "HP DeskJet 870C";
//factory.printing.copies = 2;
//factory.printing.collate = true;
//factory.printing.paperSize = "A4";
//factory.printing.paperSource = "Manual feed"
var header = (headerText==null||headerText=="")?'':'';
var footer = (footerText==null||footerText=="")?'':'';
factory.printing.header = "&b"+header+"&b" ;
factory.printing.footer = "&b"+footer;
factory.printing.portrait = true;
factory.printing.leftMargin =10.00;
factory.printing.topMargin =10.00;
factory.printing.rightMargin =10.00;
factory.printing.bottomMargin =10.00;
}
3.在需要打印的页面的head标签中加入如下代码:
<script language="javascript" src="scriptXprint.js"></script> <!-- 引入scriptXprint.js文件-->
<style media="print">
.Noprint {DISPLAY: none;} <!--设置不需要被打印的元素的样式-->
</style>
<script defer>
function window.onload() {
setPrintBase('ҳü','Ò³½Å'); <!-- 当页面加载时,设置页眉页脚需显示的内容-->
}
</script>
4.在head标签与body标签之间加入如下代码,以启动scripX.cab控件:
<OBJECT id="factory" style="DISPLAY: none" codeBase="smsx.cab#VVersion=6,3,435,20" classid="clsid:1663ed61-23eb-11d2-b92f-008048fdd814" viewastext></OBJECT>
5.用class="Noprint"来设置不需要被显示在打印效果中的元素的样式
6.在某个按钮或链接的单击事件上调用如下相应的函数即可:
<input type=button value="´òÓ¡" οnclick="factory.printing.Print(true)">
<input type=button value="Ò³ÃæÉèÖÃ" οnclick="factory.printing.PageSetup()">
<input type=button value="´òÓ¡Ô¤ÀÀ" οnclick="factory.printing.Preview()">
<input type="button" value="¹Ø±Õ" οnclick="window.close();">