逐步解說:在 WPF 中裝載 Windows Form 複合控制項
更新:2010 年 8 月
Windows Presentation Foundation (WPF) 提供用來建立應用程式的豐富環境。 然而,長期開發 Windows Forms 程式碼時,更有效的方式是在 WPF 應用程式中重複使用至少一部分的程式碼,而不是從頭重新撰寫程式碼。 最常見的情形是當您擁有現成的 Windows Forms 控制項時。 在某些情況下,您甚至不能存取這些控制項的原始程式碼。 WPF 提供直接在 WPF 應用程式中裝載這類控制項的程序。 例如,在裝載特製化的 DataGridView 控制項時,您可以使用 WPF 進行大部分的程式設計工作。
這個逐步解說會引導您完成一個應用程式,這個應用程式會裝載 Windows Forms 複合控制項來執行 WPF 應用程式中的資料輸入工作。 複合控制項會封裝成 DLL。 這個一般程序可以延伸至更複雜的應用程式和控制項。 這個逐步解說的外觀及功能設計與逐步解說:在 Windows Form 中裝載 WPF 複合控制項幾乎完全相同。 主要差異是保留了裝載案例。
逐步解說分成兩個部分。 第一個部分簡短說明 Windows Forms 複合控制項的實作。 第二個部分則詳細討論如何在 WPF 應用程式中裝載複合控制項、從控制項接收事件,以及存取控制項的部分屬性。
逐步解說將說明的工作包括:
實作 Windows Form 複合控制項。
實作 WPF 主應用程式。
如需這個逐步解說中所說明之工作的完整程式碼清單,請參閱在 WPF 中裝載 Windows Form 複合控制項範例 (英文)。
必要條件
您需要下列元件才能完成此逐步解說:
- Visual Studio 2010。
實作 Windows Form 複合控制項
此範例中使用的 Windows Forms 複合控制項是簡單的資料輸入表單。 此表單會取得使用者的名稱和地址,然後使用自訂事件,將該資訊傳回給主應用程式 (Host)。 下圖顯示呈現的控制項。
Windows Form 複合控制項
建立專案
若要開始專案:
啟動 Microsoft Visual Studio,並開啟 [新增專案] 對話方塊。
在 [視窗] 分類中,選取 [Windows Form 控制項程式庫] 範本。
將新專案命名為 MyControls。
在位置中,指定適當命名的最上層資料夾,例如 WpfHostingWindowsFormsControl。 稍後,您會將主應用程式放在這個資料夾中。
按一下 [確定] 建立專案。 預設專案包含一個名為 UserControl1 的控制項。
在 [方案總管] 中,將 UserControl1 重新命名為 MyControl1。
您的專案應該參考下列系統 DLL。 如果預設未包含下列 DLL,請將它們加入至專案。
System
System.Data
System.Drawing
System.Windows.Forms
System.Xml
將控制項加入至表單
若要將控制項加入至表單:
- 在設計工具中開啟 MyControl1。
在表單上加入五個 Label 控制項與其對應的 TextBox 控制項,其大小和排列方式跟上圖一樣。 在此範例中,TextBox 控制項的名稱為:
txtName
txtAddress
txtCity
txtState
txtZip
加入兩個標示為 [確定] 和 [取消] 的 Button 控制項。 在此範例中,按鈕名稱分別為 btnOK 和 btnCancel。
實作支援程式碼
在程式碼檢視中開啟表單。 控制項會藉由引發自訂 OnButtonClick 事件,將所收集的資料傳回至其主應用程式。 此資料會包含在事件引數物件中。 下列程式碼顯示 event 和 delegate 宣告。
將下列程式碼加入 MyControl1 類別。
Public Delegate Sub MyControlEventHandler(ByVal sender As Object, ByVal args As MyControlEventArgs)
Public Event OnButtonClick As MyControlEventHandler
public delegate void MyControlEventHandler(object sender, MyControlEventArgs args);
public event MyControlEventHandler OnButtonClick;
MyControlEventArgs 類別包含要傳回給主應用程式的資訊。
將下列類別加入表單。
Public Class MyControlEventArgs
Inherits EventArgs
Private _Name As String
Private _StreetAddress As String
Private _City As String
Private _State As String
Private _Zip As String
Private _IsOK As Boolean
Public Sub New(ByVal result As Boolean, ByVal name As String, ByVal address As String, ByVal city As String, ByVal state As String, ByVal zip As String)
_IsOK = result
_Name = name
_StreetAddress = address
_City = city
_State = state
_Zip = zip
End Sub
Public Property MyName() As String
Get
Return _Name
End Get
Set
_Name = value
End Set
End Property
Public Property MyStreetAddress() As String
Get
Return _StreetAddress
End Get
Set
_StreetAddress = value
End Set
End Property
Public Property MyCity() As String
Get
Return _City
End Get
Set
_City = value
End Set
End Property
Public Property MyState() As String
Get
Return _State
End Get
Set
_State = value
End Set
End Property
Public Property MyZip() As String
Get
Return _Zip
End Get
Set
_Zip = value
End Set
End Property
Public Property IsOK() As Boolean
Get
Return _IsOK
End Get
Set
_IsOK = value
End Set
End Property
End Class
public class MyControlEventArgs : EventArgs
{
private string _Name;
private string _StreetAddress;
private string _City;
private string _State;
private string _Zip;
private bool _IsOK;
public MyControlEventArgs(bool result,
string name,
string address,
string city,
string state,
string zip)
{
_IsOK = result;
_Name = name;
_StreetAddress = address;
_City = city;
_State = state;
_Zip = zip;
}
public string MyName
{
get { return _Name; }
set { _Name = value; }
}
public string MyStreetAddress
{
get { return _StreetAddress; }
set { _StreetAddress = value; }
}
public string MyCity
{
get { return _City; }
set { _City = value; }
}
public string MyState
{
get { return _State; }
set { _State = value; }
}
public string MyZip
{
get { return _Zip; }
set { _Zip = value; }
}
public bool IsOK
{
get { return _IsOK; }
set { _IsOK = value; }
}
}
當使用者按一下 [確定] 或 [取消] 按鈕時,Click 事件處理常式會建立 MyControlEventArgs 物件,該物件含有資料並會引發 OnButtonClick 事件。 這兩個處理常式之間的唯一差異在於事件引數的 IsOK 屬性。 此屬性可讓主應用程式判斷按下的是哪一個按鈕。 [確定] 按鈕的這個屬性設為 true,而 [取消] 按鈕的這個屬性設為 false。 下列程式碼顯示這兩個按鈕處理常式。
將下列程式碼加入 MyControl1 類別。
Private Sub btnOK_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnOK.Click
Dim retvals As New MyControlEventArgs(True, txtName.Text, txtAddress.Text, txtCity.Text, txtState.Text, txtZip.Text)
RaiseEvent OnButtonClick(Me, retvals)
End Sub
Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancel.Click
Dim retvals As New MyControlEventArgs(False, txtName.Text, txtAddress.Text, txtCity.Text, txtState.Text, txtZip.Text)
RaiseEvent OnButtonClick(Me, retvals)
End Sub
private void btnOK_Click(object sender, System.EventArgs e)
{
MyControlEventArgs retvals = new MyControlEventArgs(true,
txtName.Text,
txtAddress.Text,
txtCity.Text,
txtState.Text,
txtZip.Text);
OnButtonClick(this, retvals);
}
private void btnCancel_Click(object sender, System.EventArgs e)
{
MyControlEventArgs retvals = new MyControlEventArgs(false,
txtName.Text,
txtAddress.Text,
txtCity.Text,
txtState.Text,
txtZip.Text);
OnButtonClick(this, retvals);
}
為組件提供強式名稱並建置組件
對於 WPF 應用程式所要參考的這個組件而言,它必須具有強式名稱 (Strong Name)。 若要建立強式名稱,請使用 Sn.exe 建立金鑰檔,並將它加入至專案。
開啟 Visual Studio 命令提示字元。 若要這麼做,請按一下 [開始] 功能表,然後選取 [所有程式/Microsoft Visual Studio 2010/Visual Studio Tools/Visual Studio 命令提示字元]。 這樣會啟動具有自訂環境變數的主控台視窗。
在命令提示字元中,使用 cd 命令移至您的專案資料夾。
執行下列命令,以便產生名稱為 MyControls.snk 的金鑰檔。
Sn.exe -k MyControls.snk
若要將金鑰檔包含於專案中,請在 [方案總管] 中以滑鼠右鍵按一下專案名稱,然後按一下 [屬性]。 在 [專案設計工具] 中,按一下 [簽署] 索引標籤,選取 [簽署組件] 核取方塊,然後瀏覽至您的金鑰檔。
建置方案。 此組建會產生名稱為 MyControls.dll 的 DLL。
實作 WPF 主應用程式
WPF 主應用程式使用 WindowsFormsHost 控制項來裝載 MyControl1。 此應用程式會處理 OnButtonClick 事件,以便接收來自控制項的資料。 此外,它還具備選項按鈕集合,可讓您從 WPF 應用程式變更控制項的某些屬性。 下圖顯示的是完成的應用程式。
完整的應用程式,顯示內嵌於 WPF 應用程式的控制項
建立專案
若要開始專案:
開啟 Visual Studio,然後選取 [新增專案]。
在 [視窗] 分類中,選取 [WPF 應用程式] 範本。
將新專案命名為 WpfHost。
在位置中,指定內含 MyControls 專案的相同最上層資料夾。
按一下 [確定] 建立專案。
對於內含 MyControl1 的 DLL 和其他組件,您也需要加入這些 DLL 和組件的參考。
以滑鼠右鍵按一下 [方案總管] 中的專案名稱,然後選取 [加入參考]。
按一下 [瀏覽] 索引標籤,然後瀏覽至內含 MyControls.dll 的資料夾。 在此逐步解說中,這個資料夾為 MyControls\bin\Debug。
選取 MyControls.dll,然後按一下 [確定]。
加入 WindowsFormsIntegration 組件的參考,該組件名為 WindowsFormsIntegration.dll。
實作基本配置
主應用程式的user interface (UI) 會在 MainWindow.xaml 中實作。 此檔案包含可定義配置的Extensible Application Markup Language (XAML) 標記,並且會裝載 Windows Forms 控制項。 應用程式分為三個區域:
[控制項屬性] 面板,其中包含選項按鈕集合,可供您修改所裝載控制項的各種屬性。
[資料來源控制項] 面板,其中包含數個 TextBlock 項目,可顯示從所裝載控制項傳回的資料。
裝載的控制項本身。
下列 XAML 顯示基本配置。 這個範例省略了裝載 MyControl1 所需的標記,但我們稍後會進行討論。
請用下列程式碼取代 MainWindow.xaml 中的 XAML。 如果您使用的是 Visual Basic,請將類別變更為 x:Class="MainWindow"。
<Window xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WpfHost.MainWindow"
xmlns:mcl="clr-namespace:MyControls;assembly=MyControls"
Loaded="Init">
<DockPanel>
<DockPanel.Resources>
<Style x:Key="inlineText" TargetType="{x:Type Inline}">
<Setter Property="FontWeight" Value="Normal"/>
</Style>
<Style x:Key="titleText" TargetType="{x:Type TextBlock}">
<Setter Property="DockPanel.Dock" Value="Top"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Margin" Value="10,5,10,0"/>
</Style>
</DockPanel.Resources>
<StackPanel Orientation="Vertical"
DockPanel.Dock="Left"
Background="Bisque"
Width="250">
<TextBlock Margin="10,10,10,10"
FontWeight="Bold"
FontSize="12">Control Properties</TextBlock>
<TextBlock Style="{StaticResource titleText}">Background Color</TextBlock>
<StackPanel Margin="10,10,10,10">
<RadioButton Name="rdbtnOriginalBackColor"
IsChecked="True"
Click="BackColorChanged">Original</RadioButton>
<RadioButton Name="rdbtnBackGreen"
Click="BackColorChanged">LightGreen</RadioButton>
<RadioButton Name="rdbtnBackSalmon"
Click="BackColorChanged">LightSalmon</RadioButton>
</StackPanel>
<TextBlock Style="{StaticResource titleText}">Foreground Color</TextBlock>
<StackPanel Margin="10,10,10,10">
<RadioButton Name="rdbtnOriginalForeColor"
IsChecked="True"
Click="ForeColorChanged">Original</RadioButton>
<RadioButton Name="rdbtnForeRed"
Click="ForeColorChanged">Red</RadioButton>
<RadioButton Name="rdbtnForeYellow"
Click="ForeColorChanged">Yellow</RadioButton>
</StackPanel>
<TextBlock Style="{StaticResource titleText}">Font Family</TextBlock>
<StackPanel Margin="10,10,10,10">
<RadioButton Name="rdbtnOriginalFamily"
IsChecked="True"
Click="FontChanged">Original</RadioButton>
<RadioButton Name="rdbtnTimes"
Click="FontChanged">Times New Roman</RadioButton>
<RadioButton Name="rdbtnWingdings"
Click="FontChanged">Wingdings</RadioButton>
</StackPanel>
<TextBlock Style="{StaticResource titleText}">Font Size</TextBlock>
<StackPanel Margin="10,10,10,10">
<RadioButton Name="rdbtnOriginalSize"
IsChecked="True"
Click="FontSizeChanged">Original</RadioButton>
<RadioButton Name="rdbtnTen"
Click="FontSizeChanged">10</RadioButton>
<RadioButton Name="rdbtnTwelve"
Click="FontSizeChanged">12</RadioButton>
</StackPanel>
<TextBlock Style="{StaticResource titleText}">Font Style</TextBlock>
<StackPanel Margin="10,10,10,10">
<RadioButton Name="rdbtnNormalStyle"
IsChecked="True"
Click="StyleChanged">Original</RadioButton>
<RadioButton Name="rdbtnItalic"
Click="StyleChanged">Italic</RadioButton>
</StackPanel>
<TextBlock Style="{StaticResource titleText}">Font Weight</TextBlock>
<StackPanel Margin="10,10,10,10">
<RadioButton Name="rdbtnOriginalWeight"
IsChecked="True"
Click="WeightChanged">
Original
</RadioButton>
<RadioButton Name="rdbtnBold"
Click="WeightChanged">Bold</RadioButton>
</StackPanel>
</StackPanel>
<WindowsFormsHost Name="wfh"
DockPanel.Dock="Top"
Height="300">
<mcl:MyControl1 Name="mc"/>
</WindowsFormsHost>
<StackPanel Orientation="Vertical"
Height="Auto"
Background="LightBlue">
<TextBlock Margin="10,10,10,10"
FontWeight="Bold"
FontSize="12">Data From Control</TextBlock>
<TextBlock Style="{StaticResource titleText}">
Name: <Span Name="txtName" Style="{StaticResource inlineText}"/>
</TextBlock>
<TextBlock Style="{StaticResource titleText}">
Street Address: <Span Name="txtAddress" Style="{StaticResource inlineText}"/>
</TextBlock>
<TextBlock Style="{StaticResource titleText}">
City: <Span Name="txtCity" Style="{StaticResource inlineText}"/>
</TextBlock>
<TextBlock Style="{StaticResource titleText}">
State: <Span Name="txtState" Style="{StaticResource inlineText}"/>
</TextBlock>
<TextBlock Style="{StaticResource titleText}">
Zip: <Span Name="txtZip" Style="{StaticResource inlineText}"/>
</TextBlock>
</StackPanel>
</DockPanel>
</Window>
第一個 StackPanel 項目包含好幾組 RadioButton 控制項,這些控制項可讓您修改所裝載控制項的各種預設屬性。 後面接著是 WindowsFormsHost 項目,該項目會裝載 MyControl1。 而最後一個 StackPanel 項目包含數個 TextBlock 項目,可顯示從所裝載控制項傳回的資料。 項目的順序以及 Dock 和 Height 屬性設定會絲毫不差地將裝載的控制項內嵌到視窗中。
裝載控制項
下列為前一個 XAML 的編輯後版本,著重於裝載 MyControl1 所需的項目。
<Window xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
x:Class="WpfHost.MainWindow"
xmlns:mcl="clr-namespace:MyControls;assembly=MyControls"
Loaded="Init">
...
<WindowsFormsHost Name="wfh"
DockPanel.Dock="Top"
Height="300">
<mcl:MyControl1 Name="mc"/>
</WindowsFormsHost>
xmlns 命名空間對應屬性會建立含有所裝載控制項之 MyControls 命名空間的參考。 此對應可讓您以 XAML 將 MyControl1 表示為 <mcl:MyControl1>。
XAML 中的兩個項目會處理裝載:
WindowsFormsHost 表示 WindowsFormsHost 項目,該項目可讓您在 WPF 應用程式中裝載 Windows Forms 控制項。
mcl:MyControl1 表示 MyControl1,它會加入至 WindowsFormsHost 項目的子系集合中。 因此,此 Windows Forms 控制項會呈現為 WPF 視窗的一部分,而您可以從此應用程式與該控制項通訊。
實作程式碼後置檔案
程式碼後置檔案 MainWindow.xaml.vb 或 MainWindow.xaml.cs 包含程序性程式碼,可以實作上一節所討論之 UI 的功能。 主要工作如下:
將事件處理常式附加至 MyControl1 的 OnButtonClick 事件。
根據選項按鈕集合的設定方式,修改 MyControl1 的各種屬性。
顯示控制項所收集的資料。
初始化應用程式
初始化程式碼會包含在視窗之 Loaded 事件的事件處理常式中,而且會將事件處理常式附加至控制項的 OnButtonClick 事件。
在 MainWindow.xaml.vb 或 MainWindow.xaml.cs 中,將下列程式碼加入至 MainWindow 類別。
Private app As Application
Private myWindow As Window
Private initFontWeight As FontWeight
Private initFontSize As [Double]
Private initFontStyle As FontStyle
Private initBackBrush As SolidColorBrush
Private initForeBrush As SolidColorBrush
Private initFontFamily As FontFamily
Private UIIsReady As Boolean = False
Private Sub Init(ByVal sender As Object, ByVal e As RoutedEventArgs)
app = System.Windows.Application.Current
myWindow = CType(app.MainWindow, Window)
myWindow.SizeToContent = SizeToContent.WidthAndHeight
wfh.TabIndex = 10
initFontSize = wfh.FontSize
initFontWeight = wfh.FontWeight
initFontFamily = wfh.FontFamily
initFontStyle = wfh.FontStyle
initBackBrush = CType(wfh.Background, SolidColorBrush)
initForeBrush = CType(wfh.Foreground, SolidColorBrush)
Dim mc As MyControl1 = wfh.Child
AddHandler mc.OnButtonClick, AddressOf Pane1_OnButtonClick
UIIsReady = True
End Sub
private Application app;
private Window myWindow;
FontWeight initFontWeight;
Double initFontSize;
FontStyle initFontStyle;
SolidColorBrush initBackBrush;
SolidColorBrush initForeBrush;
FontFamily initFontFamily;
bool UIIsReady = false;
private void Init(object sender, EventArgs e)
{
app = System.Windows.Application.Current;
myWindow = (Window)app.MainWindow;
myWindow.SizeToContent = SizeToContent.WidthAndHeight;
wfh.TabIndex = 10;
initFontSize = wfh.FontSize;
initFontWeight = wfh.FontWeight;
initFontFamily = wfh.FontFamily;
initFontStyle = wfh.FontStyle;
initBackBrush = (SolidColorBrush)wfh.Background;
initForeBrush = (SolidColorBrush)wfh.Foreground;
(wfh.Child as MyControl1).OnButtonClick += new MyControl1.MyControlEventHandler(Pane1_OnButtonClick);
UIIsReady = true;
}
因為先前討論的 XAML 會將 MyControl1 加入至 WindowsFormsHost 項目的子項目集合中,所以您可將 WindowsFormsHost 項目的 Child 轉型,以取得 MyControl1 的參考。 然後,您可以使用該參考將事件處理常式附加至 OnButtonClick。
除了提供控制項本身的參考以外,WindowsFormsHost 還公開您可以從此應用程式操作之控制項的數個屬性。 初始化程式碼會將這些值指定給私用 (Private) 全域變數,以便稍後用在應用程式中。
為了方便您存取 MyControls DLL 中的型別,請將下列 Imports or using 陳述式加入至檔案的開頭。
Imports MyControls
using MyControls;
處理 OnButtonClick 事件
當使用者按一下其中一個控制項按鈕時,MyControl1 會引發 OnButtonClick 事件。
將下列程式碼加入 MainWindow 類別。
'Handle button clicks on the Windows Form control
Private Sub Pane1_OnButtonClick(ByVal sender As Object, ByVal args As MyControlEventArgs)
txtName.Inlines.Clear()
txtAddress.Inlines.Clear()
txtCity.Inlines.Clear()
txtState.Inlines.Clear()
txtZip.Inlines.Clear()
If args.IsOK Then
txtName.Inlines.Add(" " + args.MyName)
txtAddress.Inlines.Add(" " + args.MyStreetAddress)
txtCity.Inlines.Add(" " + args.MyCity)
txtState.Inlines.Add(" " + args.MyState)
txtZip.Inlines.Add(" " + args.MyZip)
End If
End Sub
//Handle button clicks on the Windows Form control
private void Pane1_OnButtonClick(object sender, MyControlEventArgs args)
{
txtName.Inlines.Clear();
txtAddress.Inlines.Clear();
txtCity.Inlines.Clear();
txtState.Inlines.Clear();
txtZip.Inlines.Clear();
if (args.IsOK)
{
txtName.Inlines.Add( " " + args.MyName );
txtAddress.Inlines.Add( " " + args.MyStreetAddress );
txtCity.Inlines.Add( " " + args.MyCity );
txtState.Inlines.Add( " " + args.MyState );
txtZip.Inlines.Add( " " + args.MyZip );
}
}
文字方塊中的資料會封裝在 MyControlEventArgs 物件中。 如果使用者按一下 [確定] 按鈕,事件處理常式便會擷取資料並將資料顯示在 MyControl1 下面的面板中。
修改控制項的屬性
WindowsFormsHost 項目會公開所裝載控制項的數個預設屬性。 因此,您可以變更控制項的外觀,使其更接近您的應用程式樣式。 左面板中的選項按鈕集,可供使用者修改數個色彩和字型屬性。 每組按鈕都有 Click 事件的處理常式,該處理常式會偵測使用者的選項按鈕選取項目,並且變更控制項的對應屬性。
將下列程式碼加入 MainWindow 類別。
Private Sub BackColorChanged(ByVal sender As Object, ByVal e As RoutedEventArgs)
If sender.Equals(rdbtnBackGreen) Then
wfh.Background = New SolidColorBrush(Colors.LightGreen)
ElseIf sender.Equals(rdbtnBackSalmon) Then
wfh.Background = New SolidColorBrush(Colors.LightSalmon)
ElseIf UIIsReady = True Then
wfh.Background = initBackBrush
End If
End Sub
Private Sub ForeColorChanged(ByVal sender As Object, ByVal e As RoutedEventArgs)
If sender.Equals(rdbtnForeRed) Then
wfh.Foreground = New SolidColorBrush(Colors.Red)
ElseIf sender.Equals(rdbtnForeYellow) Then
wfh.Foreground = New SolidColorBrush(Colors.Yellow)
ElseIf UIIsReady = True Then
wfh.Foreground = initForeBrush
End If
End Sub
Private Sub FontChanged(ByVal sender As Object, ByVal e As RoutedEventArgs)
If sender.Equals(rdbtnTimes) Then
wfh.FontFamily = New FontFamily("Times New Roman")
ElseIf sender.Equals(rdbtnWingdings) Then
wfh.FontFamily = New FontFamily("Wingdings")
ElseIf UIIsReady = True Then
wfh.FontFamily = initFontFamily
End If
End Sub
Private Sub FontSizeChanged(ByVal sender As Object, ByVal e As RoutedEventArgs)
If sender.Equals(rdbtnTen) Then
wfh.FontSize = 10
ElseIf sender.Equals(rdbtnTwelve) Then
wfh.FontSize = 12
ElseIf UIIsReady = True Then
wfh.FontSize = initFontSize
End If
End Sub
Private Sub StyleChanged(ByVal sender As Object, ByVal e As RoutedEventArgs)
If sender.Equals(rdbtnItalic) Then
wfh.FontStyle = FontStyles.Italic
ElseIf UIIsReady = True Then
wfh.FontStyle = initFontStyle
End If
End Sub
Private Sub WeightChanged(ByVal sender As Object, ByVal e As RoutedEventArgs)
If sender.Equals(rdbtnBold) Then
wfh.FontWeight = FontWeights.Bold
ElseIf UIIsReady = True Then
wfh.FontWeight = initFontWeight
End If
End Sub
private void BackColorChanged(object sender, RoutedEventArgs e)
{
if (sender == rdbtnBackGreen)
wfh.Background = new SolidColorBrush(Colors.LightGreen);
else if (sender == rdbtnBackSalmon)
wfh.Background = new SolidColorBrush(Colors.LightSalmon);
else if (UIIsReady == true)
wfh.Background = initBackBrush;
}
private void ForeColorChanged(object sender, RoutedEventArgs e)
{
if (sender == rdbtnForeRed)
wfh.Foreground = new SolidColorBrush(Colors.Red);
else if (sender == rdbtnForeYellow)
wfh.Foreground = new SolidColorBrush(Colors.Yellow);
else if (UIIsReady == true)
wfh.Foreground = initForeBrush;
}
private void FontChanged(object sender, RoutedEventArgs e)
{
if (sender == rdbtnTimes)
wfh.FontFamily = new FontFamily("Times New Roman");
else if (sender == rdbtnWingdings)
wfh.FontFamily = new FontFamily("Wingdings");
else if (UIIsReady == true)
wfh.FontFamily = initFontFamily;
}
private void FontSizeChanged(object sender, RoutedEventArgs e)
{
if (sender == rdbtnTen)
wfh.FontSize = 10;
else if (sender == rdbtnTwelve)
wfh.FontSize = 12;
else if (UIIsReady == true)
wfh.FontSize = initFontSize;
}
private void StyleChanged(object sender, RoutedEventArgs e)
{
if (sender == rdbtnItalic)
wfh.FontStyle = FontStyles.Italic;
else if (UIIsReady == true)
wfh.FontStyle = initFontStyle;
}
private void WeightChanged(object sender, RoutedEventArgs e)
{
if (sender == rdbtnBold)
wfh.FontWeight = FontWeights.Bold;
else if (UIIsReady == true)
wfh.FontWeight = initFontWeight;
}
建置並執行應用程式。 在 Windows Form 複合控制項中加入一些文字,然後按一下 [確定]。 這些文字會出現在標籤中。 按一下不同的選項按鈕,以查看在控制項上的效果。
請參閱
工作
逐步解說:在 WPF 中裝載 Windows Form 控制項
參考
概念
逐步解說:在 Windows Form 中裝載 WPF 複合控制項
其他資源
變更記錄
日期 |
記錄 |
原因 |
---|---|---|
2010 年 8 月 |
更新成 Visual Studio 2010 適用的內容。 |
客戶回函。 |