How to make a CMenu come up with an item selected

Posted on 2002-04-07
Medium Priority
Last Modified: 2013-11-20
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.
Question by:veronicas
LVL 49

Expert Comment

ID: 6924358
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

Author Comment

ID: 6924427
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.  
LVL 49

Expert Comment

ID: 6924909
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.


Expert Comment

ID: 6926672
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...
LVL 49

Expert Comment

ID: 6938505
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
LVL 49

Expert Comment

ID: 7007770
hi veronicas,
Do you have any additional questions?  Do any comments need clarification?

-- Dan
LVL 49

Expert Comment

ID: 7007781
hi veronicas,
Do you have any additional questions?  Do any comments need clarification?

-- Dan
LVL 49

Accepted Solution

DanRollins earned 200 total points
ID: 7022929
hi veronicas,
Do you have any additional questions?  Do any comments need clarification?

-- Dan

Expert Comment

ID: 7128462
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...

LVL 49

Expert Comment

ID: 7128646
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
LVL 11

Expert Comment

ID: 7203957
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.


Expert Comment

ID: 7234007
Force accepted

** Mindphaser - Community Support Moderator **

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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.

Join & Write a Comment

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Watch the video of Kernel Migrator for SharePoint, which demonstrate the process easily of migration from SharePoint to SharePoint, OneDrive for Business & Google Drive servers, Public Folder to SharePoint, File Server to SharePoint. The tool has va…

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question