我的程序在尝试使子菜单项变灰(和禁用)时遇到问题。
我正在寻找的是禁用“运行”项,除非所需的 .ini 条目不为空。
我的代码
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
HMENU hmenu = GetMenu(hWnd);
// Reading in ini
if (0 == strcmp(webLocation, "")){
EnableMenuItem(hmenu,ID_WEBSERVICES_RUN,MF_DISABLED | MF_GRAYED);
WritePrivateProfileString(_T("WEBSERVICES"), _T("Location"), _T("Tool Not Found"), WpathStr);
}
我不确定我是否正确获取了 HMENU 以及为什么此代码无法达到预期效果。
如有任何帮助,我们将不胜感激。
最佳答案
您不能只将它放在顶层的 WndProc 中。 WndProc 处理事件,不管窗口是否已经构造。由于许多不同的原因,它会被多次调用。
您的 WndProc 几乎肯定看起来像是 message
上的一个大开关。你想要的是WM_INITDIALOG
:
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case WM_INITDIALOG:
// jump to a new function that reads the .ini
// and disables the control etc.
return OnInitDialog(hWnd, wParam, lParam);
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
关于C++(没有 MFC)禁用/灰色菜单项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10227907/