1. 程式人生 > >IE瀏覽器自帶列印控制元件WebBrowser

IE瀏覽器自帶列印控制元件WebBrowser

使用方式:

1.在html中的body標籤中插入:

<body onload="body_onload();">
    <form id="form1" runat="server">
        <object id="WebBrowser" classid="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2" height="0" width="0">
        </object>
        <table id="tblbtn" class="notDisplay" runat="server" style
="margin-left:200px;">
<!--不列印的內容--> <tr height="24" align="right"> <td align="left"> <input type="button" id="btnprint2" value="列印" onclick="document.all.WebBrowser.ExecWB(6, 1)" />&nbsp; <input type="button"
id="btnprint" value="直接列印" onclick="document.all.WebBrowser.ExecWB(6, 6)" />
&nbsp; <input type="button" id="btnSetprinter" value="頁面設定" onclick="document.all.WebBrowser.ExecWB(8, 1)" />&nbsp; <input type="button" id="btnpreview" value="列印預覽"
onclick="document.all.WebBrowser.ExecWB(7, 1)" />
&nbsp; <input type="button" id="exit" value="退出" onclick="javascript: window.close();" /> </td> </tr> </table> <div style="width:100%" id="divNeiRong" runat="server"></div><!--列印的內容--> </form> </body>

2.css列印專用樣式:

<style media="print" type="text/css">
       .notDisplay{  
           display:none;   
       }  
       .PageNext{
           page-break-after: always;
       }  
</style> 

3.執行js函式:

//修改登錄檔上下左右邊距,清空頁首,清空頁尾
function PageSetup_Null(mtop,mbottom,mleft,mright) 
{
    //注意登錄檔裡的單位是英寸,列印設定中是毫米,1英寸=25.4毫米
    mtop=""+parseFloat(mtop)/25.4;
    mbottom=""+parseFloat(mbottom)/25.4;
    mleft=""+parseFloat(mleft)/25.4;
    mright=""+parseFloat(mright)/25.4;
    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="margin_left" ;
        RegWsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,mleft);  //左邊邊界

        HKEY_Key="margin_right" ;
        RegWsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,mright); //右邊邊界

        HKEY_Key="margin_top" ;
        RegWsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,mtop);//上邊邊界

        HKEY_Key="margin_bottom" ;
        RegWsh.RegWrite(HKEY_Root+HKEY_Path+HKEY_Key,mbottom); //下邊邊界

        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 body_onload()
{
    PageSetup_Null(8,0,8,8);//毫米
}