WM_NOTIFY和ON_NOTIFY macro及OnNotify虚函数

WM_NOTIFY是子控件用来向父窗体发送信息的消息,其参数一般以NMHDR结构来封装,例如
            NMHDR nmh;
            memset(&nmh,0,sizeof(NMHDR));
            nmh.hwndFrom = GetSafeHwnd();
            nmh.idFrom = GetDlgCtrlID();
            nmh.code = NM_CLICK;
            GetParent()->SendMessage(WM_NOTIFY,GetDlgCtrlID(),(LPARAM)&nmh);
如此在父窗体上,OnNotify虚函数就能接收到这个消息,并且进行消息反射或者其他什么处理。而如果父窗体要自己来handle这个消息的话,这就需要通过ON_NOTIFY来进行消息影射,一般声明如下(以上面为例):
ON_NOTIFY(NM_CLICK,819,OnNotifyTabWnd)
afx_msg void OnNotifyTabWnd(NMHDR*pNotifyStruct,LRESULT*result);
void CMainFrame::OnNotifyTabWnd(NMHDR*pNotifyStruct,LRESULT*result)
{
}
WM_NOTIFY是为了扩展WM_COMMAND的,既如此,是否WM_COMMAND也是类似的消息传递路径呢?(是的)
具体可以参考MSDN,以下是片断:

OnNotify processes the message map for control notification.

Override this member function in your derived class to handle the WM_NOTIFY message. An override will not process the message map unless the base class OnNotify is called.

For more information on the WM_NOTIFY message, see Technical Note 61 (TN061), ON_NOTIFY and WM_NOTIFY messages. You may also be interested the related topics described in Control Topics, and TN062, Message Reflection for Windows Controls.

你可能感兴趣的:(C++,MFC)