1
votes

I'm writing a C++ application for Win32 in VS 2010 Pro. The app contains an Edit menu with a Paste item, and the app's main window contains a text box control. All of the routine Cut/Copy/Paste commands are working nicely in that control -- which happens to be the application's only user-editable field.

I want to disable the Paste menu item when there is no cursor blinking in the text box control -- that is, when the text box does not have the focus -- because when the text box is not the "active" control, no Cut/Copy/Paste action is possible.

I can enable/disable the menu item using the following code (as I've tested by placing this snippet right after the menu is created, inside WM_CREATE -- as a test). I don't know where to place the code in my program so that it's called in the course of normal user events -- like mouse clicks -- that would take the focus away from/return it to the text box.

MENUITEMINFO mii = { sizeof(MENUITEMINFO) };
mii.fMask = MIIM_STATE;
GetMenuItemInfo(hMenu, ID_EDIT_PASTE, FALSE, &mii);
mii.fState ^= MFS_DISABLED;
SetMenuItemInfo(hMenu, ID_EDIT_PASTE, FALSE, &mii);

Once the code is placed in the correct location, I plan to embed it in an if statement so that it runs (disabling the menu item) only if the app's text box does not have the focus:

if(GetFocus() != hWndTextbox)

Perhaps there is a case statement inside my CALLBACK switch(msg) section where this should all reside? I'm still learning basic C++ Win32 program structure. Thank you!

(PS two resources that have helped me get this far: An examination of menus from a beginner's point of view, How can I tell if a Window has focus? (Win32 API))

2

2 Answers

4
votes

I think you are looking for WM_INITMENUPOPUP. This is sent when a menu is about to be shown, so you can do whatever checks are necessary and enable/disable menu items appropriately.

0
votes

So you want to disable the menu when the edit control does not have focus/lost focus?

In your WndProc the Edit control sends notifications in the form of a WM_COMMAND, the two that would interest you are: EN_KILLFOCUS - when the control lost keyboard focus EN_SETFOCUS - when the control gains keyboard focus.