Can't use 'User-defined-Message' in APP class of dialog-based MFC application. Why?
-
I created a project with VS2008, dialog-based MFC. What I want to do with that project is Sending 'User-defined-Message' in APP Class. So I registered my own message in app class(xxxApp.h) like this: #define USER_MESSAGE_2 (WM_USER+ 102) And, in message map, I added ON_MESSAGE code in app class(xxxApp.cpp) like below: BEGIN_MESSAGE_MAP(CwmUserApp, CWinAppEx) ON_COMMAND(ID_HELP, &CWinApp::OnHelp) ON_MESSAGE( USER_MESSAGE_2, OnMyMessage1HandlerApp ) ... END_MESSAGE_MAP() Also I wrote message handler like this: LRESULT CwmUserApp::OnMyMessage1HandlerApp(WPARAM wParam, LPARAM lParam) { AfxMessageBox(_T("Is it working?")); return 0; } I compiiled. Then one error came up: error C2440: 'static_cast': 'LRESULT (__thiscall CwmUserApp::* )(WPARAM,LPARAM)'에서 'LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)'(으)로 변환할 수 없습니다. Sorry, it's korean. If I translate, it says Can not convert from 'LRESULT (__thiscall CwmUserApp::* )(WPARAM,LPARAM)' to 'LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)' I am not that good with programming, but as much as I understand, Does that say I simply cannot use 'User-defined-Message' in APP class? 'User-defined-Message' in dlg class or view class works fine with me though. Thanks in advance :) chan
-
I created a project with VS2008, dialog-based MFC. What I want to do with that project is Sending 'User-defined-Message' in APP Class. So I registered my own message in app class(xxxApp.h) like this: #define USER_MESSAGE_2 (WM_USER+ 102) And, in message map, I added ON_MESSAGE code in app class(xxxApp.cpp) like below: BEGIN_MESSAGE_MAP(CwmUserApp, CWinAppEx) ON_COMMAND(ID_HELP, &CWinApp::OnHelp) ON_MESSAGE( USER_MESSAGE_2, OnMyMessage1HandlerApp ) ... END_MESSAGE_MAP() Also I wrote message handler like this: LRESULT CwmUserApp::OnMyMessage1HandlerApp(WPARAM wParam, LPARAM lParam) { AfxMessageBox(_T("Is it working?")); return 0; } I compiiled. Then one error came up: error C2440: 'static_cast': 'LRESULT (__thiscall CwmUserApp::* )(WPARAM,LPARAM)'에서 'LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)'(으)로 변환할 수 없습니다. Sorry, it's korean. If I translate, it says Can not convert from 'LRESULT (__thiscall CwmUserApp::* )(WPARAM,LPARAM)' to 'LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)' I am not that good with programming, but as much as I understand, Does that say I simply cannot use 'User-defined-Message' in APP class? 'User-defined-Message' in dlg class or view class works fine with me though. Thanks in advance :) chan
check the definition of ON_MESSAGE macro in afxmsg_.h
See: The type of the function must be afx_msg LRESULT (CWnd::*)(WPARAM, LPARAM). -
check the definition of ON_MESSAGE macro in afxmsg_.h
See: The type of the function must be afx_msg LRESULT (CWnd::*)(WPARAM, LPARAM).The type of the function must be afx_msg LRESULT (CWnd::*)(WPARAM, LPARAM). Which means, Some kind of type casting is needed? I don't follow the details how to fix this problem; Would you be more specific? Thanks in advance Chan
-
The type of the function must be afx_msg LRESULT (CWnd::*)(WPARAM, LPARAM). Which means, Some kind of type casting is needed? I don't follow the details how to fix this problem; Would you be more specific? Thanks in advance Chan
specifically, you have to write your ON_MESSAGE handler function in a CWnd derived class. If you have a window class CMyWnd which is derived from CWnd, then message map entry will be ON_MESSAGE( USER_MESSAGE_2, &CMyWnd::OnMyMessage1HandlerApp ) Declare and define OnMyMessage1HandlerApp function in CMyWnd class.
-
I created a project with VS2008, dialog-based MFC. What I want to do with that project is Sending 'User-defined-Message' in APP Class. So I registered my own message in app class(xxxApp.h) like this: #define USER_MESSAGE_2 (WM_USER+ 102) And, in message map, I added ON_MESSAGE code in app class(xxxApp.cpp) like below: BEGIN_MESSAGE_MAP(CwmUserApp, CWinAppEx) ON_COMMAND(ID_HELP, &CWinApp::OnHelp) ON_MESSAGE( USER_MESSAGE_2, OnMyMessage1HandlerApp ) ... END_MESSAGE_MAP() Also I wrote message handler like this: LRESULT CwmUserApp::OnMyMessage1HandlerApp(WPARAM wParam, LPARAM lParam) { AfxMessageBox(_T("Is it working?")); return 0; } I compiiled. Then one error came up: error C2440: 'static_cast': 'LRESULT (__thiscall CwmUserApp::* )(WPARAM,LPARAM)'에서 'LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)'(으)로 변환할 수 없습니다. Sorry, it's korean. If I translate, it says Can not convert from 'LRESULT (__thiscall CwmUserApp::* )(WPARAM,LPARAM)' to 'LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)' I am not that good with programming, but as much as I understand, Does that say I simply cannot use 'User-defined-Message' in APP class? 'User-defined-Message' in dlg class or view class works fine with me though. Thanks in advance :) chan
-
Are you sure CWinApp derived classes can recieve arbitrary windows messages in MFC? I could be wrong but I thought they could only handle commands. Cheers, Ash
No. CWinApp derived class is never meant to handle windows messages. I forgot to mention him to move the ON_MESSAGE( USER_MESSAGE_2, &CMyWnd::OnMyMessage1HandlerApp ) entry to CMyWnd class's message map. Thanks.. He might have already done it, since he wouldn't find a way to SendMessage to a CWinApp derived class :-D Cheers.. :thumbsup:
-
I created a project with VS2008, dialog-based MFC. What I want to do with that project is Sending 'User-defined-Message' in APP Class. So I registered my own message in app class(xxxApp.h) like this: #define USER_MESSAGE_2 (WM_USER+ 102) And, in message map, I added ON_MESSAGE code in app class(xxxApp.cpp) like below: BEGIN_MESSAGE_MAP(CwmUserApp, CWinAppEx) ON_COMMAND(ID_HELP, &CWinApp::OnHelp) ON_MESSAGE( USER_MESSAGE_2, OnMyMessage1HandlerApp ) ... END_MESSAGE_MAP() Also I wrote message handler like this: LRESULT CwmUserApp::OnMyMessage1HandlerApp(WPARAM wParam, LPARAM lParam) { AfxMessageBox(_T("Is it working?")); return 0; } I compiiled. Then one error came up: error C2440: 'static_cast': 'LRESULT (__thiscall CwmUserApp::* )(WPARAM,LPARAM)'에서 'LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)'(으)로 변환할 수 없습니다. Sorry, it's korean. If I translate, it says Can not convert from 'LRESULT (__thiscall CwmUserApp::* )(WPARAM,LPARAM)' to 'LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)' I am not that good with programming, but as much as I understand, Does that say I simply cannot use 'User-defined-Message' in APP class? 'User-defined-Message' in dlg class or view class works fine with me though. Thanks in advance :) chan
let me write you my coding for user defined msgs maybe this will give you something:
#define WM_USER_THREAD_COMPLETED WM_USER + 2
#define WM_USER_THREAD_ENDED WM_USER + 3in stdafx.h
ON_MESSAGE( WM_USER_THREAD_COMPLETED, OnThreadCompleted )
ON_MESSAGE( WM_USER_THREAD_ENDED, OnThreadEnded )on the messagemap and my handles are like this:
LRESULT CStartStopDlg::OnThreadCompleted( WPARAM wParam, LPARAM lParam)
{// i am using wParam and lParam in the code segment
...
return 1;
}
LRESULT CStartStopDlg::OnThreadEnded( WPARAM wParam, LPARAM )
{
...
return 1;
}to call these, i use
::PostMessage( m_hWnd, WM_USER_THREAD_COMPLETED, (WPARAM)ps, (LPARAM)ns );
::PostMessage(m_hWnd,WM_USER_THREAD_ENDED,(WPARAM)ps,0);where m_hWnd is my main dialog window
-
No. CWinApp derived class is never meant to handle windows messages. I forgot to mention him to move the ON_MESSAGE( USER_MESSAGE_2, &CMyWnd::OnMyMessage1HandlerApp ) entry to CMyWnd class's message map. Thanks.. He might have already done it, since he wouldn't find a way to SendMessage to a CWinApp derived class :-D Cheers.. :thumbsup:
Dear Cool_Dev Thank you for your help I've solved this problem. Here are what I've done; First of all, It is not nessesary to move the ON_MESSAGE( USER_MESSAGE_2, &CMyWnd::OnMyMessage1HandlerApp ) entry to CMyWnd class's message map. Even though CWinApp derived class is never meant to handle windows messages, We still can hanlde windows messages in CWinApp derived class. How? This is how to get it done. It was simple. Cast the Handler in App class's message map with CWnd::* It should be like this; ON_MESSAGE( USER_MESSAGE_2, (afx_msg LRESULT(AFX_MSG_CALL CWnd::*)(WPARAM, LPARAM))OnMyMessage1HandlerApp ) Cast the Handler with (afx_msg LRESULT(AFX_MSG_CALL CWnd::*)(WPARAM, LPARAM)) Then, problem solved. But I haven't proved the safety of this casting during the application run-time. Well, I compiled and run the application. It seems fine. Nothing odd happened so far with this type casting. Thanks. Chan from SEOUL
modified on Wednesday, August 11, 2010 3:50 AM
-
Dear Cool_Dev Thank you for your help I've solved this problem. Here are what I've done; First of all, It is not nessesary to move the ON_MESSAGE( USER_MESSAGE_2, &CMyWnd::OnMyMessage1HandlerApp ) entry to CMyWnd class's message map. Even though CWinApp derived class is never meant to handle windows messages, We still can hanlde windows messages in CWinApp derived class. How? This is how to get it done. It was simple. Cast the Handler in App class's message map with CWnd::* It should be like this; ON_MESSAGE( USER_MESSAGE_2, (afx_msg LRESULT(AFX_MSG_CALL CWnd::*)(WPARAM, LPARAM))OnMyMessage1HandlerApp ) Cast the Handler with (afx_msg LRESULT(AFX_MSG_CALL CWnd::*)(WPARAM, LPARAM)) Then, problem solved. But I haven't proved the safety of this casting during the application run-time. Well, I compiled and run the application. It seems fine. Nothing odd happened so far with this type casting. Thanks. Chan from SEOUL
modified on Wednesday, August 11, 2010 3:50 AM