문제

나는 그 차이가 함수에 대한 MFC의 다음 두 메시지 트랩 사이의 차이가 무엇인지 궁금해했다 (onsize).

1- 메시지 맵을 통해 :

BEGIN_MESSAGE_MAP(CClassWnd, CBaseClassWnd)
...
    ON_WM_SIZE()
..
END_MESSAGE_MAP()

2- AFX_MESSAGE를 통해 :

afx_msg type OnSize(...);

그것들은 상호 교환 적으로 사용되는 것 같습니다. 어느 것이 사용되어야합니까, 아니면 다른 요인에 의존합니까?

도움이 되었습니까?

해결책

클래스에 메시지 처리기를 추가하려면 두 부분 모두가 필요합니다. 메시지 맵은 모든 메시지 핸들러 기능에 대한 선언과 함께 클래스 내에서 선언해야합니다 (예 : OnSize).

class CClassWnd : public CBaseClassWnd {
    ...
    afx_msg void OnSize(UINT nType, int cx, int cy);
    DECLARE_MESSAGE_MAP
};

afx_msg 빈 자리 표시 자 매크로 일뿐입니다. 실제로는 아무것도하지 않지만 항상 컨벤션에 포함됩니다.

그런 다음 메시지 맵은 클래스의 .CPP 파일에 정의됩니다.

BEGIN_MESSAGE_MAP(CClassWnd, CBaseClassWnd)
    ON_WM_SIZE()
END_MESSAGE_MAP()

이 매크로는 클래스에 대한 조회 테이블을 생성하여 창에서 수신 한 메시지를 해당 핸들러 기능으로 발송 할 수 있습니다. 그만큼 ON_WM_SIZE 매크로 허용 wParam 그리고 lParam 메시지 매개 변수 WM_SIZE 메시지 핸들러 함수에 대한보다 의미있는 값으로 디코딩되는 메시지 (nType, cx, 그리고 cy 이 경우). MFC는 대부분의 창 메시지에 매크로를 제공합니다 (WM_LBUTTONDOWN, WM_DESTROY, 등).

MFC에서 메시지 맵이 작동하는 방법에 대한 자세한 정보를 찾을 수 있습니다. 여기 MSDN에서.

다른 팁

afx_msg is just an empty macro, it's basically just there to denote that the method is an MFC message handler for readability purposes. Even with afx_msg there you still need to have an entry in the message map.

Some of the Windows message are already handled by MFC, so in these cases you can get away with adding just the method to your derived class.

For example the CWnd class (as do many other MFC classes) already maps a few Windows messages into it's message map (i.e. ON_WM_DRAWITEM, ON_WM_MEASUREITEM, ON_WM_ENTERIDLE etc, etc).

But any other message not already mapped by MFC will need to have both a class method and an entry in the message map for it to work.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top