What does & mean in a MENUITEM in a .rc file? - windows

I am a beginner in windows programming. I have seen a .rc file which looks like this
IDR_MAINFRAME MENU PRELOAD DISCARDABLE
BEGIN
POPUP "&File"
BEGIN
MENUITEM "&New", ID_FILE_NEW
MENUITEM "&Open...\tCtrl+O", ID_FILE_OPEN
MENUITEM SEPARATOR
MENUITEM "P&rint Setup...", ID_FILE_PRINT_SETUP
MENUITEM SEPARATOR
MENUITEM "Recent File", ID_FILE_MRU_FILE1, GRAYED
MENUITEM SEPARATOR
MENUITEM "E&xit", ID_APP_EXIT
END
END
Can someone explain me what does & mean in MENUITEM "&New"
and what does it mean in MENUITEM "E&xit" ?

& defines accelerator for the entry - so menu entry corresponding to "&File" can be selected by pressing "F" when menu is active or "Alt+F" for top level choices.
Same applies to dialog resources.

Related

How can I use AppleScript to find disabled menu items?

I have an AppleScript script that will find all of the menu items from all of the menu bar menus. I've filtered out each separator, so I have just the commands, and now I'd like to find out how to find out which menu items are disabled. Does anybody know how I could do that?
Update:
Here's the code I have to get the names of each menu command. It's working fine, and I tried to get the menu items that were enabled in the getAppMenuItems, but it just returned all the menu items, missing values too.
set appName to "Script Editor"
set allMenus to {}
set everything to {}
set onlyEnabled to {}
tell application "System Events" to tell process appName
set allMenus to name of every menu of menu bar 1
end tell
repeat with menuName in allMenus
set the end of everything to strings of getAppMenuItems(appName, menuName, onlyEnabled)
end repeat
on getAppMenuItems(appProcess, appMenus, enabledItems)
tell application "System Events" to tell process appProcess
(*
# Get all menu items
set theItems to every menu item of menu appMenus of menu bar 1
repeat with i from 1 to number of items in theItems
set thisItem to item i of theItems
if thisItem is not enabled then
log ("Item: " & name of thisItem & " is enabled")
set the end of enabledItems to the name of thisItem
end if
end repeat
*)
return name of every menu item of menu appMenus of menu bar 1
end tell
end getAppMenuItems
# From the commented part of the 'getAppMenuItems' function above.
# When that part of the function is uncommented, I just get everything,
# missing values and all
onlyEnabled
# Edit Menu
item 4 of everything
(* Outputs: Half of these items are not enabled
{"Undo", "Redo", "Cut", "Copy", "Paste", "Paste and Match Style", "Paste Reference", "Delete", "Select All", "Complete", "Find", "Spelling and Grammar", "Substitutions", "Transformations", "Speech", "Start Dictation…", "Emoji & Symbols"} *)
In order to get the properties of a menu item, you first need to get a reference to that menu item. You would then need to go through the various menu items and build lists or records of those that meet your criteria, so that the references can be used as needed.
From the comments, general-purpose handlers to click at or get menu references by using a list of the menu hierarchy would serve your purpose, so those would be something like:
on run -- examples
#clickMenu at {"Finder", "View", 10, "Name"} -- note that there are 2 "Clean Up" items
set menuRef to getMenuReference for {"Script Editor", "File", "New"}
log result
clickMenu at menuRef -- clickMenu at {"Script Editor", "File", "New"} can also be used
end run
to getMenuReference for menuList -- Get a reference to a menu item in an application's main menu.
(*
The menu hierarchy is described in the menuList parameter.
The last menu item doesn't need to be exact, just close enough for a match.
Menu items can also be integer indexes (separators are counted).
When using an index, note that menus can contain dynamic or hidden items.
parameters: menuList [list] -
item 1 [text]: the application name
item 2 [text]: the menu bar item name
item(s) 3+ [text or integer]: the (sub)menu item(s)
returns the reference or missing value
*)
try
set itemCount to (count menuList) -- needs to be at least {application, menu, item}
if itemCount < 3 then error "menuReference handler: the menu item list contains too few items"
set {appName, menuName} to items 1 thru 2 of menuList
set {menuItem, found} to {last item of menuList, false}
tell application appName to activate
tell application "System Events" -- set up the menu path
set appName to (name of application processes whose frontmost is true) as text -- handle different process names
set menuPath to menu menuName of menu bar item menuName of menu bar 1 of application process appName
if itemCount > 3 then repeat with i from 3 to (itemCount - 1) -- add sub menu items
set menuName to item i of menuList
if class of menuName is integer then set menuName to item menuName of (get name of menu items of menuPath)
set menuPath to menu (menuName as text) of menu item (menuName as text) of menuPath
end repeat
if class of menuItem is not integer then -- the target menu item
repeat with anItem in (get name of menu items of menuPath)
if anItem begins with (menuItem as text) then -- match a partial string (also 'contains', etc)
set {menuItem, found} to {anItem, true}
exit repeat
end if
end repeat
if not found then error "menuReference handler: menu item " & quoted form of menuItem & " not found"
end if
return menu item menuItem of menuPath
end tell
on error errorMessage number errorNumber
log errorMessage
# error errorMessage number errorNumber -- uncomment to pass error up the chain
end try
return missing value
end getMenuReference
to clickMenu at menuItem -- Click a menu item in an application's main menu.
(*
The menuItem can be a reference to the menu item or a list of the menu hierarchy.
parameters: menuItem [reference or list] - see the getMenuReference handler
returns true if successful, false otherwise
*)
try
if class of menuItem is list then -- get a reference
set menuItem to getMenuReference for menuItem
if menuItem is missing value then error "clickMenu handler: the menu item was not found"
end if
tell application "System Events"
# log (get properties of menuItem)
if (enabled of menuItem) then -- filter for desired property
click menuItem
delay 0.25
return true -- success
end if
end tell
on error errorMessage number errorNumber
log errorMessage
# error errorMessage number errorNumber -- uncomment to pass error up the chain
end try
return false -- failure
end clickMenu

