天天看點

IE調用WebBrowser控件實作WEB列印、分頁列印、無預覽列印

         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;'>