共用方式為


HOW TO:在混合應用程式中啟用視覺化樣式

更新:2010 年 8 月

本主題顯示如何在 WPF 架構應用程式中所裝載 (Host) 的 Windows Forms 控制項上啟用 Microsoft Windows XP 視覺化樣式。

如果應用程式呼叫 EnableVisualStyles 方法,而且應用程式是在 Microsoft Windows XP 中執行時,大部分的 Windows Forms 控制項都會自動使用視覺化樣式。 如需詳細資訊,請參閱 使用視覺化樣式呈現控制項

如需本主題中所說明之工作的完整程式碼清單,請參閱在混合式應用程式中啟用視覺化樣式範例 (英文)。

啟用 Windows Form 視覺化樣式

若要啟用 Windows Form 視覺化樣式

  1. 建立名為 HostingWfWithVisualStyles 的 WPF 應用程式專案。

  2. 在 [方案總管] 中加入下列組件的參考。

    • WindowsFormsIntegration

    • System.Windows.Forms

  3. 按兩下 [工具箱] 中的 Grid 圖示,將 Grid 項目放在設計介面上。

  4. 在 [屬性] 視窗中,將 HeightWidth 屬性的值設定為 [Auto]。

  5. 在 [設計] 檢視或 [XAML] 檢視中,選取 Window

  6. 按一下 [屬性] 視窗中的 [事件] 索引標籤。

  7. 按兩下 Loaded 事件。

  8. 在 MainWindow.xaml.vb 或 MainWindow.xaml.cs 中,插入下列程式碼以處理 Loaded 事件。

    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. 按 F5 建置並執行應用程式。

    會使用視覺化樣式繪製 Windows Forms 控制項。

停用 Windows Form 視覺化樣式

若要停用視覺化樣式,只需移除對 EnableVisualStyles 方法的呼叫即可。

若要停用 Windows Form 視覺化樣式

  1. 在程式碼編輯器中開啟 MainWindow.xaml.vb 或 MainWindow.xaml.cs。

  2. 將對 EnableVisualStyles 方法的呼叫標記為註解。

  3. 按 F5 建置並執行應用程式。

    會使用預設系統樣式繪製 Windows Forms 控制項。

請參閱

工作

逐步解說:在 WPF 中裝載 Windows Form 控制項

參考

EnableVisualStyles

System.Windows.Forms.VisualStyles

WindowsFormsHost

概念

使用視覺化樣式呈現控制項

變更記錄

日期

記錄

原因

2010 年 8 月

更新成 Visual Studio 2010 適用的內容。

客戶回函。