728x90
반응형
728x170
▶ 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
반응형
그리드형(광고전용)
'C# > WPF' 카테고리의 다른 글
[C#/WPF] Frame 엘리먼트 : Source 속성을 사용해 리소스 XAML 페이지 로드하기 (0) | 2022.01.02 |
---|---|
[C#/WPF] Frame 클래스 : Source 속성을 사용해 리소스 XAML 페이지 로드하기 (0) | 2022.01.02 |
[C#/WPF] Application 클래스 : GetResourceStream 정적 메소드를 사용해 리소스 XAML 페이지 로드하기 (0) | 2022.01.02 |
[C#/WPF] VirtualizingStackPanel 클래스 : IsVirtualizing/VirtualizationMode 속성을 사용해 대용량 데이터 바인딩하기 (0) | 2021.12.31 |
[C#/WPF] Popup 클래스 : 다른 엘리먼트 위에 엘리먼트를 오버레이 배치하기 (0) | 2021.12.31 |
[C#/WPF] 3D 애니메이션 시계 사용하기 (절전 모드 방지 기능 추가) (0) | 2021.11.17 |
[C#/WPF] DataGrid 클래스 : 데이터 업데이트 성능 측정하기 (0) | 2021.10.22 |
[C#/WPF] DataGrid 클래스 : 대용량 데이터 바인딩하기 (0) | 2021.10.03 |
[C#/WPF/.NETCORE] FaceDetector 클래스 : DetectFacesAsync 메소드를 사용해 얼굴 탐지하기 (0) | 2021.09.23 |
[C#/WPF/.NETCORE] 누겟 설치 : Microsoft.Windows.SDK.Contracts (0) | 2021.09.23 |
댓글을 달아 주세요