How to: Enable Visual Styles in a Hybrid Application
This topic shows how to enable Microsoft Windows XP visual styles on a Windows Forms control hosted in a WPF-based application.
If your application calls the EnableVisualStyles method, most of your Windows Forms controls will automatically use visual styles when your application is run on Microsoft Windows XP. For more information, see Rendering Controls with Visual Styles.
For a complete code listing of the tasks illustrated in this topic, see Enabling Visual Styles in a Hybrid Application Sample.
Note: |
---|
The dialog boxes and menu commands you see might differ from those described in Help, depending on your active settings or edition. To change your settings, choose Import and Export Settings on the Tools menu. |
Enabling Windows Forms Visual Styles
To enable Windows Forms visual styles
Create a WPF application project named HostingWfWithVisualStyles.
In Solution Explorer, add a reference to the WindowsFormsIntegration assembly, which is named WindowsFormsIntegration.dll.
The default location for this file is %programfiles%\Reference Assemblies\Microsoft\Framework\v3.0\WindowsFormsIntegration.dll.
In Solution Explorer, add a reference to the Windows Forms assembly, which is named System.Windows.Forms.dll.
In the Toolbox, double-click the Grid icon to place a Grid element on the design surface.
In the Properties window, set the values of the Height and Width properties to Auto.
Click the Xaml button to open Window1.xaml in the Code Editor.
Insert the following code to attach en event handler for the Loaded event.
<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" >
<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" >
Open Window1.xaml.cs in the Code Editor.
Insert the following code to handle the Loaded event.
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); }
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
Press F5 to build and run the application.
The Windows Forms control is painted with visual styles.
Disabling Windows Forms Visual Styles
To disable visual styles, simply remove the call to the EnableVisualStyles method.
To disable Windows Forms visual styles
Open Window1.xaml.cs in the Code Editor.
Comment out the call to the EnableVisualStyles method.
Press F5 to build and run the application.
The Windows Forms control is painted with the default system style.
See Also
Tasks
Walkthrough: Hosting a Windows Forms Control in Windows Presentation Foundation
Reference
EnableVisualStyles
System.Windows.Forms.VisualStyles
WindowsFormsHost
Other Resources
Rendering Controls with Visual Styles
Migration and Interoperability How-to Topics