[MFC] SendMessage 예제
본문 바로가기

응용 프로그램 개발/C++, MFC, Windows

[MFC] SendMessage 예제

728x90
반응형
global.h // 공통코드 헤더

#define UWM_USER_MSG	(WM_USER + 10) // 사용자 정의 메세지 선언

 

 

CDlgTest.cpp // 메세지를 받는 클래스

BEGIN_MESSAGE_MAP(CDlgTest, CDialogEX)
  ON_MESSAGE(UWM_USER_MSG, OnReceiveData) // 메세지를 받았을때 수행할 함수
END_MESSAGE_MAP()

LRESULT CDlgTest::OnReceiveData((WPARAM)wParam, (LPARAM)lParam )
{
  // 메시지를 받아서 처리하는 함수
  CString *msg = (CString*) wParam;
  AfxMessageBox(msg->GetString());
  return 0;
}

 

 

CDlgMain.cpp // 메세지를 보내는 클래스

void CDlgMain::OnInitDialog()
{
  CDialogEX::OnInitDialog();

  SetNotifyWnd(GetSafeHwnd(), UWM_USER_MSG);
  if((m_hNotifyWnd != 0) && (m_nNotifyMsgId > 0))
  {
    ::SendMessage(m_hNotifyWnd, m_nNotifyMsgId, (WPARAM)(&msg), (LPARAM)NULL);
  }
  
}

void CDlgMain::SetNotifyWnd(HWND hNotifyWnd, UINT nNotifyMsgID)
{
	m_hNotifyWnd = hNotifyWnd;
	m_nNotifyMsgID = nNotifyMsgID;
}

 

SendMessage : 리턴 메세지를 받아야 다음 코드를 실행

PostMessage : 리턴 메세지를 받지 않아도 다음 코드 진행

728x90
반응형