为什么GetMenu()不能工作? [英] Why won't GetMenu() work?

查看:70
本文介绍了为什么GetMenu()不能工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的框架窗口类(主窗口类)与此问题相关的代码段是:



The code segment of my frame window class(Main window class) that is relevant to this question is this:

protected:  // control bar embedded members
	CMFCMenuBar       m_wndMenuBar;
	CMyToolBar        m_wndToolBar;
	CMFCStatusBar     m_wndStatusBar;
	CMFCToolBarImages m_UserImages;





问题是esach时间尝试使用GetMenu()函数返回的CMenu指针我的消息处理程序抛出异常。如下所示:





The problem is that esach time attempt to use the CMenu pointer returned by GetMenu() function within of my message handlers an exception is thrown. This illustrated below:

void CMainFrame::Handler()
{
   CMenu *pMainMenu = GetMenu();

   CMenu * pSubMenu = pMainMenu->GetSubmenu(0);

   pSubMenu->InsertMenu(0,MF_BYPOSITION | MF_STRING,IDM_RESET,_T("&Reset"));//This line won't execute
}





抛出的异常报告pSubMenu的m_hMenu成员不是有效。可能有什么问题?





编辑:



对于一些莫名其妙的原因,我可以让GetMenu()函数工作。在上面的程序中,尝试获取指向Windows应用程序的FILE子菜单的指针,但没有成功。



请问可能出错?或者更好的是,有人可以给我一个简单的mfc代码来获取指向Windows应用程序的FILE菜单(我相信是一个子菜单)子菜单的指针吗?



EDIT1:

抛出一个例子文件



afxwin1.inl



The exception thrown reports that the m_hMenu member of pSubMenu is not valid. What could be wrong?




For some inexplicable reasons, I can get GetMenu() function to work. In the program above an attempt is made to get a pointer to the FILE submenu of a windows app, but without success.

Please what can be wrong? Or better still, can someone show me a simple mfc code to get the pointer to the FILE menu( which I beleive is a submenu) submenu of a windows app?


On throwing an exeption the file

afxwin1.inl

FXWIN_INLINE CMenu* CMenu::GetSubMenu(int nPos) const
	{ ASSERT(::IsMenu(m_hMenu)); return CMenu::FromHandle(::GetSubMenu(m_hMenu, nPos)); }/*A pointer points to this line as the location of the exception.*/





一个对话框显示消息:

App.exe中0x50b9a12c(mfc1ooud.dll)的未处理的exeption:

)xC0000005:访问冲突读数0x00000004





真的可能出错?或者可能是因为我的应用程序使用了三个不同的文档模板,因此每个模板和文件类型都有三个不同的菜单资源eac。



EDIT2:



我在上面的代码段中将GetMenu之后立即放入以下代码行。



A dialogue box displayed the message:
Unhandled exeption at 0x50b9a12c (mfc1ooud.dll) in App.exe:
)xC0000005: Access violation reading 0x00000004


What could really be wrong? Or could it be because my app used three different document template and as such have three different menu resource eac for each template and file type.



I put the following code line immediately after GetMenu in the code segment above.

ASSERT(m_pMainMenu);





包含显示GetMenu()没有返回有效结果。

什么可能使GetMenu()失败。封闭函数是一个类的成员,如下所示。



That inclusion revealed that GetMenu() did not return a valid result.
What could make GetMenu() fail. The enclosing function is a member of a class difined as shown below.

class CMainFrame : public CMDIFrameWndEx
{
}



是否GetMenu()函数无法返回指针到附加到CMDIFrameWndEx对象的菜单?如果是,如何获得指向附加到CMDIFrameWndEx对象的菜单的指针?





NEW EDIT3

这是我现在使用的代码。消息框报告插入成功,但我看不到新插入的菜单。可能有什么问题?




Is it that GetMenu() function can not return a pointer to the menu attached to a CMDIFrameWndEx object? If yes, how does one get a pointer to the menu attached to the CMDIFrameWndEx object?


NEW EDIT3
This is the code I am usng now. The message box reports insert success, but I couldn't see the newly inserted menu. What could be wrong?

const CMFCMenuBar *pMenuBar = GetMenuBar();

	HMENU hMainMenu = NULL;
	HMENU hSubMenu = NULL;

	hMainMenu = pMenuBar->GetHMenu();
	ASSERT(hMainMenu);
	hSubMenu = GetSubMenu(hMainMenu,0);
	ASSERT(hSubMenu);
	
	if(InsertMenu(hSubMenu,0,MF_BYPOSITION | MF_STRING,ID_TEST_FIRST,_T("&This is a test")))
	{
		MessageBox(_T("Insert Success"));
	}
	DrawMenuBar();

推荐答案

可能没有子菜单( pSubMenu 为NULL)。 GetSubMenu()用于获取弹出(上下文)菜单。但看起来您想要访问应用程序的主菜单。





以上可能不清楚。 GetSubmenu()只有在传递的位置引用子菜单时才会返回有效指针。它将返回 NULL 否则(例如菜单项或分隔符)。



使用 GetMenuItemInfo [ ^ ]检查项目类型。





更新的问题表明您使用的是 CMDIFrameWndEx 继承的类。它以不同的方式处理菜单并提供 GetMenuBar [ ^ ]函数返回指向 CMFCMenuBar的指针。将该指针与 GetHMenu 一起使用以获取菜单句柄。
There is probably no sub menu (pSubMenu is NULL). GetSubMenu() is used to get a pop-up (context) menu. But it looks like you want to access the main menu of your application.


The above may be unclear. GetSubmenu() will only return a valid pointer when the passed position refers to a sub menu. It will return NULL otherwise (e.g. menu item or a separator).

Use GetMenuItemInfo[^] to check for the item type.


The updated question indicates that you are using a CMDIFrameWndEx inherited class. That handles menus in a different way and provides the GetMenuBar[^] function return a pointer to CMFCMenuBar. Use that pointer with GetHMenu to get the menu handle.


根据文档 [ ^ ]:

As per the documentation[^]:


A指向CMenu对象的指针,如果给定位置存在弹出菜单,则m_hMenu成员包含弹出菜单的句柄;否则为NULL。


A pointer to a CMenu object whose m_hMenu member contains a handle to the pop-up menu if a pop-up menu exists at the given position; otherwise NULL.



在您的情况下 0 位置没有弹出菜单。


请参阅 https://msdn.microsoft.com/en-us/ library / 7xkd69hf.aspx [ ^ ]。尝试使用此代码,然后使用调试器以确保其正常工作。
See https://msdn.microsoft.com/en-us/library/7xkd69hf.aspx[^]. Try this code, and step through with your debugger to ensure it works correctly.


这篇关于为什么GetMenu()不能工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