自動開新視窗、列印與關閉視窗

  • 1092
  • 0

在ASP.Net使用JavaScript與呼叫WebBrowser的ExecWB方法,實現自動開啟視窗、自動列印與自動關閉視窗的功能。

 

自動開啟視窗:

protected void AutoPrintButton_Click(object sender, EventArgs e)
{
    ClientScriptManager clientScriptManager = Page.ClientScript;
    string scriptText = "window.open('Print.aspx');";
    clientScriptManager.RegisterClientScriptBlock(this.GetType(), "MyScript", scriptText, true);
}

自動列印與自動關閉視窗:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using HtmlAgilityPack;
using Helper.WebBrowser.ExecWB;
using System.IO;

namespace AutoPrintTest
{
    public partial class Print : System.Web.UI.Page
    {
        protected void Page_Init(object sender, EventArgs e)
        {

            Response.Clear();
            Response.ContentType = "text/HTML";
            Response.BufferOutput = true;
            string strFileName = "HtmlPageTest.html";
            this.InsertTag(strFileName);
            Response.Flush();
        }

        protected void InsertTag(string strFileName)
        {

            HtmlDocument hDoc = new HtmlDocument();
            hDoc.Load(Server.MapPath(@"./App_Data/" + strFileName));

            HtmlNode hNode = hDoc.DocumentNode.SelectSingleNode("//body");

            string strValue = string.Empty;
            //strValue += "<JavaScript>myFunction(){";
            strValue += this.InsertWebBrowserCommand(Command.Print, Prompt.DontPromptUser);

            //JavaScript dialogArguments._IE_PrintType SCRIPT5002 error in IE11 on Windows 7
            //http://stackoverflow.com/questions/28207854/javascript-dialogarguments-ie-printtype-script5002-error-in-ie11-on-windows-7
            strValue += "window.onfocus=function(){";
            strValue += this.InsertWebBrowserCommand(Command.Close, Prompt.DontPromptUser);
            strValue += "}";
            //strValue += "}</JavaScript>";

            hNode.SetAttributeValue("onLoad", strValue);

            HtmlNode hNode2 = HtmlNode.CreateNode("<object id='WebBrowser' width = 0 height = 0 classid ='CLSID:8856F961-340A-11D0-A96B-00C04FD705A2'></object>");
            hNode.AppendChild(hNode2);

            hDoc.Save(Response.OutputStream);

        }

        protected string InsertWebBrowserCommand(Command cmd, Prompt prm)
        {
            return "WebBrowser.ExecWB(" + (int)cmd + "," + (int)prm + ");";
        }
    }
}

Helper命名空間:

namespace Helper
{    
    namespace WebBrowser
    {
        namespace ExecWB
        {
            public enum Command
            {
                Print = 6,
                Close = 45 
            }
            public enum Prompt
            {
                DoDefault = 0,
                PromptUser = 1,
                DontPromptUser = 2,
                ShowHelp = 3
            }
        }
    }
}

 

說明:
當透過Body標籤的OnLoad事件下達列印的指令後,由於需要一段時間完成列印,此時若接著下達關閉視窗的指令,將會產生執行時期錯誤,所以必須將下達關閉視窗的指令包裝在下列的程式碼片段當中:

 

strValue += "window.onfocus=function(){";
            strValue += this.InsertWebBrowserCommand(Command.Close, Prompt.DontPromptUser);
            strValue += "}";

參考資料:
[1]JavaScript dialogArguments._IE_PrintType SCRIPT5002 error in IE11 on Windows 7
http://stackoverflow.com/questions/28207854/javascript-dialogarguments-ie-printtype-script5002-error-in-ie11-on-windows-7

[2]ExecWB method
https://msdn.microsoft.com/en-us/library/aa752087(v=vs.85).aspx

範例程式碼下載網址:
https://1drv.ms/u/s!AtXzrT02xf2jhHs81_ySIajCMHLm