Creating a WM_USER handler with MFC

13,589

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.

Share:
13,589
PaulN
Author by

PaulN

Updated on June 28, 2022

Comments

  • PaulN
    PaulN almost 2 years

    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