WebBrowser クラス
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文
<ComVisibleAttribute(True)> _ <ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _ Public Class WebBrowser Inherits WebBrowserBase
[ComVisibleAttribute(true)] [ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)] public class WebBrowser : WebBrowserBase
[ComVisibleAttribute(true)] [ClassInterfaceAttribute(ClassInterfaceType::AutoDispatch)] public ref class WebBrowser : public WebBrowserBase
/** @attribute ComVisibleAttribute(true) */ /** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */ public class WebBrowser extends WebBrowserBase
ComVisibleAttribute(true) ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) public class WebBrowser extends WebBrowserBase
WebBrowser コントロールを使用すると、ブラウザで表示できるドキュメント (Web ページなど) を Windows フォーム アプリケーションでホストできます。たとえば WebBrowser コントロールを使用して、HTML ベースのユーザー支援機能や Web ブラウズ機能をアプリケーションに統合できます。さらに WebBrowser コントロールを使用して、既存の Web ベース コントロールを Windows フォーム クライアント アプリケーションに追加することもできます。
重要 : |
---|
WebBrowser は、リソースへの負荷の大きいコントロールです。コントロールの使用を終えたら、必ず Dispose メソッドを呼び出し、すべてのリソースを確実に解放するようにしてください。Dispose メソッドは、イベントを結びつけたスレッドと同じスレッドで呼び出す必要があります。イベントを結びつけるスレッドは、常にメッセージ スレッドまたはユーザー インターフェイス (UI) スレッドのいずれかになります。 |
WebBrowser コントロールは、信頼性が一部しか確認されていないコードでは使用できません。詳細については、「部分信頼コードからのライブラリの使用」を参照してください。
WebBrowser コントロールには、ナビゲーションに関する複数のプロパティ、メソッド、イベントがあります。次に示すメンバを使用して、コントロールを特定の URL に移動することや、ナビゲーション履歴リストの中を前後に移動すること、および現在のユーザーのホーム ページや検索ページを読み込むことができます。
ナビゲーションに失敗した場合、問題を示すページが表示されます。これらのいずれかのメンバを使用したナビゲーションにより、ナビゲーションのさまざまな段階で Navigating イベント、Navigated イベント、および DocumentCompleted イベントが発生します。
これらのメンバや、Stop メソッドや Refresh メソッドなどのメンバを利用して、Internet Explorer に似たユーザー インターフェイス コントロールをアプリケーションに実装できます。WebBrowser コントロールをフォームに表示しない場合でも、一部のメンバは役立ちます。たとえば、Print メソッドを使用すると、Web ページをユーザーに表示せずに最新の Web ページを印刷できます。
また WebBrowser コントロールを使用すると、アプリケーションで作成したコンテンツや、データベースまたはリソース ファイルから取得したコンテンツを表示できます。DocumentText プロパティまたは DocumentStream プロパティを使用すると、現在のドキュメントの内容を文字列またはデータ ストリームとして取得または設定できます。
また、Document プロパティを使用して Web ページのコンテンツを操作することもできます。このプロパティには、現在のページの HTML ドキュメント オブジェクト モデル (DOM: Document Object Model) へのマネージ アクセスを提供する HtmlDocument オブジェクトが格納されています。このプロパティを ObjectForScripting プロパティと組み合わせて使用し、アプリケーションのコードと Web ページのダイナミック HTML (DHTML) コードとの双方向通信を実装することで、Web ベースのコントロールと Windows フォーム コントロールを 1 つのユーザー インターフェイスに統合できます。Document プロパティを使用して、アプリケーションからスクリプト コードのメソッドを呼び出すことができます。スクリプト コードにより、window.external オブジェクトを介してアプリケーションにアクセスできます。このオブジェクトはホスト アクセス用に提供される組み込み DOM オブジェクトであり、ObjectForScripting プロパティで指定されたオブジェクトに割り当てられます。
メモ |
---|
このクラスによって、クラス レベルでのセキュリティ要求が行われます。派生クラスまたはコール スタック内の呼び出し元に、完全に信頼されたアクセス許可が設定されていない場合、SecurityException がスローされます。セキュリティ要求の詳細については、「リンク確認要求」および「継承確認要求」を参照してください。 |
メモ |
---|
WebBrowser クラスは、シングル スレッド アパートメント (STA: Single Thread Apartment) モードに設定されているスレッドでだけ使用できます。このクラスを使用するには、Main メソッドが確実に STAThreadAttribute 属性でマークされているようにします。 |
Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows CE プラットフォームメモ : .NET Compact Framework アプリケーションで WebBrowser の機能をすべて利用するには、Pocket PC および Smartphone 用 Windows Mobile Version 5.0 ソフトウェアが必要です。詳細については、「方法 : .NET Compact Framework で WebBrowser コントロールを使用する」を参照してください。
WebBrowser コントロールで使用するアドレス バーを実装する方法を次のコード例に示します。この例では、webBrowser1 という名前の WebBrowser コントロール、TextBoxAddress という名前の TextBox コントロール、および ButtonGo という名前の Button コントロールがフォームに含まれていることを前提としています。テキスト ボックスに URL を入力し、Enter キーを押すか [移動] ボタンをクリックすると、WebBrowser コントロールが、指定された URL に移動します。ハイパーリンクをクリックして移動した場合、テキスト ボックスが自動的に更新され、現在の URL が表示されます。
コード全体については、「方法 : Windows フォーム アプリケーションに Web ブラウザの機能を追加する」を参照してください。
' Navigates to the URL in the address box when ' the ENTER key is pressed while the ToolStripTextBox has focus. Private Sub toolStripTextBox1_KeyDown( _ ByVal sender As Object, ByVal e As KeyEventArgs) _ Handles toolStripTextBox1.KeyDown If (e.KeyCode = Keys.Enter) Then Navigate(toolStripTextBox1.Text) End If End Sub ' Navigates to the URL in the address box when ' the Go button is clicked. Private Sub goButton_Click( _ ByVal sender As Object, ByVal e As EventArgs) _ Handles goButton.Click Navigate(toolStripTextBox1.Text) End Sub ' Navigates to the given URL if it is valid. Private Sub Navigate(ByVal address As String) If String.IsNullOrEmpty(address) Then Return If address.Equals("about:blank") Then Return If Not address.StartsWith("http://") And _ Not address.StartsWith("https://") Then address = "http://" & address End If Try webBrowser1.Navigate(New Uri(address)) Catch ex As System.UriFormatException Return End Try End Sub ' Updates the URL in TextBoxAddress upon navigation. Private Sub webBrowser1_Navigated(ByVal sender As Object, _ ByVal e As WebBrowserNavigatedEventArgs) _ Handles webBrowser1.Navigated toolStripTextBox1.Text = webBrowser1.Url.ToString() End Sub
// Navigates to the URL in the address box when // the ENTER key is pressed while the ToolStripTextBox has focus. private void toolStripTextBox1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { Navigate(toolStripTextBox1.Text); } } // Navigates to the URL in the address box when // the Go button is clicked. private void goButton_Click(object sender, EventArgs e) { Navigate(toolStripTextBox1.Text); } // Navigates to the given URL if it is valid. private void Navigate(String address) { if (String.IsNullOrEmpty(address)) return; if (address.Equals("about:blank")) return; if (!address.StartsWith("http://") && !address.StartsWith("https://")) { address = "http://" + address; } try { webBrowser1.Navigate(new Uri(address)); } catch (System.UriFormatException) { return; } } // Updates the URL in TextBoxAddress upon navigation. private void webBrowser1_Navigated(object sender , WebBrowserNavigatedEventArgs e) { toolStripTextBox1.Text = webBrowser1.Url.ToString(); }
// Navigates to the URL in the address text box when // the ENTER key is pressed while the text box has focus. void TextBoxAddress_KeyDown( Object^ /*sender*/, System::Windows::Forms::KeyEventArgs^ e ) { if ( e->KeyCode == System::Windows::Forms::Keys::Enter && !this->TextBoxAddress->Text->Equals( "" ) ) { this->WebBrowser1->Navigate( this->TextBoxAddress->Text ); } } // Navigates to the URL in the address text box when // the Go button is clicked. void ButtonGo_Click( System::Object^ /*sender*/, System::EventArgs^ /*e*/ ) { if ( !this->TextBoxAddress->Text->Equals( "" ) ) { this->WebBrowser1->Navigate( this->TextBoxAddress->Text ); } } // Updates the URL in TextBoxAddress upon navigation. void WebBrowser1_Navigated( Object^ /*sender*/, System::Windows::Forms::WebBrowserNavigatedEventArgs^ /*e*/ ) { this->TextBoxAddress->Text = this->WebBrowser1->Url->ToString(); }
System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Control
System.Windows.Forms.WebBrowserBase
System.Windows.Forms.WebBrowser
プラットフォーム
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
Weblioに収録されているすべての辞書からWebBrowser クラスを検索する場合は、下記のリンクをクリックしてください。
全ての辞書からWebBrowser クラスを検索
- WebBrowser クラスのページへのリンク