2009-03-26 21 views
2

私は様々なWebBrowser control stackoverflow questionsをナビゲートしていましたが、私が持っている問題の答えを見つけることができないようです。私はWebBrowser control to print a web pageを使用しようとしています。WebBrowserコントロール - コンソールアプリケーション - イベントが起動しない

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イベントは発動しません:MSDN's exampleに続いて、私は次のコンソールアプリケーションを作成しました。このWindows.Formsコントロールをコンソールアプリケーションで使用することは可能ですか?

+0

jachymkoが正しいと思われるので、これを「サイレント」WinFormsアプリケーションに変換します。 –

答えて

1

STAスレッドの基本的な要件は、メッセージポンプを実行する必要があることです。 Windowsフォームでは、Application.Runを使用できます。または、user32!GetMessage & DispatchMessageを使用して、手動でメッセージポンプを記述することもできます。しかし、おそらく、WinFormsまたはWPFでこれを使用するほうが簡単でしょう。

+0

私の質問hereにお答えすることは可能でしょうか? – bitcycle

2

実行中のプロセスイベントが発生している間は動作します。

待機ループでは単に "Application.DoEvents()"を呼び出すことができます。それ以上のものは何もする必要はなく、コンソールアプリケーションでうまく動作します。

関連する問題