Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Can't use 'User-defined-Message' in APP class of dialog-based MFC application. Why?

Can't use 'User-defined-Message' in APP class of dialog-based MFC application. Why?

Scheduled Pinned Locked Moved C / C++ / MFC
c++helpquestion
9 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JongchanAhn
    wrote on last edited by
    #1

    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

    C A D 3 Replies Last reply
    0
    • J JongchanAhn

      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

      C Offline
      C Offline
      Cool_Dev
      wrote on last edited by
      #2

      ON_MESSAGE

      check the definition of ON_MESSAGE macro in afxmsg_.h
      See: The type of the function must be afx_msg LRESULT (CWnd::*)(WPARAM, LPARAM).

      J 1 Reply Last reply
      0
      • C Cool_Dev

        ON_MESSAGE

        check the definition of ON_MESSAGE macro in afxmsg_.h
        See: The type of the function must be afx_msg LRESULT (CWnd::*)(WPARAM, LPARAM).

        J Offline
        J Offline
        JongchanAhn
        wrote on last edited by
        #3

        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

        C 1 Reply Last reply
        0
        • J JongchanAhn

          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

          C Offline
          C Offline
          Cool_Dev
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • J JongchanAhn

            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

            A Offline
            A Offline
            Aescleal
            wrote on last edited by
            #5

            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

            C 1 Reply Last reply
            0
            • A Aescleal

              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

              C Offline
              C Offline
              Cool_Dev
              wrote on last edited by
              #6

              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:

              J 1 Reply Last reply
              0
              • J JongchanAhn

                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

                D Offline
                D Offline
                dilara semerci
                wrote on last edited by
                #7

                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 + 3

                in 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

                1 Reply Last reply
                0
                • C Cool_Dev

                  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:

                  J Offline
                  J Offline
                  JongchanAhn
                  wrote on last edited by
                  #8

                  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

                  C 1 Reply Last reply
                  0
                  • J JongchanAhn

                    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

                    C Offline
                    C Offline
                    Cool_Dev
                    wrote on last edited by
                    #9

                    Yes.. you are right. The above scenario works well with theApp.PostThreadMessage(USER_MESSAGE_2, 0, 0); :thumbsup: But when you want to send USER_MESSAGE_2 message to a window, you should move the entry to curresponding window's message map.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups