tags:

views:

33

answers:

3

Hi, I'm trying to hook into the OnSysCommand function but I'm getting a confusing error.

In the header, I am declaring the function like:

afx_msg void OnSysCommand(UINT nID, LPARAM lParam );

And in the cpp the code is:

BEGIN_MESSAGE_MAP(CMFCTest1App, CWinAppEx)
 ON_COMMAND(ID_APP_ABOUT, &CMFCTest1App::OnAppAbout)
 // Standard file based document commands
 ON_COMMAND(ID_FILE_NEW, &CWinAppEx::OnFileNew)
 ON_COMMAND(ID_FILE_OPEN, &CWinAppEx::OnFileOpen)
 ON_WM_SYSCOMMAND()
END_MESSAGE_MAP()

void CMFCTest1App::OnSysCommand(UINT nID, LPARAM lParam )
{
 AfxMessageBox(L"System command recieved");
}

When compiling I get the following error message:

1> MFCTest1.cpp 1>c:\users\dell3\documents\visual studio 2010\projects\mfctest1\mfctest1\mfctest1.cpp(43): error C2440: 'static_cast' : cannot convert from 'void (__thiscall CMFCTest1App::* )(UINT,LPARAM)' to 'void (__thiscall CWnd::* )(UINT,LPARAM)' 1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast 1> 1>Build FAILED.

The same thing happens when I try to hook into the OnClose function. Any suggestions would be much appreciated. Thanks.

A: 

I think your CMFCTest1App has to inherit from CWnd. Anyhow, it is easier if you use the wizard to create message mappings.

vulkanino
+1  A: 

Move the handlers out of your app class and into your window or frame class.

These messages are meant to be handled in a window class (derived from CWnd) and not in your app class (derived from CWinApp).

Nate
Thanks- moved the message to the CMainFrame class and it compiled fine.
g32331
A: 

Your CMFCTest1App class does not have to inherit from CWnd. It's fine the way it is. Also, you can leave your ID_FILE_OPEN, etc command handlers in that class. It is a bit confusing for beginners, but it actually makes sense.

The OnSysCommand() message handler is not one of these exceptions. As others have already mentioned, it belongs to a certain window. The WM_SYSCOMMAND doc. explains in detail what it does.

So, move the command handler to the window it belongs you and you're good.

Thanks for the explanation, and the useful link.
g32331
If you like an answer, you can upvote it by clicking on the up-arrow.