Delphi form menus

So I have two menu's. When I click a button on menu 1, it creates menu 2, then menu 1 hides itself. After that I want to go back to menu 1 and hide menu 2.
How do I keep a reference to menu 1 in menu 2? Then if I want to go back to menu 2 I don't want to create a new instance, I want to use the already created one. How do I do that?
I know this may be easy but I find Delphi extremley confusing and can't seem to find a way out of it.
Thanks!
If you mean Main menu when you say "form menu" then you can have only one actual TMainMenu component on a form.
Dynamically creating a new TMainMenu is doable, but involves message handling to postpone the deletion of the exisiting TMainMenu to then be able to create a new one. The reason is that you can not delete a menu in the OnClick handler of one of the menu's items.
Let me suggest an easier way to achieve something similar by hiding / showing branches of a single TMainMenu as follows:
Drop a TMainMenu on the form and write menu items as usual. In my example I created two branches, one named MenuA and the other MenuB. Under these I added menu items, of which the first one (you are free to choose which one you use) activates the other menu branch and hides it's own branch. And visa versa for the other one.
Here's the menu part of the form in text view:
object MainMenu1: TMainMenu
Left = 112
Top = 48
object MenuA: TMenuItem
Caption = 'MenuA'
object Item11: TMenuItem
Caption = 'MenuB'
OnClick = Item11Click
end
object Item12: TMenuItem
Caption = 'Item12'
end
end
object MenuB: TMenuItem
Caption = 'MenuB'
object Item21: TMenuItem
Caption = 'MenuA'
OnClick = Item21Click
end
object Item22: TMenuItem
Caption = 'Item22'
end
object Item23: TMenuItem
Caption = 'Item23'
end
end
end
And here is the code for the menu clicks. Note that I make the second menu branch hidden in the forms OnCreate even. Instead, you can of course set the Visible property to False at design time, in the Object Inspector.
procedure TForm9.FormCreate(Sender: TObject);
begin
MenuB.Visible := False;
end;
procedure TForm9.Item11Click(Sender: TObject);
begin
MenuB.Visible := True;
MenuA.Visible := False;
end;
procedure TForm9.Item21Click(Sender: TObject);
begin
MenuB.Visible := False;
MenuA.Visible := True;
end;

