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. MFC: calling a menu item from another menu item

MFC: calling a menu item from another menu item

Scheduled Pinned Locked Moved C / C++ / MFC
c++
10 Posts 3 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.
  • D Offline
    D Offline
    deostroll
    wrote on last edited by
    #1

    I have two menu items under the File menu - Call My Dialog & Call MessageBox. Call MessageBox will invoke a message box. I am trying to do the same thing when the user hits Call My Dialog menu item.

    void CMainFrame::OnFileCallmessagebox()
    {
    // TODO: Add your command handler code here
    MessageBox(_T("hello world!!"), _T("Sample message box"));
    }

    void CMainFrame::OnFileCallmydialog()
    {

    /\*this->SendMessage(,
    	HIBYTE(ID\_APP\_CALL\_MESSAGEBOX),
    	LOBYTE(ID\_APP\_CALL\_MESSAGEBOX));\*/
    
    HWND hwnd = AfxGetMainWnd()->GetSafeHwnd();
    ::SendMessage(hwnd, WM\_COMMAND, 
    	HIBYTE(ID\_APP\_CALL\_MESSAGEBOX),
    	LOBYTE(ID\_APP\_CALL\_MESSAGEBOX));
    

    }

    Whatever I do I keep getting some failure assertion

    L 1 Reply Last reply
    0
    • D deostroll

      I have two menu items under the File menu - Call My Dialog & Call MessageBox. Call MessageBox will invoke a message box. I am trying to do the same thing when the user hits Call My Dialog menu item.

      void CMainFrame::OnFileCallmessagebox()
      {
      // TODO: Add your command handler code here
      MessageBox(_T("hello world!!"), _T("Sample message box"));
      }

      void CMainFrame::OnFileCallmydialog()
      {

      /\*this->SendMessage(,
      	HIBYTE(ID\_APP\_CALL\_MESSAGEBOX),
      	LOBYTE(ID\_APP\_CALL\_MESSAGEBOX));\*/
      
      HWND hwnd = AfxGetMainWnd()->GetSafeHwnd();
      ::SendMessage(hwnd, WM\_COMMAND, 
      	HIBYTE(ID\_APP\_CALL\_MESSAGEBOX),
      	LOBYTE(ID\_APP\_CALL\_MESSAGEBOX));
      

      }

      Whatever I do I keep getting some failure assertion

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      deostroll wrote:

      Whatever I do I keep getting some failure assertion

      First thing to do is look at the assertion message and figure out what is going wrong; or post it here so we can help you. Secondly your ::SendMessage[^] syntax is not correct. Thirdly, why not just tie both menu items to the OnFileCallmessagebox() function?

      txtspeak is the realm of 9 year old children, not developers. Christian Graus

      D 1 Reply Last reply
      0
      • L Lost User

        deostroll wrote:

        Whatever I do I keep getting some failure assertion

        First thing to do is look at the assertion message and figure out what is going wrong; or post it here so we can help you. Secondly your ::SendMessage[^] syntax is not correct. Thirdly, why not just tie both menu items to the OnFileCallmessagebox() function?

        txtspeak is the realm of 9 year old children, not developers. Christian Graus

        D Offline
        D Offline
        deostroll
        wrote on last edited by
        #3

        suppose I had a modal dialog box. I click a button on it, I'd want to call the same routine (i.e. the Call Message Box click event) via SendMessage(). Is this not possible? I've rewritten and debugged my code...and found out that the values I am sending for wParam and lParam are wrong

          this->SendMessage(WM\_COMMAND,
        	HIWORD(ID\_APP\_CALL\_MESSAGEBOX),
        	LOWORD(ID\_APP\_CALL\_MESSAGEBOX));
        

        what are the correct values?

        L 1 Reply Last reply
        0
        • D deostroll

          suppose I had a modal dialog box. I click a button on it, I'd want to call the same routine (i.e. the Call Message Box click event) via SendMessage(). Is this not possible? I've rewritten and debugged my code...and found out that the values I am sending for wParam and lParam are wrong

            this->SendMessage(WM\_COMMAND,
          	HIWORD(ID\_APP\_CALL\_MESSAGEBOX),
          	LOWORD(ID\_APP\_CALL\_MESSAGEBOX));
          

          what are the correct values?

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          deostroll wrote:

          suppose I had a modal dialog box. I click a button on it, I'd want to call the same routine (i.e. the Call Message Box click event) via SendMessage().

          I have no idea why you would want to do this. A modal dialog provides a break in an application where the user is asked for some information, or to make a decision, before returning to the main application. If your dialog needs to call another dialog or (terrible idea) itself, then your design is flawed.

          deostroll wrote:

          what are the correct values(for SendMessage())?

          I cannot be certain, but I suspect it is something along the lines of

          this->SendMessage(WM_COMMAND, ID_APP_CALL_MESSAGEBOX, 0L);

          I suggest you take a look at the MSDN Documentation[^] for further information.

          txtspeak is the realm of 9 year old children, not developers. Christian Graus

          D 1 Reply Last reply
          0
          • L Lost User

            deostroll wrote:

            suppose I had a modal dialog box. I click a button on it, I'd want to call the same routine (i.e. the Call Message Box click event) via SendMessage().

            I have no idea why you would want to do this. A modal dialog provides a break in an application where the user is asked for some information, or to make a decision, before returning to the main application. If your dialog needs to call another dialog or (terrible idea) itself, then your design is flawed.

            deostroll wrote:

            what are the correct values(for SendMessage())?

            I cannot be certain, but I suspect it is something along the lines of

            this->SendMessage(WM_COMMAND, ID_APP_CALL_MESSAGEBOX, 0L);

            I suggest you take a look at the MSDN Documentation[^] for further information.

            txtspeak is the realm of 9 year old children, not developers. Christian Graus

            D Offline
            D Offline
            deostroll
            wrote on last edited by
            #5

            What if the dialog was non-modal? Wouldn't this be a valid scenario? Btw how do u do a modeless dialog box?

            L 1 Reply Last reply
            0
            • D deostroll

              What if the dialog was non-modal? Wouldn't this be a valid scenario? Btw how do u do a modeless dialog box?

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              deostroll wrote:

              What if the dialog was non-modal?

              The same considerations apply; it's a question of design and usability. If I am running an app on my PC and select a menu item which pops up a dialog box, and then press a button on that dialog which pops another dialog, I tend not to feel too kindly towards the designer. As I said before you need to understand what dialogs are for and use them sparingly. Programs that just throw dialogs and other clutter at users tend to be consigned fairly quickly to the dustbin of history.

              deostroll wrote:

              Btw how do u do a modeless dialog box?

              You read the MSDN documentation[^] to start with.

              txtspeak is the realm of 9 year old children, not developers. Christian Graus

              D D 2 Replies Last reply
              0
              • L Lost User

                deostroll wrote:

                What if the dialog was non-modal?

                The same considerations apply; it's a question of design and usability. If I am running an app on my PC and select a menu item which pops up a dialog box, and then press a button on that dialog which pops another dialog, I tend not to feel too kindly towards the designer. As I said before you need to understand what dialogs are for and use them sparingly. Programs that just throw dialogs and other clutter at users tend to be consigned fairly quickly to the dustbin of history.

                deostroll wrote:

                Btw how do u do a modeless dialog box?

                You read the MSDN documentation[^] to start with.

                txtspeak is the realm of 9 year old children, not developers. Christian Graus

                D Offline
                D Offline
                deostroll
                wrote on last edited by
                #7

                Hey, I don't mean to call another dialog from another dialog. I just put a messagebox in one of my menu item's event handler. It is just to test if the code executes...which it apparently does not? k, on the topic on non-modal dialogs I don't understand the difference between the following 2 snippets:

                /* Snippet 1 */
                CMyDialog* dlg = new CMyDialog();
                dlg->Create(IDD_MY_DIALOG);
                dlg->ShowWindow(SW_SHOW);

                /* Snippet 2 */
                CMyDialog dlg = new CMyDialog();
                dlg.Create(IDD_MY_DIALOG);
                dlg.ShowWindow(SW_SHOW);

                Snippet 2 doesn't work! Why is that so?

                L 1 Reply Last reply
                0
                • D deostroll

                  Hey, I don't mean to call another dialog from another dialog. I just put a messagebox in one of my menu item's event handler. It is just to test if the code executes...which it apparently does not? k, on the topic on non-modal dialogs I don't understand the difference between the following 2 snippets:

                  /* Snippet 1 */
                  CMyDialog* dlg = new CMyDialog();
                  dlg->Create(IDD_MY_DIALOG);
                  dlg->ShowWindow(SW_SHOW);

                  /* Snippet 2 */
                  CMyDialog dlg = new CMyDialog();
                  dlg.Create(IDD_MY_DIALOG);
                  dlg.ShowWindow(SW_SHOW);

                  Snippet 2 doesn't work! Why is that so?

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  deostroll wrote:

                  Snippet 2 doesn't work! Why is that so?

                  I don't really know MFC that well, but I guess the CMyDialog() constructor returns a pointer to a new object. Sorry, senior moment there, new returns a pointer to the object created. You can check what actually happens when you run your app by using the debugger.

                  txtspeak is the realm of 9 year old children, not developers. Christian Graus

                  modified on Friday, February 12, 2010 12:11 PM

                  1 Reply Last reply
                  0
                  • L Lost User

                    deostroll wrote:

                    What if the dialog was non-modal?

                    The same considerations apply; it's a question of design and usability. If I am running an app on my PC and select a menu item which pops up a dialog box, and then press a button on that dialog which pops another dialog, I tend not to feel too kindly towards the designer. As I said before you need to understand what dialogs are for and use them sparingly. Programs that just throw dialogs and other clutter at users tend to be consigned fairly quickly to the dustbin of history.

                    deostroll wrote:

                    Btw how do u do a modeless dialog box?

                    You read the MSDN documentation[^] to start with.

                    txtspeak is the realm of 9 year old children, not developers. Christian Graus

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    Richard MacCutchan wrote:

                    If I am running an app on my PC and select a menu item which pops up a dialog box, and then press a button on that dialog which pops another dialog, I tend not to feel too kindly towards the designer.

                    Really? This sort of thing happens all over Windows (XP and Vista), Office (2003 and 2007), IE (6 and 8), Visual Studio (6 and 2005), etc.

                    "One man's wage rise is another man's price increase." - Harold Wilson

                    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                    "Man who follows car will be exhausted." - Confucius

                    L 1 Reply Last reply
                    0
                    • D David Crow

                      Richard MacCutchan wrote:

                      If I am running an app on my PC and select a menu item which pops up a dialog box, and then press a button on that dialog which pops another dialog, I tend not to feel too kindly towards the designer.

                      Really? This sort of thing happens all over Windows (XP and Vista), Office (2003 and 2007), IE (6 and 8), Visual Studio (6 and 2005), etc.

                      "One man's wage rise is another man's price increase." - Harold Wilson

                      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                      "Man who follows car will be exhausted." - Confucius

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #10

                      DavidCrow wrote:

                      This sort of thing happens all over Windows, Office, IE, Visual Studio, etc.

                      Strange, it doesn't on the versions I have!

                      txtspeak is the realm of 9 year old children, not developers. Christian Graus

                      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