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.

Enabling Windows Forms Visual Styles

To enable Windows Forms visual styles

  1. Create a WPF Application project named HostingWfWithVisualStyles.

  2. In Solution Explorer, add references to the following assemblies.

    • WindowsFormsIntegration

    • System.Windows.Forms

  3. In the Toolbox, double-click the Grid icon to place a Grid element on the design surface.

  4. In the Properties window, set the values of the Height and Width properties to Auto.

  5. In Design view or XAML view, select the Window.

  6. In the Properties window, click the Events tab.

  7. Double-click the Loaded event.

  8. In MainWindow.xaml.vb or MainWindow.xaml.cs, insert the following code to handle the Loaded event.

    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
    
    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);
    }
    
  9. 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

  1. Open MainWindow.xaml.vb or MainWindow.xaml.cs in the Code Editor.

  2. Comment out the call to the EnableVisualStyles method.

  3. 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 WPF

Reference

EnableVisualStyles

System.Windows.Forms.VisualStyles

WindowsFormsHost

Concepts

Rendering Controls with Visual Styles