HOW TO:在混合應用程式中啟用視覺化樣式
更新:2007 年 11 月
本主題顯示如何在 WPF 架構應用程式中所裝載 (Host) 的 Windows Form 控制項上啟用 Microsoft Windows XP 視覺化樣式。
如果應用程式呼叫 EnableVisualStyles 方法,而且應用程式是在 Microsoft Windows XP 中執行時,大部分的 Windows Form 控制項都會自動使用視覺化樣式。如需詳細資訊,請參閱使用視覺化樣式呈現控制項。
如需本主題中所說明之工作的完整程式碼清單,請參閱在混合應用程式中啟用視覺化樣式範例。
![]() |
---|
根據目前使用的設定與版本,您所看到的對話方塊與功能表命令可能會與 [說明] 中所描述的不同。若要變更設定,請從 [工具] 功能表中選擇 [匯入和匯出設定]。 |
啟用 Windows Form 視覺化樣式
若要啟用 Windows Form 視覺化樣式
建立名為 HostingWfWithVisualStyles 的 WPF 應用程式專案。
在 [方案總管中],加入名為 WindowsFormsIntegration.dll 之 WindowsFormsIntegration 組件 (Assembly) 的參考。
此檔案的預設位置是 %programfiles%\Reference Assemblies\Microsoft\Framework\v3.0\WindowsFormsIntegration.dll。
在 [方案總管] 中,加入名為 System.Windows.Forms.dll 之 Windows Form 組件的參考。
在 [程式碼編輯器] 中開啟 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 Form 控制項。
停用 Windows Form 視覺化樣式
若要停用視覺化樣式,只需移除對 EnableVisualStyles 方法的呼叫即可。
若要停用 Windows Form 視覺化樣式
在 [程式碼編輯器] 中開啟 Window1.xaml.cs。
將對 EnableVisualStyles 方法的呼叫標記為註解。
按 F5 建置並執行應用程式。
會使用預設系統樣式繪製 Windows Form 控制項。
請參閱
工作
逐步解說:在 Windows Presentation Foundation 中裝載 Windows Form 控制項
概念
參考
System.Windows.Forms.VisualStyles