方法 : ハイブリッド アプリケーションで視覚スタイルを有効にする
更新 : 2007 年 11 月
ここでは、WPF ベースのアプリケーションでホストされている Windows フォーム コントロールで Microsoft Windows XP の視覚スタイルを有効にする方法を示します。
アプリケーションが EnableVisualStyles メソッドを呼び出すと、ほとんどの Windows フォーム コントロールは、アプリケーションが Microsoft Windows XP で実行されるときの視覚スタイルを自動的に使用します。詳細については、「visual スタイルが使用されているコントロールのレンダリング」を参照してください。
このトピックで示すタスクの完全なコード一覧については、「ハイブリッド アプリケーションでの視覚スタイルの有効化のサンプル」を参照してください。
メモ : |
---|
使用している設定またはエディションによっては、ヘルプの記載と異なるダイアログ ボックスやメニュー コマンドが表示される場合があります。設定を変更するには、[ツール] メニューの [設定のインポートとエクスポート] をクリックします。 |
Windows フォーム視覚スタイルの有効化
Windows フォーム視覚スタイルを有効にするには
HostingWfWithVisualStyles という名前の WPF アプリケーション プロジェクトを作成します。
ソリューション エクスプローラで、WindowsFormsIntegration.dll という名前の WindowsFormsIntegration アセンブリへの参照を追加します。
このファイルの既定の場所は、%programfiles%\Reference Assemblies\Microsoft\Framework\v3.0\WindowsFormsIntegration.dll です。
ソリューション エクスプローラで、System.Windows.Forms.dll という名前の Windows フォーム アセンブリへの参照を追加します。
コード エディタで Window1.xaml を開きます。
次のコードを挿入し、Loaded イベントのイベント ハンドラを結合します。
<Window x:Class="Window1" xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" Title="HostingWfWithVisualStyles" Height="300" Width="300" Loaded="WindowLoaded" >
<Window x:Class="HostingWfWithVisualStyles.Window1" xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml" Title="HostingWfWithVisualStyles" Height="300" Width="300" Loaded="WindowLoaded" >
コード エディタで Window1.xaml.cs を開きます。
Loaded イベントを処理するには、次のコードを挿入します。
Private Sub WindowLoaded(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
private void WindowLoaded(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); }
F5 キーを押してアプリケーションをビルドし、実行します。
Windows フォーム コントロールが、視覚スタイルで塗りつぶされます。
Windows フォーム視覚スタイルの無効化
視覚スタイルを無効にするには、EnableVisualStyles メソッドの呼び出しを削除します。
Windows フォーム視覚スタイルを無効にするには
コード エディタで Window1.xaml.cs を開きます。
EnableVisualStyles メソッドの呼び出しをコメント化します。
F5 キーを押してアプリケーションをビルドし、実行します。
Windows フォーム コントロールが既定のシステム スタイルで塗りつぶされます。
参照
処理手順
チュートリアル : Windows Presentation Foundation での Windows フォーム コントロールのホスト
概念
visual スタイルが使用されているコントロールのレンダリング
参照
System.Windows.Forms.VisualStyles