代码之家  ›  专栏  ›  技术社区  ›  Aaron Daniels

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

  •  2
  • Aaron Daniels  · 技术社区  · 16 年前

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

    2 回复  |  直到 8 年前
        1
  •  2
  •   Mikey    15 年前

    只要在运行时处理事件,它就可以正常工作。

        2
  •  1
  •   user83286 user83286    16 年前

    STA线程的基本要求是需要运行消息泵。