-
July 26th, 2007, 08:10 PM
#1
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
}
-
July 27th, 2007, 02:50 AM
#2
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
-
July 27th, 2007, 08:18 AM
#3
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
-
July 27th, 2007, 08:40 AM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|