728x90
반응형
728x170
■ WindowsFormsHost 클래스의 PropertyMap 속성을 사용하여 WPF 속성을 호스팅된 Windows Forms 컨트롤의 해당 속성에 매핑하는 방법을 보여준다.
▶ 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"
Width="800"
Height="600"
Title="TestProject"
FontFamily="나눔고딕코딩"
FontSize="16">
<DockPanel
Margin="10"
LastChildFill="True">
<WindowsFormsHost Name="windowsFormsHost" DockPanel.Dock="Left"
FontSize="20" />
</DockPanel>
</Window>
▶ MainWindow.xaml.cs
using System;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Forms.Integration;
using System.Windows.Media;
namespace TestProject
{
/// <summary>
/// 메인 윈도우
/// </summary>
public partial class MainWindow : Window
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 생성자 - MainWindow()
/// <summary>
/// 생성자
/// </summary>
public MainWindow()
{
InitializeComponent();
Loaded += Window_Loaded;
this.windowsFormsHost.SizeChanged += windowsFormsHost_SizeChanged;
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Private
//////////////////////////////////////////////////////////////////////////////// Event
#region 윈도우 로드시 처리하기 - Window_Loaded(sender, e)
/// <summary>
/// 윈도우 로드시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void Window_Loaded(object sender, RoutedEventArgs e)
{
System.Windows.Forms.Application.EnableVisualStyles();
System.Windows.Forms.CheckBox checkBox = new System.Windows.Forms.CheckBox();
checkBox.Dock = System.Windows.Forms.DockStyle.Fill;
checkBox.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
checkBox.Text = "Windows Forms checkbox";
checkBox.CheckedChanged += checkBox_CheckedChanged;
this.windowsFormsHost.Child = checkBox;
ReplaceFlowDirectionPropertyMapping();
RemoveCursorPropertyMapping();
AddClipPropertyMapping();
ExtendBackgroundPropertyMapping();
this.windowsFormsHost.FlowDirection = FlowDirection.LeftToRight;
this.windowsFormsHost.Clip = new RectangleGeometry();
this.windowsFormsHost.Background = new ImageBrush();
}
#endregion
#region 윈도우즈 폼 호스트 크기 변경시 처리하기 - windowsFormsHost_SizeChanged(sender, e)
/// <summary>
/// 윈도우즈 폼 호스트 크기 변경시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void windowsFormsHost_SizeChanged(object sender, SizeChangedEventArgs e)
{
ProcessClipPropertyTranslator(this.windowsFormsHost, "Clip", null);
}
#endregion
#region 체크 박스 체크 변경시 처리하기 - checkBox_CheckedChanged(sender, e)
/// <summary>
/// 체크 박스 체크 변경시 처리하기
/// </summary>
/// <param name="sender">이벤트 발생자</param>
/// <param name="e">이벤트 인자</param>
private void checkBox_CheckedChanged(object sender, EventArgs e)
{
System.Windows.Forms.CheckBox checkBox = sender as System.Windows.Forms.CheckBox;
if(checkBox.CheckState == System.Windows.Forms.CheckState.Checked)
{
this.windowsFormsHost.FlowDirection = FlowDirection.RightToLeft;
}
else
{
this.windowsFormsHost.FlowDirection = FlowDirection.LeftToRight;
}
}
#endregion
//////////////////////////////////////////////////////////////////////////////// Function
#region 클리핑 영역 생성하기 - CreateClippingRegion()
/// <summary>
/// 클리핑 영역 생성하기
/// </summary>
/// <returns>클리핑 영역</returns>
private System.Drawing.Region CreateClippingRegion()
{
System.Drawing.Drawing2D.GraphicsPath graphicsPath = new System.Drawing.Drawing2D.GraphicsPath();
graphicsPath.StartFigure();
graphicsPath.AddEllipse
(
new System.Drawing.Rectangle
(
0,
0,
(int)this.windowsFormsHost.ActualWidth,
(int)this.windowsFormsHost.ActualHeight
)
);
graphicsPath.CloseFigure();
return new System.Drawing.Region(graphicsPath);
}
#endregion
#region Clip 속성 번역자 처리하기 - ProcessClipPropertyTranslator(hostObject, propertyName, value)
/// <summary>
/// Clip 속성 번역자 처리하기
/// </summary>
/// <param name="hostObject">호스트 객체</param>
/// <param name="propertyName">속성명</param>
/// <param name="value">값</param>
private void ProcessClipPropertyTranslator(object hostObject, string propertyName, object value)
{
WindowsFormsHost windowsFormsHost = hostObject as WindowsFormsHost;
System.Windows.Forms.CheckBox checkBox = windowsFormsHost.Child as System.Windows.Forms.CheckBox;
if(checkBox != null)
{
checkBox.Region = CreateClippingRegion();
}
}
#endregion
#region FlowDirection 속성 번역자 처리하기 - ProcessFlowDirectionPropertyTranslator(hostObject, propertyName, value)
/// <summary>
/// FlowDirection 속성 번역자 처리하기
/// </summary>
/// <param name="hostObject">호스트 객체</param>
/// <param name="propertyName">속성명</param>
/// <param name="value">값</param>
private void ProcessFlowDirectionPropertyTranslator(object hostObject, string propertyName, object value)
{
WindowsFormsHost windowsFormsHost = hostObject as WindowsFormsHost;
FlowDirection flowDirection = (FlowDirection)value;
System.Windows.Forms.CheckBox checkBox = windowsFormsHost.Child as System.Windows.Forms.CheckBox;
checkBox.RightToLeft = (flowDirection == FlowDirection.RightToLeft) ? System.Windows.Forms.RightToLeft.Yes : System.Windows.Forms.RightToLeft.No;
}
#endregion
#region Background 속성 번역자 처리하기 - ProcessBackgroundPropertyTranslator(hostObject, propertyName, value)
/// <summary>
/// Background 속성 번역자 처리하기
/// </summary>
/// <param name="hostObject">호스트 객체</param>
/// <param name="propertyName">속성명</param>
/// <param name="value">값</param>
private void ProcessBackgroundPropertyTranslator(object hostObject, string propertyName, object value)
{
WindowsFormsHost windowsFormsHost = hostObject as WindowsFormsHost;
System.Windows.Forms.CheckBox checkBox = windowsFormsHost.Child as System.Windows.Forms.CheckBox;
ImageBrush imageBrush = value as ImageBrush;
if(imageBrush != null)
{
string assemblyFilePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string imageFilePath = Path.Combine(assemblyFilePath, "IMAGE", "sample.jpg");
checkBox.BackgroundImage = new System.Drawing.Bitmap(imageFilePath);
}
}
#endregion
#region Clip 속성 매핑 추가하기 - AddClipPropertyMapping()
/// <summary>
/// Clip 속성 매핑 추가하기
/// </summary>
private void AddClipPropertyMapping()
{
this.windowsFormsHost.PropertyMap.Add("Clip", new PropertyTranslator(ProcessClipPropertyTranslator));
}
#endregion
#region 커서 속성 매핑 제거하기 - RemoveCursorPropertyMapping()
/// <summary>
/// 커서 속성 매핑 제거하기
/// </summary>
private void RemoveCursorPropertyMapping()
{
this.windowsFormsHost.PropertyMap.Remove("Cursor");
}
#endregion
#region FlowDirection 속성 매핑 변경하기 - ReplaceFlowDirectionPropertyMapping()
/// <summary>
/// FlowDirection 속성 매핑 변경하기
/// </summary>
private void ReplaceFlowDirectionPropertyMapping()
{
this.windowsFormsHost.PropertyMap.Remove("FlowDirection");
this.windowsFormsHost.PropertyMap.Add("FlowDirection", new PropertyTranslator(ProcessFlowDirectionPropertyTranslator));
}
#endregion
#region Background 속성 매핑 확장하기 - ExtendBackgroundPropertyMapping()
/// <summary>
/// Background 속성 매핑 확장하기
/// </summary>
private void ExtendBackgroundPropertyMapping()
{
if(this.windowsFormsHost.PropertyMap["Background"] != null)
{
this.windowsFormsHost.PropertyMap["Background"] += new PropertyTranslator(ProcessBackgroundPropertyTranslator);
}
}
#endregion
}
}
728x90
반응형
그리드형(광고전용)
'C# > WPF' 카테고리의 다른 글
[C#/WPF] 백그라운드 스레드로 차단 작업 처리하기 (0) | 2023.01.08 |
---|---|
[C#/WPF] DispatcherObject 클래스 : Dispatcher 속성을 사용해 장기 실행 계산이 포함된 단일 스레드 애플리케이션 만들기 (0) | 2023.01.07 |
[C#/WPF] ObjectCache 클래스 : 응용 프로그램 데이터 캐싱하기 (0) | 2023.01.06 |
[C#/WPF] RenderOptions 클래스 : SetCachingHint 정적 메소드를 사용해 캐싱 힌트 옵션 설정하기 (0) | 2023.01.04 |
[C#/WPF] HwndHost 클래스 : Win32 컨트롤 호스트하기 (0) | 2023.01.04 |
[C#/WPF] WindowsFormsHost 엘리먼트 : 하이브리드 애플리케이션에서 데이터 바인딩하기 (0) | 2022.12.31 |
[C#/WPF] WindowsFormsHost 엘리먼트 : WinForm 레이아웃 컨트롤 사용하기 (0) | 2022.12.29 |
[C#/WPF] WindowsFormsHost 엘리먼트 : Margin/Padding 속성 사용하기 (0) | 2022.12.29 |
[C#/WPF] WindowsFormsHost 엘리먼트 : 회전 설정하기 (0) | 2022.12.29 |
[C#/WPF] WindowsFormsHost 엘리먼트 : 스케일링 설정하기 (0) | 2022.12.29 |
댓글을 달아 주세요