일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- Encapusulation
- file access
- Read
- sensor
- Pointer
- Barcode
- c++
- wpf
- atmega328
- UNO
- preprocessing
- java
- Unity
- compare
- parameter
- memory
- mfc
- length
- aduino
- Class
- flutter
- digitalRead
- Contour
- APP
- Android
- 3D
- SERIAL
- stream
- public
- inheritance
- Today
- Total
폴크(FOLC)
VS2022 MFC U/I - WPF U/I 붙이기 본문
MFC 프로젝트에 WPF UI 화면을 삽입하고 이벤트를 처리하는 방법은 Interop 기술을 활용하는 방식으로 이루어집니다. Visual Studio 2022에서도 지원되며, 다음과 같은 구성요소와 절차가 필요합니다.
개요: MFC에 WPF 삽입 원리
MFC 프로젝트는 기본적으로 Win32 기반 C++ 환경이므로, WPF(.NET 기반)의 UI 요소를 삽입하기 위해 중간 호스트 컨트롤을 사용해야 합니다.
대표적인 방식:
- HwndSource: WPF → Win32 로 호스팅
- HwndHost: Win32 (MFC) → WPF를 포함할 때 사용
- ElementHost: Windows Forms에서 사용하지만, MFC에서는 C++/CLI를 통해 유사한 방식 사용
절차 요약
- WPF UserControl 생성 (.NET 프로젝트)
- C++/CLI 프로젝트 생성 (브리지 역할)
- MFC 프로젝트에 C++/CLI DLL 참조 및 WPF 삽입
- 이벤트 바인딩 처리
1단계: WPF UserControl 작성
예: MyWpfControl.xaml
xml 파일
<UserControl x:Class="MyInteropWpf.MyWpfControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
...>
<StackPanel>
<Button Content="Click Me" Click="OnClick"/>
</StackPanel>
</UserControl>
csharp 파일
public partial class MyWpfControl : UserControl
{
public MyWpfControl()
{
InitializeComponent();
}
public event EventHandler ButtonClicked;
private void OnClick(object sender, RoutedEventArgs e)
{
ButtonClicked?.Invoke(this, EventArgs.Empty);
}
}
2단계: C++/CLI 래퍼 프로젝트 생성
Visual Studio에서 CLR Class Library 생성 (예: WpfHostBridge)
// WpfHostBridge.h
#pragma once
using namespace System;
using namespace System::Windows;
using namespace System::Windows::Interop;
using namespace System::Windows::Controls;
namespace WpfHostBridge {
public ref class WpfHostControl : HwndHost
{
private:
MyInteropWpf::MyWpfControl^ _wpfControl;
HwndSource^ _source;
protected:
virtual IntPtr BuildWindowCore(HandleRef hwndParent) override
{
_wpfControl = gcnew MyInteropWpf::MyWpfControl();
_wpfControl->ButtonClicked += gcnew EventHandler(this, &WpfHostControl::OnButtonClicked);
_source = gcnew HwndSource(0, 0, 0, 0, 0,
"WPFHost", hwndParent.Handle);
_source->RootVisual = _wpfControl;
return (IntPtr)_source->Handle;
}
virtual void DestroyWindowCore(HandleRef hwnd) override
{
_source->Dispose();
}
void OnButtonClicked(Object^ sender, EventArgs^ e)
{
MessageBox::Show("WPF 버튼 클릭됨!");
}
};
}
- WPF UserControl을 생성하고 내부에서 버튼 클릭 이벤트를 처리
- HwndHost를 상속하여 MFC에 삽입 가능한 핸들을 노출
3단계: MFC 프로젝트에서 래퍼 사용
A. 참조 추가
- MFC 프로젝트에 C++/CLI DLL을 참조로 추가
- .NET Framework 4.x이상 사용
B. 컨트롤 호스팅
// MFC View나 Dialog 클래스에 포함
HWND hwndWpf;
WpfHostBridge::WpfHostControl^ host;
void CMyView::OnInitialUpdate()
{
CView::OnInitialUpdate();
// .NET 환경 초기화 필요
System::Windows::Forms::Application::EnableVisualStyles();
host = gcnew WpfHostBridge::WpfHostControl();
host->CreateHandle();
hwndWpf = (HWND)host->Handle.ToPointer();
::SetParent(hwndWpf, this->m_hWnd);
::MoveWindow(hwndWpf, 10, 10, 300, 200, TRUE);
}
4단계: 이벤트 통합
WPF → MFC 방향의 이벤트 처리:
- WPF에서 event 발생 → C++/CLI에서 캐치 → MFC로 메시지 전송
예: MFC에서 메시지 핸들러 등록
#define WM_WPF_BUTTON_CLICKED (WM_USER + 100)
LRESULT CMyView::OnWpfButtonClicked(WPARAM wParam, LPARAM lParam)
{
AfxMessageBox(_T("WPF 버튼 클릭 이벤트 수신"));
return 0;
}
그리고 C++/CLI에서 ::SendMessage()를 통해 전달할 수 있음.
요약
WPF UserControl | 사용자 UI + 이벤트 트리거 정의 |
C++/CLI 래퍼 | WPF를 호스트하고 이벤트 핸들러 래핑 |
MFC | UI 삽입 및 메시지 처리 |
MFC 프로젝트에 WPF UI와 이벤트를 통합하려면:
- WPF UI 작성
- C++/CLI 브리지 작성
- MFC에서 호스트 및 이벤트 처리
이 방식은 학습 비용은 있지만, 기존 MFC 자산을 최대한 유지하면서 WPF의 장점을 부분적으로 도입할 수 있는 매우 유연한 접근입니다.