Solved

How to make a CMenu come up with an item selected

Posted on 2002-04-07
12
296 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
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
0
 

Author Comment

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

Expert Comment

by:DanRollins
ID: 6924909
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
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...
0
 
LVL 49

Expert Comment

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

     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
ID: 7007770
hi veronicas,
Do you have any additional questions?  Do any comments need clarification?

-- Dan
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 49

Expert Comment

by:DanRollins
ID: 7007781
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
ID: 7022929
hi veronicas,
Do you have any additional questions?  Do any comments need clarification?

-- Dan
0
 
LVL 2

Expert Comment

by:Ber
ID: 7128462
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
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
0
 
LVL 11

Expert Comment

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

     "DanROllins"

comment(s) as an answer.

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

Expert Comment

by:Mindphaser
ID: 7234007
Force accepted

** Mindphaser - Community Support Moderator **
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

910 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

22 Experts available now in Live!

Get 1:1 Help Now