How to send a notification that's handled by ON_NOTIFY?

╄→гoц情女王★ 提交于 2019-12-04 01:56:27

问题


I'm trying to post a LVN_ ITEMCHANGED to my custom gridlist's owner. I know how to send a WM_ User message using PostMessage (as shown here)

::PostMessage( AfxGetMainWnd()->GetSafeHwnd(), WM_REFRESH, (WPARAM)pBuffer, (LPARAM)GetOutputIdx() );

When I use this same code to send a LVN_ITEMCHANGED message though,

::PostMessage( AfxGetMainWnd()->GetSafeHwnd(), LVN_ITEMCHANGED, 0, 0);

it doesn't seem to be caught by the

ON_NOTIFY(LVN_ITEMCHANGED, ..., ...) 

I have in the owner class.

Am I wrong to be using ::PostMessage to send a Notify event?
Is there a difference between Notify messages and WM_ prefix messages or how they're handled?
Can someone post a sample of how I would send the message properly?

Thanks in advance.

Edit
I found another solution to the problem. See my answer below.


回答1:


Send WM_NOTIFY, pass control id as wParam and NMHDR* as lParam.

You'll need to allocate an NMHDR variable and fill it appropriately - set code to LVN_ITEMCHANGED and idFrom to the control id. You can only use SendMessage(), not PostMessage() since the receiving party will directly read memory through the NMHDR* pointer.

Smth like this:

NMHDR nmhdr;
nmhdr.code = LVN_ITEMCHANGED;
nmhdr.idFrom = controlId;
nmhdr.hwndFrom = controlWindowHandle;
SendMessage( targetWindowHandle, WM_NOTIFY, controlId, &nmhdr );



回答2:


I found out that I could override the message handler in my derived class and pass the message on to my parent control simply by using this code in the message map:

ON_NOTIFY_REFLECT_EX(LVN_ITEMCHANGED, OnListItemChanged)

Then in OnListItemChanged, I first call the base class function then return FALSE. This causes the message to be reflected to the parent class effortlessly.



来源:https://stackoverflow.com/questions/1272398/how-to-send-a-notification-thats-handled-by-on-notify

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!