Thread: Visual C++ 6 Bible [Chapter 7]

  1. #1
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802

    Visual C++ 6 Bible [Chapter 7]

    I'm currently reading this book, and I've understood everything up to chapter 7 and the dynamic menus. What I can't understand (or what the book doesn't explain well enough) is the part about the command ranges and OnCommandRange functions..

    If anyone else has this book, they might know the section I'm talking about, if not, I'll explain it a little.

    The book says to define ON_COMMAND_RANGE(0x9000, 0xDFFF, OnCommandRange) macro between the boundaries of a BEGIN_MESSAGE_MAP() and END_MESSAGE_MAP() pair.

    Then we create the OnCommandRange function, like so:

    void CMainFrame::OnCommandRange(UINT id)
    {
    switch (id)
    {
    case ID_DYNA_COMMAND_1: ...
    case ID_DYNA_COMMAND_2: ...
    ...
    }
    }

    This is where I go lost.
    What do I put in the case statements? How do I create a dynamic menu? Where exactly am I supposed to put these BEGIN_MESSAGE_MAP() and END_MESSAGE_MAP() pairs?

    Can someone give me an example on how to add a menu item at runtime? I can make the program add another menu item with the ID of ID_EDIT_PASTE or another existing ID, but whenever I try to use my own ID, I get an undeclared identifier error..

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Feb 2002
    Posts
    25

    menus

    That is just it... If you created your menu using the menu editor use the identifiers assigned by the menu resource editor... Check the properties of the item. Place function calls to handle those operations.

    Code:
    
    //.h file
    
    // Generated message map functions
    protected:
    	//{{AFX_MSG(CMainFrame)
    	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    	afx_msg void OnEdit(UINT nId);
    	//}}AFX_MSG
    	DECLARE_MESSAGE_MAP()
    
    //////////////////////////////////////////////////////
    ///.cpp file
    BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
    	//{{AFX_MSG_MAP(CMainFrame)
    	ON_WM_CREATE()
    	ON_COMMAND_RANGE(ID_FILE_NEW, ID_APP_EXIT, OnFile)
    	//}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    
    
    void CMainFrame::OnFile(UINT nId) 
    {
    	switch( nId )
    	{
    	case ID_FILE_NEW: AfxMessageBox("Add code to handle New File"); break;
    	case ID_FILE_OPEN: AfxMessageBox("Add code to handle Open File"); break;
    	//ADD MORE CASES HERE
    	}
    }
    inZane
    --true programmer's don't comment--
    --programmer wannabes complain about it--

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    25
    sorry change this

    afx_msg void OnEdit(UINT nId);


    to this..

    OnFile(UINT nId)...
    inZane
    --true programmer's don't comment--
    --programmer wannabes complain about it--

  4. #4
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Oh bejesus.. thank you so much; it all makes sooo much sense now! If only they had extended the example to what you did

    One other problem

    I have two menu options: Add new menu item 1, and add new menu item 2. Whenever I try to add functionality to them using the ClassWizard I get the following two errors:

    Parsing Error: Expected ")".
    Input line "ON_COMMAND_RANGE(0x9000, 0xDFFF, OnCommandRange)"

    Parsing Error: Too many arguments in macro.
    Input Line: "N_COMMAND_RANGE(0x9000, 0xDFFF, OnCommandRange)"

    After I click ok on both of these Message boxes, the class wizard is set on CAboutDlg.

    Code:
    void CMainFrame::OnCommandRange(UINT id)
    {
    	switch (id) {
    	case ID_HELP_NEW: {
    		AfxMessageBox("Add Code to handle new menu 1");
    		CMenu* mmenu = GetMenu();
    		CMenu* submenu = mmenu->GetSubMenu(0);
    		submenu->AppendMenu(MF_STRING, 0x9040, "Add this &Where");
    					  }
    	case ID_HELP_NEW2: {
    		AfxMessageBox("Add Code to handle new menu 2");
    		CMenu* mmenu = GetMenu();
    		CMenu* submenu = mmenu->GetSubMenu(4);
    		submenu->AppendMenu(MF_STRING, 0x9660, "Add this &Where");
    					   }
    	}
    		
    
    }
    After clicking on both menu items with those ID's.. nothing happends! What am I doing wrong? Both of the menu items attached functions are empty by the way. Do I need to add something there?

    Also note I tried doing the following; but failed because OnCommandRange is not of that class, and I don't know the name of the particular instance
    Code:
    void CDynamicMenusApp::OnHelpNew() 
    {
    	OnCommandRange(ID_HELP_NEW);
    	// TODO: Add your command handler code here
    	
    }
    Last edited by Dual-Catfish; 03-07-2002 at 07:39 PM.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    223

    sounds like like a parentheses mismatch

    try using a meaningful name for the handler for ON_COMMAND_RANGE...

    Also make sure that you have matching pairs of parentheses and curly braces....

    post some code...
    zMan

  6. #6
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    Code:
    ...
    
    IMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd)
    
    BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
    	//{{AFX_MSG_MAP(CMainFrame)
    	ON_WM_CREATE()
    	ON_WM_SETFOCUS()
    	ON_WM_RBUTTONDOWN()
    	ON_COMMAND_RANGE(0x8000, 0x9500, OnCommandRange)
    	//}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    
    ...

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    223
    try the following code...

    0x8000, 0x9500 is too wide a range....you should only cover as much range as you need... and make sure that the range is in the right order... other wise it will still fail....
    This is also assuming that ID_HELP_NEW and ID_HELP_NEW2 are items that already exist in the menu and you are simply using them to add 2 more new items....


    Code:
    void CMainFrame::OnAddHelpItems(UINT id)
    {
    	switch (id) {
    	case ID_HELP_NEW: {
    		AfxMessageBox("Add Code to handle new menu 1");
    		CMenu* mmenu = GetMenu();
    		CMenu* submenu = mmenu->GetSubMenu(0);
    		submenu->AppendMenu(MF_STRING, 0x9040, "Add this &Where");
    					  }
    	case ID_HELP_NEW2: {
    		AfxMessageBox("Add Code to handle new menu 2");
    		CMenu* mmenu = GetMenu();
    		CMenu* submenu = mmenu->GetSubMenu(4);
    		submenu->AppendMenu(MF_STRING, 0x9660, "Add this &Where");
    					   }
    	}
    		
    
    }
    ...
    
    IMPLEMENT_DYNAMIC(CMainFrame, CFrameWnd)
    
    BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
    	//{{AFX_MSG_MAP(CMainFrame)
    	ON_WM_CREATE()
    	ON_WM_SETFOCUS()
    	ON_WM_RBUTTONDOWN()
    	ON_COMMAND_RANGE(ID_HELP_NEW, ID_HELP_NEW2, OnAddHelpItems)
    	//}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    
    ...
    zMan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  4. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 08:13 PM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM