Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a WM_USER handler with MFC

Tags:

c++

windows

mfc

Though I've had plenty of software experience I've not done a great deal of Windows programming. I'm trying to post a WM_USER message from a thread so that it gets picked up in the main UI thread but I'm having some trouble. This is in C++ using VS2010 and MFC.

I've created a message map thus,

#define WM_MYMSG (WM_USER + 77)
BEGIN_MESSAGE_MAP(CMyApp, CWinApp)
    ON_MESSAGE(WM_MYMSG, DoSomething)
END_MESSAGE_MAP()

Declared the handler function as follows,

afx_msg LRESULT DoSomething(WPARAM wParam, LPARAM lParam);

And written the function body as,

LRESULT CMyApp::DoSomething( WPARAM wParam, LPARAM lParam ) 
{
    UNREFERENCED_PARAMETER(wParam);
    UNREFERENCED_PARAMETER(lParam);

    CallSomeFunction();
    return 0L;
}

As far as I can see this is all in line with what MSDN says as stated here.

http://msdn.microsoft.com/en-gb/library/k35k2bfs(v=vs.100).aspx

However I'm getting an

error C2440: 'static_cast' : cannot convert from 'LRESULT (__cdecl CMyApp::*)(WPARAM,LPARAM)' to 'LRESULT (__cdecl CWnd::* )(WPARAM,LPARAM)'

relating to the line

ON_MESSAGE(WM_AVATAR_SCALE_MSG, DoSomething)

Can anyone let me know what the problem is?

Thanks for reading.

Paul

like image 368
PaulN Avatar asked Jun 03 '26 10:06

PaulN


1 Answers

CMyApp inherits from CWinApp, which inherits from CWinThread. CWinThread user-defined messages take a special macro in their message map for what you want to do:

Change this:

ON_MESSAGE(WM_AVATAR_SCALE_MSG, DoSomething)

To this:

ON_THREAD_MESSAGE(WM_AVATAR_SCALE_MSG, DoSomething)

Assuming DoSomething() is a member of your CMyApp class.

See the documentation on ON_THREAD_MESSAGE() for more information.

like image 168
WhozCraig Avatar answered Jun 06 '26 01:06

WhozCraig



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!