Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

609 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