728x90
반응형
728x170
■ WPF 응용 프로그램에서 데이터 입력을 수행하기 위해 Windows Forms 복합 컨트롤을 호스팅하는 응용 프로그램을 안내한다. 복합 컨트롤은 DLL에 패키지되어 있다. 이 일반적인 절차는 더 복잡한 응용 프로그램 및 제어로 확장될 수 있다.
[TestLibrary 프로젝트]
▶ CustomControlEventArgs.cs
using System;
namespace TestLibrary
{
/// <summary>
/// 커스텀 컨트롤 이벤트 인자
/// </summary>
public class CustomControlEventArgs : EventArgs
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// OK 버튼 클릭 여부
/// </summary>
private bool okButtonClicked;
/// <summary>
/// 명칭
/// </summary>
private string name;
/// <summary>
/// 주소
/// </summary>
private string address;
/// <summary>
/// 도시
/// </summary>
private string city;
/// <summary>
/// 주
/// </summary>
private string state;
/// <summary>
/// 우편번호
/// </summary>
private string zip;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Property
////////////////////////////////////////////////////////////////////////////////////////// Public
#region OK 버튼 클릭 여부 - OKButtonClicked
/// <summary>
/// OK 버튼 클릭 여부
/// </summary>
public bool OKButtonClicked
{
get
{
return this.okButtonClicked;
}
set
{
this.okButtonClicked = value;
}
}
#endregion
#region 명칭 - Name
/// <summary>
/// 명칭
/// </summary>
public string Name
{
get
{
return this.name;
}
set
{
this.name = value;
}
}
#endregion
#region 주소 - Address
/// <summary>
/// 주소
/// </summary>
public string Address
{
get
{
return this.address;
}
set
{
this.address = value;
}
}
#endregion
#region 도시 - City
/// <summary>
/// 도시
/// </summary>
public string City
{
get
{
return this.city;
}
set
{
this.city = value;
}
}
#endregion
#region 주 - State
/// <summary>
/// 주
/// </summary>
public string State
{
get
{
return this.state;
}
set
{
this.state = value;
}
}
#endregion
#region 우편번호 - Zip
/// <summary>
/// 우편번호
/// </summary>
public string Zip
{
get
{
return this.zip;
}
set
{
this.zip = value;
}
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - CustomControlEventArgs(okButtonClicked, name, address, city, state, zip)
/// <summary>
/// 생성자
/// </summary>
/// <param name="okButtonClicked">OK 버튼 클릭 여부</param>
/// <param name="name">명칭</param>
/// <param name="address">주소</param>
/// <param name="city">도시</param>
/// <param name="state">주</param>
/// <param name="zip">우편번호</param>
public CustomControlEventArgs(bool okButtonClicked, string name, string address, string city, string state, string zip)
{
this.okButtonClicked = okButtonClicked;
this.name = name;
this.address = address;
this.city = city;
this.state = state;
this.zip = zip;
}
#endregion
}
}
▶ CustomControlEventHandler.cs
namespace TestLibrary
{
/// <summary>
/// 커스텀 컨트롤 이벤트 핸들러
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
public delegate void CustomControlEventHandler(object sender, CustomControlEventArgs e);
}
▶ CustomControl.cs
using System;
using System.Windows.Forms;
namespace TestLibrary
{
/// <summary>
/// 커스텀 컨트롤
/// </summary>
public partial class CustomControl : UserControl
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Event
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 버튼 클릭시 - ButtonClick
/// <summary>
/// 버튼 클릭시
/// </summary>
public event CustomControlEventHandler ButtonClick;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - CustomControl()
/// <summary>
/// 생성자
/// </summary>
public CustomControl()
{
InitializeComponent();
this.okButton.Click += okButton_Click;
this.cancelButton.Click += cancelButton_Click;
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region OK 버튼 클릭시 처리하기 - okButton_Click(sender, e)
/// <summary>
/// OK 버튼 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void okButton_Click(object sender, EventArgs e)
{
CustomControlEventArgs customControlEventArgs = new CustomControlEventArgs
(
true,
this.nameTextBox.Text,
this.addressTextBox.Text,
this.cityTextBox.Text,
this.stateTextBox.Text,
this.zipTextBox.Text
);
ButtonClick(this, customControlEventArgs);
}
#endregion
#region Cancel 버튼 클릭시 처리하기 - cancelButton_Click(sender, e)
/// <summary>
/// Cancel 버튼 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void cancelButton_Click(object sender, EventArgs e)
{
CustomControlEventArgs customControlEventArgs = new CustomControlEventArgs
(
false,
this.nameTextBox.Text,
this.addressTextBox.Text,
this.cityTextBox.Text,
this.stateTextBox.Text,
this.zipTextBox.Text
);
ButtonClick(this, customControlEventArgs);
}
#endregion
}
}
[TestProject 프로젝트]
▶ MainWindow.xaml
<Window x:Class="TestProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:library="clr-namespace:TestLibrary;assembly=TestLibrary"
Width="800"
Height="600"
Title="TestProject"
FontFamily="나눔고딕코딩"
FontSize="16">
<DockPanel Margin="10">
<DockPanel.Resources>
<Style x:Key="InlineTextStyleKey" TargetType="{x:Type Inline}">
<Setter Property="FontWeight" Value="Normal" />
</Style>
<Style x:Key="TitleTextBlockKey" TargetType="{x:Type TextBlock}">
<Setter Property="DockPanel.Dock" Value="Top" />
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="Margin" Value="10 10 10 0" />
</Style>
</DockPanel.Resources>
<StackPanel DockPanel.Dock="Left"
Width="250"
Background="Bisque">
<TextBlock
Margin="10 10 10 10"
FontWeight="Bold">
Control Properties
</TextBlock>
<TextBlock Style="{StaticResource TitleTextBlockKey}">Background Color</TextBlock>
<StackPanel Margin="10 10 10 10">
<RadioButton Name="originalBackgroundColorRadioButton"
IsChecked="True">
Original
</RadioButton>
<RadioButton Name="lightGreenBackgroundColorRadioButton">
LightGreen
</RadioButton>
<RadioButton Name="lightSalmonBackgroundColorRadioButton">
LightSalmon
</RadioButton>
</StackPanel>
<TextBlock Style="{StaticResource TitleTextBlockKey}">Foreground Color</TextBlock>
<StackPanel Margin="10 10 10 10">
<RadioButton Name="originalForegroundColorRadioButton"
IsChecked="True">
Original
</RadioButton>
<RadioButton Name="redForegroundColorRadioButton">
Red
</RadioButton>
<RadioButton Name="yellowForegroundColorReadioButton">
Yellow
</RadioButton>
</StackPanel>
<TextBlock Style="{StaticResource TitleTextBlockKey}">Font Family</TextBlock>
<StackPanel Margin="10 10 10 10">
<RadioButton Name="originalFontFamilyRadioButton"
IsChecked="True">
Original
</RadioButton>
<RadioButton Name="timesNewRomanFontFamilyRadioButton">
Times New Roman
</RadioButton>
<RadioButton Name="wingdingsFontFamilyRadioButton">
Wingdings
</RadioButton>
</StackPanel>
<TextBlock Style="{StaticResource TitleTextBlockKey}">Font Size</TextBlock>
<StackPanel Margin="10 10 10 10">
<RadioButton Name="originalFontSizeRadioButton"
IsChecked="True">
Original
</RadioButton>
<RadioButton Name="tenFontSizeRadioButton">
10
</RadioButton>
<RadioButton Name="twelveFontSizeRadioButton">
12
</RadioButton>
</StackPanel>
<TextBlock Style="{StaticResource TitleTextBlockKey}">Font Style</TextBlock>
<StackPanel Margin="10 10 10 10">
<RadioButton Name="normalFontStyleRadioButton"
IsChecked="True">
Original
</RadioButton>
<RadioButton Name="italicFontStyleRadioButton">
Italic
</RadioButton>
</StackPanel>
<TextBlock Style="{StaticResource TitleTextBlockKey}">Font Weight</TextBlock>
<StackPanel Margin="10 10 10 10">
<RadioButton Name="originalFontWeightRadioButton"
IsChecked="True">
Original
</RadioButton>
<RadioButton Name="boldFontWeightRadioButton">
Bold
</RadioButton>
</StackPanel>
</StackPanel>
<WindowsFormsHost Name="windowsFormsHost" DockPanel.Dock="Top"
Height="300">
<library:CustomControl Name="customControl" />
</WindowsFormsHost>
<StackPanel
Height="Auto"
Orientation="Vertical"
Background="LightBlue">
<TextBlock
Margin="10 10 10 10"
FontWeight="Bold">
Data From Control
</TextBlock>
<TextBlock Style="{StaticResource TitleTextBlockKey}">
Name : <Span Name="nameSpan" Style="{StaticResource InlineTextStyleKey}" />
</TextBlock>
<TextBlock Style="{StaticResource TitleTextBlockKey}">
Street Address : <Span Name="addressSpan" Style="{StaticResource InlineTextStyleKey}" />
</TextBlock>
<TextBlock Style="{StaticResource TitleTextBlockKey}">
City : <Span Name="citySpan" Style="{StaticResource InlineTextStyleKey}" />
</TextBlock>
<TextBlock Style="{StaticResource TitleTextBlockKey}">
State : <Span Name="stateSpan" Style="{StaticResource InlineTextStyleKey}" />
</TextBlock>
<TextBlock Style="{StaticResource TitleTextBlockKey}">
Zip : <Span Name="zipSpan" Style="{StaticResource InlineTextStyleKey}" />
</TextBlock>
</StackPanel>
</DockPanel>
</Window>
▶ MainWindow.xaml.cs
using System;
using System.Windows;
using System.Windows.Media;
using TestLibrary;
namespace TestProject
{
/// <summary>
/// 메인 윈도우
/// </summary>
public partial class MainWindow : Window
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 애플리케이션
/// </summary>
private Application application;
/// <summary>
/// 윈도우
/// </summary>
private Window window;
/// <summary>
/// 초기 배경 브러시
/// </summary>
private SolidColorBrush initialBackgroundBrush;
/// <summary>
/// 초기 전경 브러시
/// </summary>
private SolidColorBrush initialForegroundBrush;
/// <summary>
/// 초기 폰트 패밀리
/// </summary>
private FontFamily initialFontFamily;
/// <summary>
/// 폰트 크기
/// </summary>
private double initialFontSize;
/// <summary>
/// 초기 폰트 스타일
/// </summary>
private FontStyle initialFontStyle;
/// <summary>
/// 초기 폰트 가중치
/// </summary>
private FontWeight initialFontWeight;
/// <summary>
/// 준비 여부
/// </summary>
private bool ready = false;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainWindow()
/// <summary>
/// 생성자
/// </summary>
public MainWindow()
{
InitializeComponent();
Loaded += Window_Loaded;
this.originalBackgroundColorRadioButton.Click += backgroundColorRadioButton_Click;
this.lightGreenBackgroundColorRadioButton.Click += backgroundColorRadioButton_Click;
this.lightSalmonBackgroundColorRadioButton.Click += backgroundColorRadioButton_Click;
this.originalForegroundColorRadioButton.Click += foregroundColorRadioButton_Click;
this.redForegroundColorRadioButton.Click += foregroundColorRadioButton_Click;
this.yellowForegroundColorReadioButton.Click += foregroundColorRadioButton_Click;
this.originalFontFamilyRadioButton.Click += fontFamilyRadioButton_Click;
this.timesNewRomanFontFamilyRadioButton.Click += fontFamilyRadioButton_Click;
this.wingdingsFontFamilyRadioButton.Click += fontFamilyRadioButton_Click;
this.originalFontSizeRadioButton.Click += fontSizeRadioButton_Click;
this.tenFontSizeRadioButton.Click += fontSizeRadioButton_Click;
this.twelveFontSizeRadioButton.Click += fontSizeRadioButton_Click;
this.normalFontStyleRadioButton.Click += fontStyleRadioButton_Click;
this.italicFontStyleRadioButton.Click += fontStyleRadioButton_Click;
this.originalFontWeightRadioButton.Click += fontWeightRadioButton_Click;
this.boldFontWeightRadioButton.Click += fontWeightRadioButton_Click;
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Private
#region 윈도우 로드시 처리하기 - Window_Loaded(sender, e)
/// <summary>
/// 윈도우 로드시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void Window_Loaded(object sender, EventArgs e)
{
this.application = Application.Current;
this.window = this.application.MainWindow;
this.windowsFormsHost.TabIndex = 10;
this.initialBackgroundBrush = (SolidColorBrush)this.windowsFormsHost.Background;
this.initialForegroundBrush = (SolidColorBrush)this.windowsFormsHost.Foreground;
this.initialFontFamily = this.windowsFormsHost.FontFamily;
this.initialFontSize = this.windowsFormsHost.FontSize;
this.initialFontStyle = this.windowsFormsHost.FontStyle;
this.initialFontWeight = this.windowsFormsHost.FontWeight;
CustomControl customControl = this.windowsFormsHost.Child as CustomControl;
customControl.ButtonClick += customControl_ButtonClick;
this.ready = true;
}
#endregion
#region 배경색 라디오 버튼 클릭시 처리하기 - backgroundColorRadioButton_Click(sender, e)
/// <summary>
/// 배경색 라디오 버튼 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void backgroundColorRadioButton_Click(object sender, RoutedEventArgs e)
{
if(sender == this.lightGreenBackgroundColorRadioButton)
{
this.windowsFormsHost.Background = new SolidColorBrush(Colors.LightGreen);
}
else if(sender == this.lightSalmonBackgroundColorRadioButton)
{
this.windowsFormsHost.Background = new SolidColorBrush(Colors.LightSalmon);
}
else if(this.ready == true)
{
this.windowsFormsHost.Background = this.initialBackgroundBrush;
}
}
#endregion
#region 전경색 라디오 버튼 클릭시 처리하기 - foregroundColorRadioButton_Click(sender, e)
/// <summary>
/// 전경색 라디오 버튼 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void foregroundColorRadioButton_Click(object sender, RoutedEventArgs e)
{
if(sender == this.redForegroundColorRadioButton)
{
this.windowsFormsHost.Foreground = new SolidColorBrush(Colors.Red);
}
else if(sender == this.yellowForegroundColorReadioButton)
{
this.windowsFormsHost.Foreground = new SolidColorBrush(Colors.Yellow);
}
else if(this.ready == true)
{
this.windowsFormsHost.Foreground = this.initialForegroundBrush;
}
}
#endregion
#region 폰트 패밀리 라디오 버튼 클릭시 처리하기 - fontFamilyRadioButton_Click(sender, e)
/// <summary>
/// 폰트 패밀리 라디오 버튼 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void fontFamilyRadioButton_Click(object sender, RoutedEventArgs e)
{
if(sender == this.timesNewRomanFontFamilyRadioButton)
{
this.windowsFormsHost.FontFamily = new FontFamily("Times New Roman");
}
else if(sender == this.wingdingsFontFamilyRadioButton)
{
this.windowsFormsHost.FontFamily = new FontFamily("Wingdings");
}
else if(this.ready == true)
{
this.windowsFormsHost.FontFamily = initialFontFamily;
}
}
#endregion
#region 폰트 크기 라디오 버튼 클릭시 처리하기 - fontSizeRadioButton_Click(sender, e)
/// <summary>
/// 폰트 크기 라디오 버튼 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void fontSizeRadioButton_Click(object sender, RoutedEventArgs e)
{
if(sender == this.tenFontSizeRadioButton)
{
this.windowsFormsHost.FontSize = 10;
}
else if(sender == this.twelveFontSizeRadioButton)
{
this.windowsFormsHost.FontSize = 12;
}
else if(this.ready == true)
{
this.windowsFormsHost.FontSize = this.initialFontSize;
}
}
#endregion
#region 폰트 스타일 라디오 버튼 클릭시 처리하기 - fontStyleRadioButton_Click(sender, e)
/// <summary>
/// 폰트 스타일 라디오 버튼 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void fontStyleRadioButton_Click(object sender, RoutedEventArgs e)
{
if(sender == this.italicFontStyleRadioButton)
{
this.windowsFormsHost.FontStyle = FontStyles.Italic;
}
else if(this.ready == true)
{
this.windowsFormsHost.FontStyle = this.initialFontStyle;
}
}
#endregion
#region 폰트 가중치 라디오 버튼 클릭시 처리하기 - fontWeightRadioButton_Click(sender, e)
/// <summary>
/// 폰트 가중치 라디오 버튼 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void fontWeightRadioButton_Click(object sender, RoutedEventArgs e)
{
if(sender == this.boldFontWeightRadioButton)
{
this.windowsFormsHost.FontWeight = FontWeights.Bold;
}
else if(this.ready == true)
{
this.windowsFormsHost.FontWeight = this.initialFontWeight;
}
}
#endregion
#region 커스텀 컨트롤 버튼 클릭시 처리하기 - customControl_ButtonClick(sender, e)
/// <summary>
/// 커스텀 컨트롤 버튼 클릭시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void customControl_ButtonClick(object sender, CustomControlEventArgs e)
{
this.nameSpan.Inlines.Clear();
this.addressSpan.Inlines.Clear();
this.citySpan.Inlines.Clear();
this.stateSpan.Inlines.Clear();
this.zipSpan.Inlines.Clear();
if(e.OKButtonClicked)
{
this.nameSpan.Inlines.Add (" " + e.Name );
this.addressSpan.Inlines.Add(" " + e.Address);
this.citySpan.Inlines.Add (" " + e.City );
this.stateSpan.Inlines.Add (" " + e.State );
this.zipSpan.Inlines.Add (" " + e.Zip );
}
}
#endregion
}
}
728x90
반응형
그리드형(광고전용)
'C# > WPF' 카테고리의 다른 글
[C#/WPF] WindowsFormsHost 엘리먼트 : Width/Height 속성을 사용해 명시적으로 크기 설정하기 (0) | 2022.12.29 |
---|---|
[C#/WPF] WindowsFormsHost 엘리먼트 : 절대 위치 지정 사용하기 (0) | 2022.12.29 |
[C#/WPF] WindowsFormsHost 엘리먼트 : FontSize 속성을 사용해 컨텐츠 크기 설정하기 (0) | 2022.12.29 |
[C#/WPF] WindowsFormsHost 엘리먼트 : 기본 레이아웃 사용하기 (0) | 2022.12.29 |
[C#/WPF] WindowsFormsHost 클래스 : 하이브리드 애플리케이션에서 시각적 스타일 활성화하기 (0) | 2022.12.28 |
[C#/WPF] WindowsFormsHost 엘리먼트 사용하기 (0) | 2022.12.26 |
[C#/WPF] WindowsFormsHost 클래스 : Child 속성 사용하기 (0) | 2022.12.26 |
[C#/WPF] LayoutInformation 클래스 : GetLayoutSlot 정적 메소드를 사용해 엘리먼트 경계 상자 구하기 (0) | 2022.12.25 |
[C#/WPF] Thread 클래스 : CurrentThread 정적 속성을 사용해 문화 정보 설정하기 (0) | 2022.12.25 |
[C#/WPF] Image 엘리먼트 : FlowDirection 속성 사용하기 (0) | 2022.12.24 |
댓글을 달아 주세요