첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

■ WindowsFormsHost 클래스의 PropertyMap 속성을 사용하여 WPF 속성을 호스팅된 Windows Forms 컨트롤의 해당 속성에 매핑하는 방법을 보여준다.

TestProject.zip
0.04MB

▶ 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
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요