16,471
社区成员
发帖
与我相关
我的任务
分享
CWnd* pMainWnd=CWnd::FromHandle(g_hWnd);
pFrameWnd=(CFrameWnd*)(pMainWnd);
pFrameWnd->m_bAutoMenuEnable=FALSE;
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
// Remove this if you use lpReserved
UNREFERENCED_PARAMETER(lpReserved);
if (dwReason == DLL_PROCESS_ATTACH)
{
TRACE0("IMenuMfc.DLL Initializing!\n");
// Extension DLL one-time initialization
if (!AfxInitExtensionModule(IMenuMfcDLL, hInstance))
return 0;
// Insert this DLL into the resource chain
// NOTE: If this Extension DLL is being implicitly linked to by
// an MFC Regular DLL (such as an ActiveX Control)
// instead of an MFC application, then you will want to
// remove this line from DllMain and put it in a separate
// function exported from this Extension DLL. The Regular DLL
// that uses this Extension DLL should then explicitly call that
// function to initialize this Extension DLL. Otherwise,
// the CDynLinkLibrary object will not be attached to the
// Regular DLL's resource chain, and serious problems will
// result.
new CDynLinkLibrary(IMenuMfcDLL);
HMENU hMenu=NULL;
HMENU hMyMenu=NULL;
HMENU hSubMenu=NULL;
DWORD dwPid=0;
CFrameWnd* pFrameWnd=NULL;
//获取主窗口句柄
dwPid=GetCurrentProcessId();
g_hWnd=GetWndFromProcID(dwPid);
//获取主窗口菜单句柄
hMenu=::GetMenu(g_hWnd);
CWnd* pMainWnd=CWnd::FromHandle(g_hWnd);
pFrameWnd=(CFrameWnd*)(pMainWnd);
pFrameWnd->m_bAutoMenuEnable=FALSE;
//插入菜单
hMenu=GetMenu(g_hWnd);
hMyMenu=LoadMenu(hInstance,MAKEINTRESOURCE(IDR_MENU_OPTION));
hSubMenu=GetSubMenu(hMyMenu,0);
InsertMenu(hMenu,2,MF_STRING|MF_BYPOSITION|MF_POPUP|MF_ENABLED,(UINT)hSubMenu,L"Option");
hSubMenu=GetSubMenu(hMyMenu,1);
EnableMenuItem(hMyMenu,(UINT)hSubMenu,MF_BYCOMMAND|MF_ENABLED);
//DrawMenuBar(g_hWnd);
g_oldProc=(WNDPROC)SetWindowLong(g_hWnd,GWL_WNDPROC,(LONG)MyWndProc);
}
else if (dwReason == DLL_PROCESS_DETACH)
{
TRACE0("IMenuMfc.DLL Terminating!\n");
// Terminate the library before destructors are called
AfxTermExtensionModule(IMenuMfcDLL);
SetWindowLong(g_hWnd,GWL_WNDPROC,(LONG)g_oldProc);
}
return 1; // ok
}
LRESULT CALLBACK MyWndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
if(uMsg==WM_SYSCOMMAND)
{
if(wParam==ID_OPTION_CONFIG)
::MessageBox(hWnd,L"Config message catched",L"Tips",MB_OK);
}
return CallWindowProc(g_oldProc,hWnd,uMsg,wParam,lParam);
}