您的位置:首页 > 其它

使用window.showModalDialog弹出的模态窗口中打印功能的实现

2009-04-08 21:15 826 查看
由于网站模块开发的需要 遇到在弹出的模态窗口中实现打印功能的问题 把自己的解决方法记录如下:
方法一、使用<object>对象实现在弹出模态窗口后自动打印模态窗口中的内容
由于将<object>对象放在弹出的模态窗口中无法实现打印功能,所以将<object>对象放在弹出模态窗口的页面中。
parent.php:

<object id=WebBrowser name=WebBrowser classid=CLSID:8856F961-340A-11D0-A96B-00C04FD705A2 height=0 width=0></object>

<mce:script type="text/javascript"><!--
var webbrowser = window.document.getElementById("WebBrowser");
window.showModalDialog('model.php',webbrowser,'dialogTop=150px;dialogLeft=150px;resizable=yes;scroll=yes');
// --></mce:script>

model.php:

<mce:script language="javascript"><!--

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")

//接收showModalDialog传递过来的参数
var wb = window.dialogArguments

hkey_key="header"
RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"")
hkey_key="footer"
RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"")

//打印预览
//wb.ExecWB(7,1)

//打印
wb.ExecWB(6,7)

//关闭弹出窗口
//wb.ExecWB(45,1)
}catch(e){}
}

function pagesetup_default(){
try{
var RegWsh = new ActiveXObject("WScript.Shell")
hkey_key="header"
RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"&w&bPage,&p/&P")
hkey_key="footer"
RegWsh.RegWrite(hkey_root+hkey_path+hkey_key,"&u&b&d")
}catch(e){}
}
// --></mce:script>

上述代码可以实现打印功能,可是打印出的内容却是弹出模态窗口的页面的内容而不是模态窗口的内容。
一直没有找到解决的方法,还望有解决方法的朋友可以帮忙。

方法二、在弹出的模态窗口中添加打印按钮,点击打印按钮后改变按钮的css属性(为避免把按钮打印出来),使用window.print()来打印模态窗口中的内容。
parent.php:

<mce:script type="text/javascript"><!--
var obj = new Object();
window.showModalDialog('model.php',obj,'dialogTop=150px;dialogLeft=150px;resizable=yes;scroll=yes');
// --></mce:script>

model.php:

<mce:script type="text/javascript"><!--
function orderPrint(){
var print = document.getElementById("print");
print.style.display = "none";

window.print();
//window.close();
print.style.display = "inline";
}
// --></mce:script>

<mce:style><!--
.btnPrint {
height:26px;
width:160px;

font-family: Verdana, Arial, sans-serif;
font-size:16px;
font-weight:bold;
display:inline;
}
--></mce:style><style mce_bogus="1">.btnPrint {
height:26px;
width:160px;

font-family: Verdana, Arial, sans-serif;
font-size:16px;
font-weight:bold;
display:inline;
}</style>

<input class="btnPrint" id="print" name="print" type="button" value="print" onClick="orderPrint()">
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: