【问题标题】:WebBrowser Control - Console Application - Events Not FiringWebBrowser 控件 - 控制台应用程序 - 未触发事件
【发布时间】:2010-10-15 18:54:12
【问题描述】:

我一直在浏览各种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 控件?

【问题讨论】:

  • 看来jachymko 是对的,所以我要把它转换成一个“静默”的WinForms 应用程序。

标签: printing browser webbrowser-control console-application


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    STA 线程的基本要求是它需要运行消息泵。 在 Windows 窗体中,您可以使用 Application.Run。或者您可以使用 user32!GetMessage 和 DispatchMessage 手动编写消息泵。但在 WinForms 或 WPF 中使用它可能更容易。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-28
    • 2021-11-14
    • 2014-04-27
    • 1970-01-01
    • 2019-06-20
    相关资源
    最近更新 更多