Programatically click a row in a listbox

When I click the a command button to close the form, the code requery's a cbo and listbox, moves the focus to the lstbox and a particular row. What I need to do is add some code that "clicks" the current row in the listbox that has the focus.
Here is the code I have on the click event.
If Forms![frmmain]![txtHidden] = "addok" Then
Forms![frmmain]![cboAuthor].Requery
Forms![frmmain]![LstAuthor].Requery
Forms![frmmain]![LstAuthor] = Me.AuthorID
Forms![frmmain]![txtHidden] = "AddDone"
DoCmd.Close acForm, "frmAddAuthorFly"
Forms![frmmain]![cboAuthor].Requery
Forms![frmmain]![LstAuthor].Requery
Forms![frmmain].[LstAuthor].SetFocus
<NEED TO INSERT SOMETHING HERE TO CLICK THE ROW"
Else
MsgBox "txt hidden is not addok"
DoCmd.Close acForm, "frmaddauthorfly"
End If
A click does nothing by itself, but you may have some OnClick code that runs. To do so, just call the OnClick event.
But all this requiring - and indeed "clicking" - shouldn't be needed; it seems as if you need to rethink your concept.

How to append a dynamic menu item after call CMenu::LoadMenu?

In my project, there is a menu that need to be appended an item dynamically.
In original code, items in the menu are stationary. So the menu is defined in the resource file:
IDM_SERVER_OPTIONS MENU DISCARDABLE
BEGIN
POPUP ""
BEGIN
MENUITEM "&Connect", IDC_LAUNCHITEM_CONNECT
MENUITEM "&Delete", IDC_REMOVE_SERVER
END
END
and is loaded in the code:
CMenu menu;
menu.LoadMenu(IDM_SERVER_OPTIONS);
Now, there is a new requirement that need to append a dynamic menu item after load the resource menu. I referred this aricle:
Dynamic menu using mfc
Followed it, I wrote these code:
CMenu menu;
menu.LoadMenu(IDM_SERVER_OPTIONS);
CMenu *autoConnectMenu = new CMenu;
autoConnectMenu->CreatePopupMenu();
autoConnectMenu->AppendMenu(MF_STRING | MF_ENABLED,
IDC_MENU_AUTO_CONNECT_SERVER,
utils::LoadString(IDS_MENU_AUTO_CONNECT_SERVER));
menu.AppendMenu(MF_POPUP,
(UINT)autoConnectMenu->m_hMenu,
L"auto connect server");
Unfortunately, it doesn't work. The new menu item "auto connect server" can't be displayed.
Then, I tried the HMENU function:
CMenu menu;
menu.LoadMenu(IDM_SERVER_OPTIONS);
AppendMenu((HMENU)menu.GetSubMenu(0),
MF_STRING | MF_ENABLED,
IDC_AUTO_CONNECT_SERVER,
utils::LoadString(IDS_MENU_AUTO_CONNECT_SERVER));
It works fine!
I want to know what problem in my former code? Appreciate!
I think that I have found the issue. I should have called
menu.GetSubMenu(0)->AppendMenu(...);
instead of
menu.AppendMenu(...);

Lists box manipulation on button click in VB 6.0

Hello
I am doing a small I.T assignment where we have to create a database library type borrowing system and I have two list boxes listbox 1 is where all the games are listed and you can chose to borrow them the other one is where the games go to after you click the button that transfers them to listbox 2.
Aso how can I add a button that when you click it adds a new Item (game) to the first listbox and so you can borrow that one two
Thank you for your help.
Assuming two list boxes (listbox1 and listbox2) and a command button (cmd1) on a VB 6 form named Form1
private sub Form_Load()
listbox1.AddItem "game 1"
listbox1.AddItem "game 2"
listbox1.AddItem "game 3"
end sub
Private Sub cmd1_click ()
If listbox1.listindex >-1 then
'Add the selected item from listbox1 to list box 2
listbox2.AddItem listbox1.List(listbox1.ListIndex)
'Remove selected item from listbox1
listbox1.removeitem(listbox1.listindex)
End If
End Sub

Resources