次の方法で共有


方法: ハイブリッド アプリケーションでビジュアル スタイルを有効にする

このトピックでは、WPF ベースのアプリケーションでホストされている Windows フォーム コントロールでビジュアル スタイルを有効にする方法について説明します。

アプリケーションが EnableVisualStyles メソッドを呼び出す場合、ほとんどの Windows フォーム コントロールで自動的にビジュアル スタイルが使用されます。 詳細については、「表示スタイルを使用したコントロールのレンダリング」を参照してください。

このトピックに示されているタスクの完全なコード 一覧については、「ハイブリッド アプリケーション サンプルでビジュアル スタイルを有効にする を参照してください。

Windows フォームのビジュアル スタイルの有効化

Windows フォームのビジュアル スタイルを有効にするには

  1. HostingWfWithVisualStylesという名前の WPF アプリケーション プロジェクトを作成します。

  2. ソリューション エクスプローラーで、次のアセンブリへの参照を追加します。

    • WindowsFormsIntegration

    • System.Windows.Forms

  3. ツールボックスで、Grid アイコンをダブルクリックして、デザイン サーフェイスに Grid 要素を配置します。

  4. [プロパティ] ウィンドウで、Height および Width プロパティの値を 自動に設定します。

  5. デザイン ビューまたは XAML ビューで、Windowを選択します。

  6. [プロパティ] ウィンドウで、[イベント] タブをクリックします。

  7. Loaded イベントをダブルクリックします。

  8. 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
    
  9. F5 キーを押して、アプリケーションをビルドして実行します。

    Windows フォーム コントロールは、ビジュアル スタイルで描画されます。

Windows フォームのビジュアル スタイルの無効化

ビジュアル スタイルを無効にするには、EnableVisualStyles メソッドの呼び出しを削除するだけです。

Windows フォームのビジュアル スタイルを無効にするには

  1. コード エディターでMainWindow.xaml.vbまたはMainWindow.xaml.csを開きます。

  2. EnableVisualStyles メソッドの呼び出しをコメント アウトします。

  3. F5 キーを押して、アプリケーションをビルドして実行します。

    Windows フォーム コントロールは、既定のシステム スタイルで描画されます。

こちらも参照ください