CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 2004
    Posts
    89

    Checked menu item in CScrollView

    I have a CScrollView in which I want to respond to a checked menu item. I wrote the handler, but it asserts at line 861 in Afxwin1.inl. That line is ASSERT(::IsMenu(m_hMenu)), which indicates that the pointer doesn't point to a menu. The call GetMenuItemCount() asserts. Why is it not a menu? Is there some kind of restriction on handling a menu item in a CScrollView? My code is below.

    Code:
    void CFileViewer::OnOptionsHotTracking()
    {
    	CMenu* pmenuMain = GetMenu();
    
    	CMenu* pOptionsMenu = NULL;
    	int nCount = pmenuMain->GetMenuItemCount();
    
    	int iPos;
    	for( iPos = pmenuMain->GetMenuItemCount()-1; iPos >= 0; iPos--)
    	{
    		CMenu* pMenu = pmenuMain->GetSubMenu( iPos );
    		if ( pMenu  &&  pMenu->GetMenuItemID(0) == ID_OPTIONS_FONT )
    		{
    			pOptionsMenu = pMenu;
    			break;
    		}
    	}
    
    	// set flag according to menu item checked or not
    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Checked menu item in CScrollView

    Views don't have menus. Hence GetMenu() in the View returns NULL.
    By design menu should belong to application mainframe window. So you could try:
    Code:
    CMenu* pmenuMain = AfxGetMainWnd()->GetMenu();
    ASSERT(pmenuMain );
    ....
    Victor Nijegorodov

  3. #3
    Join Date
    May 2004
    Posts
    89

    Re: Checked menu item in CScrollView

    Victor,

    Thanks for you prompt reply. I suspected that might be the problem, but I could not find that in any MSDN document. I'll use your suggested solution.

    Royce

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Checked menu item in CScrollView

    Well, perhaps, you should read MSDN a little oftener!
    From MSDN:
    CWnd::GetMenu
    CMenu* GetMenu( ) const;

    Return Value

    Identifies the menu. The value is NULL if CWnd has no menu. The return value is undefined if CWnd is a child window.

    The returned pointer may be temporary and should not be stored for later use.

    Remarks

    Retrieves a pointer to the menu for this window. This function should not be used for child windows because they do not have a menu.
    PS. the recommendation "read MSDN a little oftener" should be applied to me as well! Since MSDN states "the return value is undefined if CWnd is a child window" the ASSERT(pmenuMain) may not work! So, you could add the Ismenu check:
    Code:
    ASSERT(::IsMenu(pmenuMain->GetSafeHmenu ()));
    Victor Nijegorodov

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured