[英]How do I add a menubar to my WTL dialog window?
I am trying to add a menubar I created in my resource to a dialog from my resource, but I can't quite figure out how to. 我试图将我在资源中创建的菜单栏添加到资源中的对话框中,但是我还不太清楚该怎么做。
I searched a lot of guides on that, but they all seem to be working only with 我搜索了很多关于此的指南,但它们似乎都只能与
_Module
which seems to be a very old relic according to Does ATL/WTL still require the use of a global _Module variable? 根据ATL / WTL仍然需要使用全局_Module变量吗?
Most of these guides want to tackle the problem using this method: 这些指南中的大多数都希望使用此方法解决问题:
CMenu menu;
menu.Attach( LoadMenu( _Module.GetResourceInstance(),MAKEINTRESOURCE(<Menubar ID>)));
SetMenu( menu );
However, I would like to know what the "modern" way would be then, considering the _Module
-way is outdated. 但是,考虑到
_Module
-way已过时,我想知道什么是“现代”方式。
Can anyone point me to a solution? 谁能指出我的解决方案?
First argument of WinAPI function LoadMenu is HINSTANCE of the module from which the menu should be loaded. WinAPI函数LoadMenu的第一个参数是应从中加载菜单的模块的HINSTANCE。 If your app resources are in the executable (as opposed to a separate resource DLL), you can get its instance by calling
GetModuleHandle(NULL)
: 如果您的应用程序资源位于可执行文件中(而不是单独的资源DLL),则可以通过调用
GetModuleHandle(NULL)
获取其实例:
menu.Attach(LoadMenu(GetModuleHandle(NULL),MAKEINTRESOURCE(<Menubar ID>)));
In other cases, you will need to pass module name to the function. 在其他情况下,您需要将模块名称传递给函数。
By the way, an easier way to load a menu is: 顺便说一句,加载菜单的更简单方法是:
CMenu menu;
menu.LoadMenu(MAKEINTRESOURCE(<ID>));
Here is how it is implemented in atluser.h: 这是在atluser.h中实现的方式:
BOOL LoadMenu(ATL::_U_STRINGorID menu)
{
ATLASSERT(m_hMenu == NULL);
m_hMenu = ::LoadMenu(ModuleHelper::GetResourceInstance(), menu.m_lpstr);
return (m_hMenu != NULL) ? TRUE : FALSE;
}
So you can use the ModuleHelper
thing instead of _Module
. 因此,您可以使用
ModuleHelper
而不是_Module
。 It comes from atlapp.h: 它来自atlapp.h:
inline HINSTANCE GetResourceInstance()
{
#if (_ATL_VER >= 0x0700)
return ATL::_AtlBaseModule.GetResourceInstance();
#else // !(_ATL_VER >= 0x0700)
return ATL::_pModule->GetResourceInstance();
#endif // !(_ATL_VER >= 0x0700)
}
ATL::_AtlBaseModule.GetResourceInstance
function returns handle of the module in which ATL was compiled (if I remember correctly). ATL::_AtlBaseModule.GetResourceInstance
函数返回在其中编译ATL的模块的句柄(如果我没记错的话)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.