Thread: User-defined message problem in MFC dialog-based app

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    11

    User-defined message problem in MFC dialog-based app

    Hello all,

    The very short MFC dialog-based app I'm testing is shown below - I'm having issues with the message map of my user-defined message.

    Code:
    #define IDM_MYMESSAGE (WM_USER + 1)
    
    #include <afxwin.h>
    #include "resource.h"
    
    class CMainDialog : public CDialog {
    public:
        //Constructor
        CMainDialog(int nIDDialog) : CDialog(nIDDialog) {}
    
        //Declare handler for user-defined message
        afx_msg LRESULT OnMyMessage(LPARAM lParam = 0, WPARAM wParam = 0);
    
        DECLARE_MESSAGE_MAP()
    };
    
    class CTestApp : public CWinApp {
    public:
        //Override base class initialization
        virtual BOOL InitInstance();
    };
    
    //Map the user-defined message to its handler
    BEGIN_MESSAGE_MAP(CMainDialog,CDialog)
        ON_MESSAGE(IDM_MYMESSAGE,OnMyMessage)
    END_MESSAGE_MAP()
    
    //Define a simple user-defined message handler
    LRESULT CMainDialog::OnMyMessage(LPARAM lParam, WPARAM wParam) {
        MessageBox("My message!");
        return 0;
    }
    
    //Initialize the app
    BOOL CTestApp::InitInstance() {
        //Create dialog from template, use as main window
        CMainDialog dialog(IDD_DIALOG1);
        m_pMainWnd = &dialog;
    
        //Show the dialog and wait for return
        UINT_PTR result = dialog.DoModal();
        return FALSE;
    }
    
    //Create application instance
    CTestApp theApp;
    And I get the following error:

    Code:
    error C2440: 'static_cast' : cannot convert from 'LRESULT (__thiscall CMainDialog::* )(LPARAM,WPARAM)' to 'LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)'
    None of the functions with this name in scope match the target type
    I also tried using "&CMainDialog::OnMyMessage" in the message map, but that didn't work, either. I'm fairly new to MFC, and I'm not quite sure what the error message is actually saying. It looks like it wants an object derived from CWnd and I'm guessing since my CMainDialog is derived from CDialog and not directly from CWnd, that maybe that has something to do with it?

    In straight Win32 since a BOOL CALLBACK is used instead of LRESULT CALLBACK for dialogs, I also thought that may be part of the issue? But I couldn't find any documentation indicating that to be the case for MFC...

    I hope this is just one of those obvious things that I'm looking too hard at. Any help is appreciated.

    Edit: I also tried getting rid of the default values in the OnMyMessage() function, but still no change.
    Last edited by jrohde; 01-17-2010 at 03:44 PM.

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Umm.....that works in my apps.

    Try using a void return to see if that helps

    or look at registering the msg and using ON_REGISTERED_MESSAGE().
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. STL Map Problem with user defined Class
    By noobcpp in forum C++ Programming
    Replies: 21
    Last Post: 07-21-2008, 12:02 PM
  2. MFC Message Map Problem
    By durban in forum Windows Programming
    Replies: 1
    Last Post: 11-02-2005, 08:18 AM
  3. stupid problem in Dialog based Apps by MFC
    By AsAdi in forum Windows Programming
    Replies: 1
    Last Post: 02-06-2003, 01:47 PM
  4. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  5. Sending CChildView a Message :: MFC
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 04-06-2002, 03:00 PM