このトピックでは、WPF ベースのアプリケーションでホストされている Windows フォーム コントロールでビジュアル スタイルを有効にする方法について説明します。
アプリケーションが EnableVisualStyles メソッドを呼び出す場合、ほとんどの Windows フォーム コントロールで自動的にビジュアル スタイルが使用されます。 詳細については、「表示スタイルを使用したコントロールのレンダリング」を参照してください。
このトピックに示されているタスクの完全なコード 一覧については、「ハイブリッド アプリケーション サンプルでビジュアル スタイルを有効にする
Windows フォームのビジュアル スタイルの有効化
Windows フォームのビジュアル スタイルを有効にするには
HostingWfWithVisualStyles
という名前の WPF アプリケーション プロジェクトを作成します。ソリューション エクスプローラーで、次のアセンブリへの参照を追加します。
WindowsFormsIntegration
System.Windows.Forms
デザイン ビューまたは XAML ビューで、Windowを選択します。
[プロパティ] ウィンドウで、[イベント] タブをクリックします。
Loaded イベントをダブルクリックします。
MainWindow.xaml.vbまたはMainWindow.xaml.csで、Loaded イベントを処理する次のコードを挿入します。
private void Window_Loaded(object sender, RoutedEventArgs e) { // Comment out the following line to disable visual // styles for the hosted Windows Forms control. System.Windows.Forms.Application.EnableVisualStyles(); // Create a WindowsFormsHost element to host // the Windows Forms control. System.Windows.Forms.Integration.WindowsFormsHost host = new System.Windows.Forms.Integration.WindowsFormsHost(); // Create a Windows Forms tab control. System.Windows.Forms.TabControl tc = new System.Windows.Forms.TabControl(); tc.TabPages.Add("Tab1"); tc.TabPages.Add("Tab2"); // Assign the Windows Forms tab control as the hosted control. host.Child = tc; // Assign the host element to the parent Grid element. this.grid1.Children.Add(host); }
Private Sub Window_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs) ' Comment out the following line to disable visual ' styles for the hosted Windows Forms control. System.Windows.Forms.Application.EnableVisualStyles() ' Create a WindowsFormsHost element to host ' the Windows Forms control. Dim host As New System.Windows.Forms.Integration.WindowsFormsHost() ' Create a Windows Forms tab control. Dim tc As New System.Windows.Forms.TabControl() tc.TabPages.Add("Tab1") tc.TabPages.Add("Tab2") ' Assign the Windows Forms tab control as the hosted control. host.Child = tc ' Assign the host element to the parent Grid element. Me.grid1.Children.Add(host) End Sub
F5 キーを押して、アプリケーションをビルドして実行します。
Windows フォーム コントロールは、ビジュアル スタイルで描画されます。
Windows フォームのビジュアル スタイルの無効化
ビジュアル スタイルを無効にするには、EnableVisualStyles メソッドの呼び出しを削除するだけです。
Windows フォームのビジュアル スタイルを無効にするには
コード エディターでMainWindow.xaml.vbまたはMainWindow.xaml.csを開きます。
EnableVisualStyles メソッドの呼び出しをコメント アウトします。
F5 キーを押して、アプリケーションをビルドして実行します。
Windows フォーム コントロールは、既定のシステム スタイルで描画されます。
こちらも参照ください
- EnableVisualStyles
- System.Windows.Forms.VisualStyles
- WindowsFormsHost
- visual スタイルが使用されているコントロールのレンダリング
- チュートリアル: WPF での Windows フォーム コントロールのホスト
.NET Desktop feedback