方法: ハイブリッド アプリケーションで視覚スタイルを有効にする
ここでは、WPF ベースのアプリケーションでホストされている Windows フォーム コントロールで、視覚スタイルを有効にする方法について説明します。
アプリケーションから EnableVisualStyles メソッドを呼び出す場合、ほとんどの Windows フォーム コントロールには自動的に視覚スタイルが使用されます。 詳細については、「visual スタイルが使用されているコントロールのレンダリング」を参照してください。
このトピックで示すタスクの完全なコード一覧については、ハイブリッド アプリケーションでの視覚スタイルの有効化のサンプルを参照してください。
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 フォーム コントロールは、既定のシステム スタイルで描画されます。
関連項目
.NET Desktop feedback
フィードバック
https://aka.ms/ContentUserFeedback」を参照してください。
以下は間もなく提供いたします。2024 年を通じて、コンテンツのフィードバック メカニズムとして GitHub の issue を段階的に廃止し、新しいフィードバック システムに置き換えます。 詳細については、「フィードバックの送信と表示