How to make a CMenu come up with an item selected

I need to make a menu appear that already has an item selected & highlighted.  I know there's an easy way to do this, but I can't remember how.

Seemes there's a sendmessage involved.
Who is Participating?
DanRollinsConnect With a Mentor Commented:
hi veronicas,
Do you have any additional questions?  Do any comments need clarification?

-- Dan
The highlight will always appear on the top menu item until there is some user action such as moving the mouse over a different item or the user presseing some arrow keys.

There is a concept of the "Default Menu Item" but it refers to whether a particular item is drawn in bold text (not displayed in reverse video, which is what I believe you are asking about when you say 'highlighted' ).  When a menu item is set as the default, then the user can press ENTER to select it and invoke its command -- if no menu item is highlighted).

To set the default menu item, use the CMenu::SetDefaultItem() fn.   As a sort of alternative, you could place this particular item at the top of the menu.

It might be possible to do some oddball tricks such as posting keystrokes (say, several 'down' arrow keys), but I don't recommend it.  Users understand the Windows menuing system very well, and they don't expect things like that:  That is, if I press Alt+F, I expect the File menu to drop down and the top item on the menu to be highlighted.  If something else happens, I will be confused and I will call your tech support department and tell them that your program has a bug.

-- Dan
veronicasAuthor Commented:
This app is not a traditional mouse-driven application.  It is for some data entry people, who need to keep their fingers on the keyboard for 99% of the day.  Extra keystrokes = lost productivity, so I'd like to place a highlight bar on the top item for them.  Just like when you press Alt F, you get a bar already on the top item of the file menu.  
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

To minimize keystrokes, you can assign an accellerator key,  For instance, rather than
    Alt+F, down, down, down, Enter (=5 keystrokes)
    Alt+F, G  (=2 keystrokes)
you can set the app so that
    Ctrl+G (=1 keystroke)
does the same thing.   Do you need help in learning how to set up and use accellerator keys?

-- Dan
You can do it with GetMenuItemInfo() and SetMenuItemInfo().

Using the fMask of MIIM_STATE, you can toggle the MFS_HILITE and MFS_UNHILITE much like MFS_CHECKED and MFS_UNCHECKED are toggled using CheckMenuItem(). There are functions to do most menu stuff like checking, unchecking, enabling, disabling, etc, but for highlight you do have to dig a bit deeper.

Hopefully this helps you out...
That seems like it should work, but these sete how the item is displayed.  It seems that only a user action with the mouse or keyboard can choose which menu command to execute.

For instance, this *looks* right (the item is in reverse video), but it is not the selected item, so pressing Enter does not execute the command:

     rMII.cbSize = sizeof (MENUITEMINFO);
     rMII.fMask = MIIM_STATE;
     submenu->GetMenuItemInfo(IDCM_NewFolder, &rMII );
     ::SetMenuItemInfo( submenu->m_hMenu, IDCM_NewFolder, FALSE, &rMII );

     submenu->TrackPopupMenu( ... etc...

Are you still here?  Did you understand what I said about the keyboard accellerator?

-- Dan
hi veronicas,
Do you have any additional questions?  Do any comments need clarification?

-- Dan
hi veronicas,
Do you have any additional questions?  Do any comments need clarification?

-- Dan
I think you can highlight the menu using the HiliteMenuItem API...

From MSDN:

The HiliteMenuItem function highlights or removes the highlighting from an item in a menu bar.

BOOL HiliteMenuItem(
  HWND hwnd,         // handle to window
  HMENU hmenu,       // handle to menu
  UINT uItemHilite,  // menu item
  UINT uHilite       // highlight options

[in] Handle to the window that contains the menu.

[in] Handle to the menu bar that contains the item to be

[in] Specifies the menu item to be highlighted. This parameter is either the identifier of the menu item or the offset of the menu item in the menu bar, depending on the value of the uHilite parameter.

[in] Controls the interpretation of the uItemHilite parameter and indicates whether the menu item is highlighted. This parameter must be a combination of either MF_BYCOMMAND or MF_BYPOSITION and MF_HILITE or MF_UNHILITE.

Value Meaning
MF_BYCOMMAND Indicates that uItemHilite gives the identifier of the menu item.

MF_BYPOSITION Indicates that uItemHilite gives the zero-based relative position of the menu item.

MF_HILITE Highlights the menu item. If this flag is not specified, the highlighting is removed from the item.

MF_UNHILITE Removes highlighting from the menu item.

Return Values
If the menu item is set to the specified highlight state, the return value is nonzero.

If the menu item is not set to the specified highlight state, the return value is zero.

The MF_HILITE and MF_UNHILITE flags can be used only with the HiliteMenuItem function; they cannot be used with the ModifyMenu function.

  Windows NT/2000 or later: Requires Windows NT 3.1 or later.
  Windows 95/98/Me: Requires Windows 95 or later.
  Header: Declared in Winuser.h; include Windows.h.
  Library: Use User32.lib.

You could also try the SetMenuItemInfo API...

AFAIK either of these should work...

Hi Ber,
>>AFAIK either of these should work...
There is no need at all to guess.  It is quite easy to check.  As I stated in a previous comment, that highlights the item, but does not select it.  It *looks* selected but is not.  Go ahead.  Try it.

-- Dan
Dear veronicas

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to accept


comment(s) as an answer.

Force accepted

** Mindphaser - Community Support Moderator **
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.