Application/MFC

[MFC] WM_USER 사용 (User Message)

devsalix 2023. 2. 10. 18:12
728x90

우선 클래스 생성 시 상단에 메시지 ID를 정의하고 Dialog의 핸들을  받아오는 코드를 작성해 줍니다

 

#define DF_MSG_LOG (WM_USER + 0x01)

class CMyClass
{
public:
	CMyClass(HWND hWnd);
	~CMyClass(void);
	
private:
	HWND m_hWnd;
	
	void SendMsg();
}

 

그런 다음 클래스의 정의부는 아래와 같이 작성 합니다

 

CMyClass::CMyClass(HWND hWnd)
{
	m_hWnd = hWnd;
}

CMyClass::~CMyClass(void)
{
}

void CMyClass::SendMsg()
{
	SendMessage(m_hWnd, DF_MSG_LOG, (WPARAM)(LPCTSTR)_T("테스트"), NULL);
}

 

이렇게만 작성하면 클래스 부분의 작성은 모두 끝났습니다

 

그럼 Dialog 코드로 넘어가서

 

 

Header 파일에서 아래와 같이 클래스를 Include 후 메시지 처리 함수와 클래스를 등록해 줍니다

 

#include "MyClass.h"

class CMyDlg : public CDialogEx
{
	....
    
public:
	afx_msg LRESULT OnRecvMsg(WPARAM wParam, LPARAM lParam);
    
	CMyClass *m_pMyClass;
}

 

이후 Dialog의 Cpp 파일로 넘어와서 메시지 맵에 메시지를 등록 해 주고

 

BEGIN_MESSAGE_MAP(CMyDlg, CDialogEx)
	ON_MESSAGE(DF_MSG_LOG, &CMyDlg::OnRecvMsg)
END_MESSAGE_MAP()

 

이후에 OnInitDialog 함수 내 클래스 선언을 하고

 

메시지 처리를 위해 등록했던 함수를 작성해 줍니다

 

BOOL CMyDlg::OnInitDialog()
{
	m_pMyClass = new CMyClass(m_hWnd);
}

afx_msg LRESULT CMyDlg::OnRecvMsg(WPARAM wParam, LPARAM lParam)
{
	AfxMessageBox((LPCTSTR)wParam);

	return 0;
}

 

그럼 클래스에서 메시지를 전송하면 Dialog 쪽으로 메시지가 넘어오게 됩니다

 

 


제 글이 도움이 되셨다면 댓글 & 공감 부탁드려요 😀

 

 
728x90
반응형