Solved

How to make a CMenu come up with an item selected

Posted on 2002-04-07
12
295 Views
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.
0
Comment
Question by:veronicas
12 Comments
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
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
0
 

Author Comment

by:veronicas
Comment Utility
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.  
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
To minimize keystrokes, you can assign an accellerator key,  For instance, rather than
    Alt+F, down, down, down, Enter (=5 keystrokes)
or
    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
0
 
LVL 3

Expert Comment

by:Crius
Comment Utility
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...
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
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:

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

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

=-=-=-=-=-=-=-=-=-
veronicas,
Are you still here?  Did you understand what I said about the keyboard accellerator?

-- Dan
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
hi veronicas,
Do you have any additional questions?  Do any comments need clarification?

-- Dan
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
hi veronicas,
Do you have any additional questions?  Do any comments need clarification?

-- Dan
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 50 total points
Comment Utility
hi veronicas,
Do you have any additional questions?  Do any comments need clarification?

-- Dan
0
 
LVL 2

Expert Comment

by:Ber
Comment Utility
I think you can highlight the menu using the HiliteMenuItem API...

From MSDN:

HiliteMenuItem
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
);

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

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

uItemHilite
[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.

uHilite
[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.

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

Requirements
  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...

Cheers...
Ber...
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
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
0
 
LVL 11

Expert Comment

by:griessh
Comment Utility
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

     "DanROllins"

comment(s) as an answer.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 6

Expert Comment

by:Mindphaser
Comment Utility
Force accepted

** Mindphaser - Community Support Moderator **
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now