如何:在混合应用程序中启用视觉样式

更新:2007 年 11 月

本主题演示如何在基于 WPF 的应用程序中承载的 Windows 窗体控件上启用 Microsoft Windows XP 视觉样式。

如果应用程序调用 EnableVisualStyles 方法,则当应用程序在 Microsoft Windows XP 上运行时,大多数 Windows 窗体控件将自动使用视觉样式。有关更多信息,请参见使用视觉样式呈现控件

有关本主题中演示的任务的完整代码清单,请参见在混合应用程序中启用视觉样式的示例

说明:

显示的对话框和菜单命令可能会与“帮助”中的描述有所不同,具体取决于您现用的设置或版本。若要更改设置,请在“工具”菜单上选择“导入和导出设置”。

启用 Windows 窗体视觉样式

启用 Windows 窗体视觉样式

  1. 创建一个名为 HostingWfWithVisualStyles 的 WPF 应用程序项目。

  2. 在解决方案资源管理器中,添加一个对名为 WindowsFormsIntegration.dll 的 WindowsFormsIntegration 程序集的引用。

    该文件的默认位置为 %programfiles%\Reference Assemblies\Microsoft\Framework\v3.0\WindowsFormsIntegration.dll。

  3. 在解决方案资源管理器中,添加一个对名为 System.Windows.Forms.dll 的 Windows 窗体程序集的引用。

  4. 在工具箱中,双击 Grid 图标,在设计图面上放置一个 Grid 元素。

  5. 在“属性”窗口中,将 HeightWidth 属性的值设置为“自动”。

  6. 在代码编辑器中打开 Window1.xaml。

  7. 插入下面的代码以附加 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"
        >
    
  8. 在代码编辑器中打开 Window1.xaml.cs。

  9. 插入下面的代码以处理 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);
    }
    
  10. 按 F5 生成并运行应用程序。

    将使用视觉样式绘制 Windows 窗体控件。

禁用 Windows 窗体视觉样式

若要禁用视觉样式,只需移除对 EnableVisualStyles 方法的调用。

禁用 Windows 窗体视觉样式

  1. 在代码编辑器中打开 Window1.xaml.cs。

  2. 注释掉对 EnableVisualStyles 方法的调用。

  3. 按 F5 生成并运行应用程序。

    将使用默认系统样式绘制 Windows 窗体控件。

请参见

任务

演练:在 Windows Presentation Foundation 中承载 Windows 窗体控件

概念

使用视觉样式呈现控件

参考

EnableVisualStyles

System.Windows.Forms.VisualStyles

WindowsFormsHost

其他资源

迁移和互操作性帮助主题