■ WPF 응용 프로그램에서 데이터 입력을 수행하기 위해 Windows Forms 복합 컨트롤을 호스팅하는 응용 프로그램을 안내한다. 복합 컨트롤은 DLL에 패키지되어 있다. 이 일반적인 절차는 더 복잡한 응용 프로그램 및 제어로 확장될 수 있다.
[TestLibrary 프로젝트]
▶ CustomControlEventArgs.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
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
1 2 3 4 5 6 7 8 9 10 11 |
namespace TestLibrary { /// <summary> /// 커스텀 컨트롤 이벤트 핸들러 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> public delegate void CustomControlEventHandler(object sender, CustomControlEventArgs e); } |
▶ CustomControl.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
<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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
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 } } |