[RESOLVED] A little help with menu item plz!
Hi,
I'm trying to disable/enable an item menu, but half works because when I create my window the send message to disable is OK, but pressing my button does not enable and fail.
Part of my code:
Code:
...WndProc:
case WM_CREATE:
EnableMenuItem(GetMenu(hwnd), ID_TEST_DBMANAGER, MF_DISABLED); // is OK
break;
...My Dialog:
int lw,hw;
case WM_COMMAND:
lw = LOWORD(wparam);
hw = HIWORD(wparam);
switch (lw)
{
case ID_MYBUTTON:
EnableMenuItem(GetMenu(hwnd), ID_TEST_DBMANAGER, MF_ENABLED);//DON'T WORK why?
break;
}
output:
Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped, 0 warnings
it does not work? is too simple, I do not understand. :blush:
VS2010 win32 - no MFC
Thanks.
Re: A little help with menu item plz!
Just do the work in WM_INITMENUPOPUP message.
It tells you that a (sub)menu is about to be shown, then you can do here custom actions like enabling/disabling menu items.
Re: A little help with menu item plz!
Quote:
pressing my button does not enable and fail
Please elaborate. "Does not enable" and "fail", are there two problems or a single one? What "fail" really means?
Besides, your snippet explains practically nothing. Why "WndProc" and "My Dialog" use the same code? What are those window and dialog?
Re: A little help with menu item plz!
Quote:
Originally Posted by
Igor Vartanov
Please elaborate. "Does not enable" and "fail", are there two problems or a single one? What "fail" really means?
Besides, your snippet explains practically nothing. Why "WndProc" and "My Dialog" use the same code? What are those window and dialog?
Yes is the same problem, excuse my language
Now I use as said ovidiucucu WM_INITMENUPOPUP:
Code:
SendMessage (hwnd,WM_INITMENUPOPUP,EnableMenuItem(GetMenu(hwnd), ID_ITEM1, MF_DISABLED),0);// to disable item
AND
SendMessage (hwnd,WM_INITMENUPOPUP,EnableMenuItem(GetMenu(hwnd), ID_ITEM1, MF_ENABLED),0);// to enable item
This works well in WndProc but in my DlgProc command do nothing, is like not having hwnd, I remind I'm new to programming but I read a lot. thanks.
Re: A little help with menu item plz!
Well, I didn't suggest to send WM_INITMENUPOPUP but just to handle it.
Again, this message is sent (by the system) when a drop-down menu is about to become active.
It has no much sense to send it yourself.
See WM_INITMENUPOPUP message.
Re: A little help with menu item plz!
Quote:
Originally Posted by
ovidiucucu
Well, I didn't suggest to send WM_INITMENUPOPUP but just to
handle it.
Again, this message is sent (by the system) when a drop-down menu is about to become active.
It has no much sense to send it yourself.
See
WM_INITMENUPOPUP message.
Ok see my test code:
Code:
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CREATE://init my menu disable item
SendMessage (hwnd,WM_INITMENUPOPUP,EnableMenuItem(GetMenu(hwnd), ID_ITEM1, MF_DISABLED),0);//by default disabled to test
break;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case ID_EXIT:
DestroyWindow(hwnd);
break;
case ID_ITEM2:
SendMessage (hwnd,WM_INITMENUPOPUP,EnableMenuItem(GetMenu(hwnd), ID_ITEM1, MF_DISABLED),0);
break;
case ID_ITEM3:// call enable item
SendMessage (hwnd,WM_INITMENUPOPUP,EnableMenuItem(GetMenu(hwnd), ID_ITEM1, MF_ENABLED), 0);
break;
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
works perfect in WndProc I continue to not understand :cry:
Re: A little help with menu item plz!
OOOOoooK I resolved ^^
Not need use WM_INITMENUPOPUP just call GetParent(hwnd).
Code:
INT_PTR CALLBACK dlg1 (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_COMMAND:
switch (LOWORD(wParam))
{
case ID_BUTTON1://press my button to enable
EnableMenuItem(GetMenu(GetParent(hwnd)), ID_ITEM1, MF_ENABLED);
EndDialog (hwnd,TRUE);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
}
return 0;
}
Must be another way to do it, but this work for me, thanks.
Re: A little help with menu item plz!
Quote:
Originally Posted by
v_nom70
Ok see my test code:
Why are you still sending WM_INITMENUPOPUP?
Code:
case WM_CREATE://init my menu disable item
SendMessage (hwnd,WM_INITMENUPOPUP,EnableMenuItem(GetMenu(hwnd), ID_ITEM1, MF_DISABLED),0);//by default disabled to test
break;
You don't understand -- the WM_INITMENUPOPUP is sent by the system to you, and then you handle it. You don't go and create your own WM_INITMENUPOPUP message.
Once again:
http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
Please read the link above carefully, and then rewrite your code in accordance to what the information is stating. Your code now is still wrong, even though it seems to work for a window procedure.
Regards,
Paul McKenzie
Re: A little help with menu item plz!
Quote:
Originally Posted by
Paul McKenzie
Once again:
http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx
Please read the link above carefully, and then rewrite your code in accordance to what the information is stating. Your code now is still wrong, even though it seems to work for a window procedure.
Regards,
Paul McKenzie
I see but was what worked, now rewrite my code:
Code:
wndproc:
case WM_CREATE:
EnableMenuItem(GetMenu(hwnd), ID_ITEM1, MF_DISABLED);//by default disabled to test
dlgproc:
case WM_COMMAND:
switch (LOWORD(wParam))
{
case ID_BUTTON1:
EnableMenuItem(GetMenu(GetParent(hwnd)), ID_ITEM1, MF_ENABLED);
EndDialog (hwnd,TRUE);
break;
is now better :wave:
Re: A little help with menu item plz!
Quote:
Originally Posted by
v_nom70
I see but was what worked, ...
If by chance something was working in some situation, it does not obviously mean it's OK.
To avoid further headaches, you can pay more attention to...
- ...library documentation when you are using a library;
- ...answers when you are asking a question.
Additionally, can put a question as clear as possible.
Finally, can avoid "all in one line" coding style.
Make separate calls and eventually see returned values or error codes.
For example, let's take this line:
Quote:
Code:
EnableMenuItem(GetMenu(hwnd), ID_TEST_DBMANAGER, MF_ENABLED);//DON'T WORK why?
Why? Can ask Mother Teresa or (re)write the code as follows:
Code:
HMENU hMenu = GetMenu(hwnd);
if(NULL == hMenu)
{
OutputDebugStringW(L"The window has not a menu");
}
else
{
int nRet = EnableMenuItem(hMenu, ID_TEST_DBMANAGER, MF_ENABLED);
if(-1 == nRet)
{
OutputDebugStringW(L"The menu item does not exist");
}
// ...
}
Of course, it's not absolutely necessary to fill all your code with this kind of tests. But if something is going wrong or is susceptible to fail, it's good to take it into consideration. Or, at least just make separate calls to see the returned values when stepping in DEBUG mode or, a little bit better put ASSERTs on returned values.
Example:
Code:
HMENU hMenu = GetMenu(hwnd);
_ASSERT(NULL != hMenu); // This window has not a menu, dude!
Re: [RESOLVED] A little help with menu item plz!
wow ok, ok I'll remember that. :wave: regards.