win32 api menu turn off Mnemonic
- User
win32 api menu turn off Mnemonic
- Assistant
win32 api menu turn off Mnemonic
To disable mnemonics (i.e., the underlined shortcut keys) in Win32 API menus, you can adjust the UI state of your application to hide these accelerators. This prevents the mnemonic indicators from being displayed in menu items and disables their associated keyboard shortcuts.
Here's how you can achieve this:
WM_UPDATEUISTATE MessageSend a WM_UPDATEUISTATE message to your window with the appropriate parameters to hide the accelerators (mnemonics).
Example:
#include <windows.h>
// Function to hide mnemonics in the menu
void HideMenuMnemonics(HWND hwnd)
{
// Set the UI state to hide accelerators
SendMessage(hwnd, WM_UPDATEUISTATE,
MAKEWPARAM(UIS_SET, UISF_HIDEACCEL),
0);
}
// Function to show mnemonics again (if needed)
void ShowMenuMnemonics(HWND hwnd)
{
// Clear the UI state to show accelerators
SendMessage(hwnd, WM_UPDATEUISTATE,
MAKEWPARAM(UIS_CLEAR, UISF_HIDEACCEL),
0);
}
// Example usage within a window procedure
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CREATE:
// Hide mnemonics when the window is created
HideMenuMnemonics(hwnd);
return 0;
// Handle other messages...
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
Explanation:
WM_UPDATEUISTATE: This message updates the user interface state for a window. By sending this message with specific parameters, you can control the visibility of UI elements like mnemonics.
Parameters:
MAKEWPARAM(UIS_SET, UISF_HIDEACCEL): This combination sets the UI state to hide accelerators (mnemonics).MAKEWPARAM(UIS_CLEAR, UISF_HIDEACCEL): This clears the setting, making accelerators visible again.Mnemonics in menu items are typically defined by preceding a character with an ampersand (&). For example, "&File" defines F as the mnemonic for the "File" menu.
If you prefer to completely remove mnemonics, you can strip out ampersands from all menu item strings. This can be done at design time (within your resource scripts) or programmatically at runtime.
Example (Programmatically Removing Ampersands):
#include <windows.h>
#include <string.h>
// Function to remove ampersands from a string
void RemoveAmpersands(char* dest, const char* src)
{
while (*src)
{
if (*src != '&')
{
*dest++ = *src;
}
src++;
}
*dest = '\0';
}
// Function to update all menu items by removing ampersands
void RemoveMenuMnemonics(HMENU hMenu)
{
int count = GetMenuItemCount(hMenu);
for (int i = 0; i < count; i++)
{
char buffer[256];
MENUITEMINFO mii = { sizeof(MENUITEMINFO) };
mii.fMask = MIIM_TYPE;
mii.dwTypeData = buffer;
mii.cch = sizeof(buffer);
if (GetMenuItemInfoA(hMenu, i, TRUE, &mii))
{
char newText[256];
RemoveAmpersands(newText, buffer);
mii.dwTypeData = newText;
SetMenuItemInfoA(hMenu, i, TRUE, &mii);
}
// If the menu item has a submenu, recurse
HMENU hSubMenu = GetSubMenu(hMenu, i);
if (hSubMenu)
{
RemoveMenuMnemonics(hSubMenu);
}
}
}
// Example usage within WM_INITMENU or similar
void OnInitMenu(HWND hwnd)
{
HMENU hMenu = GetMenu(hwnd);
if (hMenu)
{
RemoveMenuMnemonics(hMenu);
DrawMenuBar(hwnd); // Refresh the menu bar to reflect changes
}
}
Explanation:
RemoveAmpersands: This helper function copies the source string to the destination while omitting any ampersand characters.
RemoveMenuMnemonics: Iterates through each menu item in the provided menu handle, retrieves the item text, removes ampersands, and sets the updated text back to the menu item. It also handles submenus recursively.
Note: Removing ampersands programmatically affects only runtime behavior. If you have access to the resource scripts (.rc files), it's better to remove ampersands there to simplify the process.
Alternatively, mnemonics visibility can be influenced by the user's system settings:
Show Underlined Mnemonics Always:
Control Panel → Ease of Access Center → Make the keyboard easier to use → Uncheck Underline keyboard shortcuts and access keys.Hide Underlined Mnemonics Unless Alt is Pressed:
Control Panel → Ease of Access Center → Make the keyboard easier to use → Check Underline keyboard shortcuts and access keys.However, it's generally not recommended for applications to override user preferences. If you choose to manipulate mnemonics visibility programmatically, ensure it aligns with your application's usability goals and consider the user's accessibility needs.
To turn off mnemonics in Win32 API menus, you can either:
WM_UPDATEUISTATE message with the UIS_SET and UISF_HIDEACCEL flags.Choose the approach that best fits your application's architecture and requirements.
Not yet included in a published release.