3

When coding Windows Applications using WIN32 on Windows, to non-dynamically load resources, one must load a *.rc file, which contains resource definition statements meant for defining common features in apps, such as buttons and menus. While reading a tutorial for the WIN32 API, from WinProg, I came across this code that confused me:

IDR_MYMENU MENU
BEGIN
    POPUP "&File"
    BEGIN
        MENUITEM "E&xit", ID_FILE_EXIT
    END

    POPUP "&Stuff"
    BEGIN
        MENUITEM "&Go", ID_STUFF_GO
        MENUITEM "G&o somewhere else", 0, GRAYED
    END
END

Looking at the code above, it is understandable what the function is. I myself understand what the code is doing in the .rc file, but the one thing that confused me was the ampersands,(&), that are visible in the middle of the strings, such as "G&o somewhere else" (Between G and O), or "E&xit" (Between E and X). My specific questions to Stack Overflow are: What do the ampersands mean in the .rc files? Where do I have to put them when coding apps? I already looked in the MSDN link describing the function of the files, but I couldn't get an answer in the docs.

Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88
  • 1
    That is the key for the shortcut. – drescherjm Jul 12 '16 at 20:37
  • Could you explain what that means, exactly, as in what is a key for a shortcut, and why is the `&` specifically there? – Arnav Borborah Jul 12 '16 at 20:40
  • 1
    You know when you see one of the letters that are underlined in the menu item when you run the program? That is the shortcut key. The `&` is there because it is the way to tell Windows (for example) "underline the 'x' for me when you display `Exit` in the menu and use it as a shortcut". – PaulMcKenzie Jul 12 '16 at 20:43
  • The term used by Microsoft is [either "menu access key" or "mnemonic"](https://msdn.microsoft.com/en-us/library/windows/desktop/ms647553(v=vs.85).aspx#_win32_Menu_Access_Keys). – andlabs Jul 12 '16 at 20:47
  • Ah, ok, thanks guys, could one of you add that as an answer, I'll mark it correct, for other user's future reference. – Arnav Borborah Jul 12 '16 at 20:49

1 Answers1

8

Those &s are only for menus, and they define the "menu access keys", or "mnemonics", also called accelerators and shortcuts by others. And yes, the terminology is overloaded and confusing.

More reading: why you probably didn't realize these were there.

andlabs
  • 11,290
  • 1
  • 31
  • 52
  • 5
    Besides menus, the `&` also works as a `mnemonic character` in controls that have a label ([Common Control Parameters](https://msdn.microsoft.com/en-us/library/windows/desktop/aa380902.aspx)). For example a button defined with `&Save` in a dialog template will display as `S̲ave` (after pressing `ALT`) and respond to `ALT+S`. – dxiv Jul 12 '16 at 21:41