printing - WebBrowser 控件 - 控制台应用程序 - 事件未触发

标签 printing browser webbrowser-control console-application

我一直在浏览各种WebBrowser control stackoverflow questions ,而且我似乎无法找到我遇到的问题的答案。我正在尝试使用 WebBrowser control to print a web page 。正在关注MSDN's example ,我创建了以下控制台应用程序:

namespace WebPrintingMadness
{
    using System;
    using System.Collections.Generic;
    using System.Text;

    /// <summary>
    /// The entry point of the program.
    /// </summary>
    class Program
    {
        /// <summary>
        /// The main entry point of the program.
        /// </summary>
        /// <param name="args">Program arguments.</param>
        [STAThread]
        public static void Main(string[] args)
        {
            string url = "https://stackoverflow.com/";

            WebPagePrinter webPagePrinter = new WebPagePrinter();
            webPagePrinter.PrintWebPage(url);
            Console.ReadLine();
        }
    }
}



namespace WebPrintingMadness
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;

    /// <summary>
    /// This class is used to print a web page.
    /// </summary>
    internal class WebPagePrinter : IDisposable 
    {
        /// <summary>
        /// A System.Windows.Forms.WebBrowser control.
        /// </summary>
        private WebBrowser webBrowser;

        /// <summary>
        /// Initializes a new instance of the WebPagePrinter class.
        /// </summary>
        internal WebPagePrinter()
        {
            this.webBrowser = new WebBrowser();
            this.webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(this.WebBrowser_DocumentCompleted);
            this.webBrowser.ScriptErrorsSuppressed = true;
        }

        /// <summary>
        /// Disposes of this instance.
        /// </summary>
        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Prints a web page.
        /// </summary>
        /// <param name="url">The url of the web page.</param>
        internal void PrintWebPage(string url)
        {   
            this.webBrowser.Navigate(url);
        }

        /// <summary>
        /// Disposes of this instance.
        /// </summary>
        /// <param name="disposing">True if disposing, otherwise false.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (this.webBrowser != null)
                {
                    this.webBrowser.Dispose();
                    this.webBrowser = null;
                }
            }
        }

        /// <summary>
        /// Event handler for the webBrowser DocumentCompleted event.
        /// </summary>
        /// <param name="sender">The event sender.</param>
        /// <param name="e">The event arguments.</param>
        private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            WebBrowser navigated = sender as WebBrowser;

            if (navigated == null)
            {
                return;
            }

            navigated.Print();
            navigated.Dispose();
        }
    }
}

但是,DocumentCompleted 事件永远不会触发。是否可以在控制台应用程序中使用此 Windows.Forms 控件?

最佳答案

只要在运行时处理事件,它就会工作。

您可以简单地在等待循环中调用“Application.DoEvents()”。您不需要做任何比这更奇特的事情,它在我的控制台应用程序中运行良好。

关于printing - WebBrowser 控件 - 控制台应用程序 - 事件未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/687146/

相关文章:

node.js - 使用 ESC/POS 从 NodeJS 获取 Epson 热敏打印机状态

CSS @Media Print - 内联元素的水平间距

javascript - 仅在需要滚动条时绘制闪烁

当我执行 WWW-Authenticate 时,Android 没有注意到

jquery - 使用 jQuery 选择焦点文本在 Firefox 中不起作用

ios以预定义尺寸打印

PHP 和 MySQL - 打印与列值匹配的行

activex - 如何允许在 WebBrowser 控件中创建 ActiveX

c# - WinForms 浏览器控件和 IE 的消亡

.net - WebView2 控件中的打印功能