Vorgehensweise: Aktivieren von visuellen Stilen in einer Hybridanwendung
In diesem Thema wird gezeigt, wie Sie visuelle Stile für ein in einer WPF-basierten Anwendung gehostetes Windows Forms-Steuerelement aktivieren.
Wenn Ihre Anwendung die EnableVisualStyles-Methode aufruft, werden die meisten Ihrer Windows Forms-Steuerelemente automatisch Formatierungsvisualisierungen verwenden. Weitere Informationen finden Sie unter Rendering von Steuerelementen mit Formatierungsvisualisierungen.
Eine vollständige Codeauflistung der Aufgaben, die in diesem Thema veranschaulicht werden, finden Sie unter Aktivierung von Visual Styles in einem hybriden Anwendungsbeispiel.
Aktivieren von Formatierungsvisualisierungen in Windows Forms
Aktivieren von Formatierungsvisualisierungen in Windows Forms
Erstellen Sie ein WPF-Anwendungsprojekt mit dem Namen
HostingWfWithVisualStyles
.Fügen Sie im Solution Explorer Verweise auf die folgenden Assemblys hinzu.
WindowsFormsIntegration
System.Windows.Forms
Doppelklicken Sie in der Toolbox auf das Symbol Grid, um ein Grid-Element auf der Entwurfsoberfläche zu platzieren.
Legen Sie im Eigenschaftenfenster die Werte der Eigenschaften Height und Width auf Autofest.
Wählen Sie in der Entwurfsansicht oder der XAML-Ansicht Window aus.
Klicken Sie im Fenster „Eigenschaften“ auf die Schaltfläche Ereignisse.
Doppelklicken Sie auf das Loaded-Ereignis.
Fügen Sie in MainWindow.xaml.vb oder MainWindow.xaml.cs den folgenden Code ein, um das Loaded-Ereignis zu behandeln.
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); }
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
Drücken Sie F5, um die Anwendung zu erstellen und auszuführen.
Das Windows Forms-Steuerelement ist mit Formatierungsvisualisierungen versehen.
Deaktivieren von Formatierungsvisualisierungen in Windows Forms
Entfernen Sie einfach den Aufruf der EnableVisualStyles-Methode, um Formatierungsvisualisierungen zu deaktivieren.
So deaktivieren Sie visuelle Designs von Windows Forms
Öffnen Sie MainWindow.xaml.vb oder MainWindow.xaml.cs im Code-Editor.
Kommentieren Sie den Aufruf der EnableVisualStyles-Methode aus.
Drücken Sie F5, um die Anwendung zu erstellen und auszuführen.
Das Windows Forms-Steuerelement wird mit dem Standardsystemstil gezeichnet.
Weitere Informationen
.NET Desktop feedback