更新 : 2010 年 8 月
Windows Presentation Foundation (WPF) は、アプリケーションの作成に適した環境を提供します。 ただし、Windows Formsのコードに多くの投資を行った場合は、コードを最初から記述し直すよりも、WPF アプリケーションのコードの少なくとも一部を再利用する方が効率的です。 最も一般的なシナリオは、既存の Windows Forms コントロールがある場合です。 場合によっては、これらのコントロールのソース コードにアクセスできないことがあります。 WPF には、そのようなコントロールを WPF アプリケーションでホストするための簡単な手順が用意されています。 たとえば、特殊な DataGridView コントロールをホストしながら、ほとんどのプログラミングには WPF を使用できます。
このチュートリアルでは、WPF アプリケーションで Windows Forms 複合コントロールをホストしてデータ エントリを実行するアプリケーションについて段階的に説明します。 複合コントロールは DLL にパッケージ化されています。 この一般的な手順は、さらに複雑なアプリケーションやコントロールに拡張できます。 このチュートリアルは、外観および機能が「チュートリアル: Windows フォームでの WPF 複合コントロールのホスト」の例とほぼ同じになるように設計されています。 主な違いは、ホストする側とされる側が逆であることです。
チュートリアルは、2 つのセクションに分かれています。 最初のセクションでは、Windows Forms 複合コントロールの実装について簡単に説明します。 2 番目のセクションでは、WPF アプリケーションで複合コントロールをホストし、コントロールからイベントを受け取って、コントロールのプロパティの一部にアクセスする方法について詳しく説明します。
このチュートリアルでは、以下のタスクを行います。
Windows フォーム複合コントロールの実装
WPF ホスト アプリケーションの実装
このチュートリアルで示すタスクの完全なコード一覧については、WPF での Windows フォーム複合コントロールのホストのサンプルを参照してください。
必要条件
このチュートリアルを実行するには、次のコンポーネントが必要です。
- Visual Studio 2010
Windows フォーム複合コントロールの実装
この例で使用する Windows Forms 複合コントロールは、単純なデータ入力フォームです。 このフォームは、ユーザーの名前と住所を受け取った後、カスタム イベントを使用してその情報をホストに返します。 レンダリングされたコントロールを次の図に示します。
Windows フォーム複合コントロール
プロジェクトの作成
プロジェクトを開始するには
Microsoft Visual Studio を起動して、[新しいプロジェクト] ダイアログ ボックスを開きます。
ウィンドウのカテゴリで、[Windows フォーム コントロール ライブラリ] テンプレートを選択します。
新しいプロジェクトに MyControls という名前を付けます。
配置場所としては、WpfHostingWindowsFormsControl など、わかりやすい名前を付けた最上位フォルダーを指定します。 このフォルダーには後でホスト アプリケーションも配置します。
[OK] をクリックして、プロジェクトを作成します。 既定のプロジェクトには、UserControl1 という名前の 1 つのコントロールが含まれます。
ソリューション エクスプローラーで、UserControl1 の名前を MyControl1 に変更します。
プロジェクトは、次のシステム DLL を参照している必要があります。 これらの DLL のいずれかが既定で含まれていない場合は、プロジェクトに追加します。
システム
System.Data
System.Drawing
System.Windows.Forms
System.Xml
フォームへのコントロールの追加
フォームにコントロールを追加するには、次の操作を実行します。
- デザイナーで MyControl1 を開きます。
5 つの Label コントロールとそれに対応する TextBox コントロールを、前の図と同じようなサイズと位置関係で、フォーム上に追加します。 この例では、TextBox コントロールには次のような名前を付けます。
txtName
txtAddress
txtCity
txtState
txtZip
OK と Cancel というラベルを付けた 2 つの Button コントロールを追加します。 この例では、ボタンの名前はそれぞれ btnOK と btnCancel です。
サポート コードの実装
コード ビューでフォームを開きます。 コントロールは、カスタム OnButtonClick イベントを発生させることで、収集したデータをホストに返します。 データは、イベント引数オブジェクトに格納されています。 次のコードは、イベントとデリゲートの宣言を示しています。
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; }
}
}
ユーザーが [OK] ボタンまたは [Cancel] ボタンをクリックすると、Click イベント ハンドラーはデータを格納した MyControlEventArgs オブジェクトを作成し、OnButtonClick イベントを発生させます。 2 つのハンドラーの違いは、イベント引数の IsOK プロパティだけです。 このプロパティにより、ホストはどちらのボタンがクリックされたのかを判別できます。 [OK] ボタンの場合は true が設定され、[Cancel] ボタンの場合は false が設定されます。 次のコード例は、2 つのボタン ハンドラーを示しています。
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 アプリケーションからこのアセンブリを参照するには、アセンブリに厳密な名前を付ける必要があります。 厳密な名前を作成するには、Sn.exe でキー ファイルを作成し、それをプロジェクトに追加します。
Visual Studio のコマンド プロンプトを開きます。 この操作を行うには、[スタート] ボタンをクリックし、[すべてのプログラム]、[Microsoft Visual Studio 2010]、[Visual Studio ツール] の順にポイントして、[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 プロジェクトの配置先と同じ最上位フォルダーを指定します。
[OK] をクリックして、プロジェクトを作成します。
MyControl1 および他のアセンブリを含む DLL への参照も追加する必要があります。
ソリューション エクスプローラーで、プロジェクト名を右クリックし、[参照の追加] を選択します。
[参照] タブをクリックし、MyControls.dll を格納しているフォルダーを参照します。 このチュートリアルの場合は、MyControls\bin\Debug フォルダーです。
MyControls.dll を選択し、[OK] をクリックします。
WindowsFormsIntegration.dll という名前の WindowsFormsIntegration アセンブリに参照を追加します。
基本レイアウトの実装
ホスト アプリケーションのuser interface (UI) は、MainWindow.xaml で実装されます。 このファイルは、レイアウトを定義する Extensible Application Markup Language (XAML) マークアップを含み、Windows Forms コントロールをホストします。 アプリケーションは次の 3 つの領域に分かれています。
[Control Properties] パネルには、ホストされるコントロールのさまざまなプロパティの変更に使用できるオプション ボタンのコレクションが含まれます。
[Data from Control] パネルには、ホストされるコントロールから返されるデータを表示する 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 に含まれる次の 2 つの要素がホストを処理します。
WindowsFormsHost は、WPF アプリケーションでの Windows Forms コントロールのホストを可能にする WindowsFormsHost 要素を表します。
MyControl1 を表す mcl: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 ではさまざまなコントロールのプロパティも公開されており、アプリケーションからそれを操作できます。 初期化コードは、後でアプリケーションで使用するため、これらの値をプライベート グローバル変数に代入します。
MyControls DLL の種類に簡単にアクセスできるように、次の Imports ステートメントまたは 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 オブジェクトに格納されます。 ユーザーが [OK] ボタンをクリックすると、イベント ハンドラーはデータを抽出して、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 フォーム複合コントロールにテキストを追加して [OK] をクリックします。 そのテキストがラベルに表示されます。 別のラジオ ボタンをクリックして、コントロール上の影響を確認します。
参照
処理手順
チュートリアル: WPF での Windows フォーム コントロールのホスト
参照
概念
チュートリアル: Windows フォームでの WPF 複合コントロールのホスト
その他の技術情報
履歴の変更
日付 |
履歴 |
理由 |
---|---|---|
2010 年 8 月 |
Visual Studio 2010 用に更新。 |
カスタマー フィードバック |