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

TestProject.zip
0.01MB

▶ WindowLongFlag.cs

namespace TestProject
{
    /// <summary>
    /// 윈도우 LONG 플래그
    /// </summary>
    public enum WindowLongFlag : int
    {
        /// <summary>
        /// GWL_EXSTYLE
        /// </summary>
        GWL_EXSTYLE = -20,

        /// <summary>
        /// GWLP_HINSTANCE
        /// </summary>
        GWLP_HINSTANCE = -6,

        /// <summary>
        /// GWLP_HWNDPARENT
        /// </summary>
        GWLP_HWNDPARENT = -8,

        /// <summary>
        /// GWL_ID
        /// </summary>
        GWL_ID = -12,

        /// <summary>
        /// GWL_STYLE
        /// </summary>
        GWL_STYLE = -16,

        /// <summary>
        /// GWL_USERDATA
        /// </summary>
        GWL_USERDATA = -21,

        /// <summary>
        /// GWL_WNDPROC
        /// </summary>
        GWL_WNDPROC = -4,

        /// <summary>
        /// DWLP_USER
        /// </summary>
        DWLP_USER = 0x8,

        /// <summary>
        /// DWLP_MSGRESULT
        /// </summary>
        DWLP_MSGRESULT = 0x0,

        /// <summary>
        /// DWLP_DLGPROC
        /// </summary>
        DWLP_DLGPROC = 0x4
    }
}

 

728x90

 

▶ CustomHwndHost.cs

using System;
using System.Runtime.InteropServices;
using System.Windows.Interop;

namespace TestProject
{
    /// <summary>
    /// 커스텀 윈도우 핸들 호스트
    /// </summary>
    public class CustomHwndHost : HwndHost
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Import
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Private

        #region 윈도우 LONG 설정하기 - SetWindowLong(windowHandle, flag, value)

        /// <summary>
        /// 윈도우 LONG 설정하기
        /// </summary>
        /// <param name="windowHandle">윈도우 핸들</param>
        /// <param name="flag">플래그</param>
        /// <param name="value">값</param>
        /// <returns>처리 결과</returns>
        [DllImport("user32")]
        private static extern int SetWindowLong(IntPtr windowHandle, WindowLongFlag flag, int value);

        #endregion
        #region 부모 설정하기 - SetParent(childWindowHandle, parentWindowHandle)

        /// <summary>
        /// 부모 설정하기
        /// </summary>
        /// <param name="childWindowHandle">자식 윈도우 핸들</param>
        /// <param name="parentWindowHandle">부모 윈도우 핸들</param>
        /// <returns>이전 부모 윈도우 핸들</returns>
        [DllImport("user32")]
        private static extern IntPtr SetParent(IntPtr childWindowHandle, IntPtr parentWindowHandle);

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// WS_CHILD
        /// </summary>
        private const int WS_CHILD = 0x40000000;

        /// <summary>
        /// 자식 윈도우 핸들
        /// </summary>
        private IntPtr childWindowHandle = IntPtr.Zero;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - CustomHwndHost(childWindowHandle)

        /// <summary>
        /// 생성자
        /// </summary>
        /// <param name="childWindowHandle">자식 윈도우 핸들</param>
        public CustomHwndHost(IntPtr childWindowHandle)
        {
            this.childWindowHandle = childWindowHandle;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 윈도우 만들기 (코어) - BuildWindowCore(parentWindowHandleRef)

        /// <summary>
        /// 윈도우 만들기 (코어)
        /// </summary>
        /// <param name="parentWindowHandleRef">부모 윈도우 핸들 참조</param>
        /// <returns>자식 윈도우 핸들 참조</returns>
        protected override HandleRef BuildWindowCore(HandleRef parentWindowHandleRef)
        {
            HandleRef childWIndowHandleRef = new HandleRef();

            if(this.childWindowHandle != IntPtr.Zero)
            {
                SetWindowLong(this.childWindowHandle, WindowLongFlag.GWL_STYLE, WS_CHILD);

                SetParent(this.childWindowHandle, parentWindowHandleRef.Handle);

                childWIndowHandleRef = new HandleRef(this, this.childWindowHandle);
            }

            return childWIndowHandleRef;
        }

        #endregion
        #region 윈도우 제거하기 (코어) - DestroyWindowCore(windowHandleRef)

        /// <summary>
        /// 윈도우 제거하기 (코어)
        /// </summary>
        /// <param name="windowHandleRef">윈도우 핸들 참조</param>
        protected override void DestroyWindowCore(HandleRef windowHandleRef)
        {
        }

        #endregion
    }
}

 

반응형

 

▶ 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="HwndHost 클래스 : WPF 윈도우 내부에서 메모장 윈도우 호스팅하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Grid Margin="10"
        Background="Transparent"
        UseLayoutRounding="True"
        SnapsToDevicePixels="True">
        <Border Name="border"
            BorderThickness="1"
            BorderBrush="Black" />
    </Grid>
</Window>

 

300x250

 

▶ MainWindow.xaml.cs

using System.Diagnostics;
using System.Windows;

namespace TestProject
{
    /// <summary>
    /// 메인 윈도우
    /// </summary>
    public partial class MainWindow : Window
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainWindow()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();

            #region 노트패드 프로세스를 생성한다.

            Process process = new Process();
            
            process.StartInfo.FileName = "notepad.exe";
            process.StartInfo.Arguments = null;

            process.Start();

            process.WaitForInputIdle(100);

            #endregion

            CustomHwndHost host = new CustomHwndHost(process.MainWindowHandle);

            this.border.Child = host;
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요