본문 바로가기

프로그래밍/MFC

[MFC Dialog] PreTranslateMessage


특정 컨트롤러나 Dialog에 이벤트를 주고 싶다. 하면 그냥 이벤트 등록하면 간단한 일이다. 하지만 특정 부분에 대해서, 세세하게 이벤트를 달아주고 싶다면, 이 방법을 써보자.



PreTranslateMessage



MSDN -
Override this function to filter window messages before they are dispatched to the Windows functions TranslateMessage and DispatchMessage The default implementation performs accelerator-key translation, so you must call the CWinApp::PreTranslateMessage member function in your overridden version.

before 이하가 중요하다. - 영어시간 아닙니다 (웃음) 

TranslateMessage, DispatchMessage 에 보내지기 전에 걸러내기 위해 작성된 함.수.란다.

맴버를 보면 

virtual BOOL PreTranslateMessage(   MSG* pMsg );
인데, pMsg는 또 포인터 구조체 란다.
 

pMsg
  A pointer to a MSG structure that contains the message to process.
저는 MSDN을 신뢰합니다.

그래서,, MSG 구조체를 살펴보면,

typedef struct tagMSG {     // msg    
     HWND hwnd;   
     UINT message;   
     WPARAM wParam;   
     LPARAM lParam;   
     DWORD time;   
     POINT pt;
} MSG;
hWnd
  Identifies the window whose window procedure receives the message.

message
  Specifies the message number.

wParam
   
Specifies additional information about the message. The exact meaning depends on the value of the message member.

lParam
   
Specifies additional information about the message. The exact meaning depends on the value of the message member.

time
  
Specifies the time at which the message was posted.

pt
  
Specifies the cursor position, in screen coordinates, when the message was posted.


한글로 봅시다.

hwnd : 메시지를 받는 윈도우 프로시져를 가진 윈도우의 식별자. (누구꺼냐고)
message : 메시지 번호는 윈도우 메시지들. WM형제들
wParam, lParam : 메시지 전달인자 Virtual key codes 
time :  메시지 발생 시간
pt : 메시지가 발생한 지점, 그러니까 좌표계, point형이니까

 



예)
BOOL CsunClientDlg::PreTranslateMessage(MSG* pMsg)
{
	// TODO: 여기에 특수화된 코드를 추가 및/또는 기본 클래스를 호출합니다.
		if( pMsg->hwnd == m_edtUserId.GetSafeHwnd() &&	// 이 이벤트가 일어난 곳이 m_edtUserId라는 컨트롤이고
		pMsg->message == WM_KEYDOWN &&					// 무슨 키가 눌러졌는데 
		pMsg->wParam == VK_RETURN)								// 그 키가 엔터키이면
	{}
	return false; //CDialog::PreTranslateMessage(pMsg);
}
※ GetSafeHwnd()는 자신의 핸들을 가져오는 메소드 입니다.
※ message는 키보드, 마우스 다 가능합니다. 

이런 식으로 작성하면 어디에서든 원하는 이벤트를 가져올 수 있다.

자 이제 컨트롤에 이벤트를 달아보자 :D 
와아아아아아와와오아와아아ㅗ아ㅗ아ㅗ아와아아앙아아오아ㅗ아와오아 (퍽 

ㅇ>-< 

'프로그래밍 > MFC' 카테고리의 다른 글

[MFC] 포인터 해제?  (0) 2011.11.14
[MFC] SendMessage  (4) 2011.10.27
[MFC Dialog] OnKeyDown message  (0) 2011.10.27
[MFC Dialog] TreeCtrl에 더블클릭 이벤트 주기 ON_NOTIFY  (0) 2011.10.19
[MFC Dialog 메신져] TreeCtrl 탐색  (0) 2011.10.18