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

发布于 2024-07-15 07:16:50 字数 3635 浏览 5 评论 0原文

我一直在浏览各种 WebBrowser 控制 stackoverflow 问题,但我似乎找不到问题的答案我有。 我正在尝试使用 WebBrowser 控件打印网页。 按照 MSDN 的示例,我创建了以下控制台应用程序:

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 控件?

I have been navigating the various WebBrowser control stackoverflow questions, and I can't seem to find an answer to a problem I am having. I am trying to use the WebBrowser control to print a web page. Following MSDN's example, I have created the following console application:

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();
        }
    }
}

However, the DocumentCompleted event never fires. Is it possible to use this Windows.Forms control in an console application?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七色彩虹 2024-07-22 07:16:50

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

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

It will work as long as process events while it's running.

You can simply call "Application.DoEvents()" in a wait loop. You don't need to do anything fancier than that, and it works fine in my console application.

遮了一弯 2024-07-22 07:16:50

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

The basic requirement of an STA thread is that it needs to run a message pump.
In Windows Forms, you can use Application.Run. Or you could write the message pump by hand, using user32!GetMessage & DispatchMessage. But it's probably easier to use the one in WinForms or WPF.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文