Menu and Resource Configuration Reference
This section provides comprehensive reference material for working with menus, accelerators, string tables, dialog boxes, and other resources within the Windows Win32 API. Understanding resource management is crucial for creating user-friendly and localized Windows applications.
Overview of Resource Types
Windows applications store various types of data as resources, which are compiled into the application's executable file or separate resource (.res) files. These resources can be loaded and accessed at runtime, allowing for easier management and localization.
- Menus: Define the structure and content of application menus.
- Accelerators: Map keyboard shortcuts to menu commands or other actions.
- String Tables: Store application strings for localization and easy modification.
- Dialog Boxes: Define the layout and controls for custom dialog windows.
- Icons and Bitmaps: Store graphical elements used in the application's user interface.
- Cursors: Define custom mouse pointers.
Core Concepts and APIs
Working with Menus
Menus are fundamental to the Windows user interface. They allow users to select commands and options. The Win32 API provides functions to create, manipulate, and display menus.
Key Functions:
CreateMenu: Creates a new, empty menu.CreatePopupMenu: Creates a new, empty pop-up menu.AppendMenu: Appends a new item to the end of a menu.InsertMenu: Inserts a new item into a menu at a specified position.GetMenu: Retrieves the handle to the menu of a specified window.TrackPopupMenu: Displays a pop-up menu.DestroyMenu: Destroys the specified menu and frees any memory associated with it.
Accelerator Tables
Accelerator tables map virtual-key codes and modifier keys to command IDs. This enables users to execute commands quickly using keyboard shortcuts.
Key Functions:
CreateAcceleratorTable: Loads an accelerator table from a resource.TranslateAccelerator: Translates accelerator key messages and sends them to the appropriate window.DestroyAcceleratorTable: Destroys the specified accelerator table.
Accelerator table entries typically consist of:
fVirt: Specifies the type of virtual key (e.g.,FVIRTKEY) and whether to combine it with control or shift keys.key: The virtual-key code.cmd: The command ID to be sent to the window procedure.
String Tables for Localization
String tables are invaluable for internationalizing applications. By placing all user-visible strings in a string table, you can easily provide different language versions of your application by replacing the string table resource.
Key Functions:
LoadString: Loads a string from the application's resource file.FindResource: Identifies the location of a resource.LoadResource: Loads a resource into memory.
In resource definition files (.rc), strings are defined with unique IDs:
STRINGTABLE
BEGIN
IDS_APPNAME "My Wonderful Application"
IDS_WELCOME_MSG "Welcome!"
END
Dialog Box Management
Dialog boxes provide interactive interfaces for users to input data or make selections. The Win32 API offers extensive support for creating and managing dialogs.
Key Functions:
CreateDialog/CreateDialogIndirect: Creates a modal or modeless dialog box.DialogBox/DialogBoxIndirect: Displays a modal dialog box.GetDlgItem: Retrieves a handle to a control in a dialog box.SetDlgItemText: Sets the text of a dialog box control.GetDlgItemText: Retrieves the text of a dialog box control.EndDialog: Closes a modal dialog box.
.rc files using the DIALOG statement, specifying controls, their properties, and layout.
Other Resource Types
Beyond menus, accelerators, strings, and dialogs, you can manage other resources:
- Icons (
RT_ICON): Use functions likeLoadIcon. - Bitmaps (
RT_BITMAP): Use functions likeLoadBitmap. - Cursors (
RT_CURSOR): Use functions likeLoadCursor.
Resource Definition Language (.rc files)
Resource definition files are plain text files that describe the resources for an application. They are processed by the resource compiler (rc.exe) to create a binary resource file.
A typical .rc file might contain:
#include "resource.h" // For constants like IDS_APPNAME
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
// Icon
IDI_MYAPPICON ICON "res\\myapp.ico"
// Menu
IDR_MAINMENU MENU
BEGIN
POPUP "&File"
BEGIN
MENUITEM "&New", ID_FILE_NEW
MENUITEM "&Open", ID_FILE_OPEN
MENUITEM SEPARATOR
MENUITEM "&Exit", ID_FILE_EXIT
END
POPUP "&Help"
BEGIN
MENUITEM "&About", ID_HELP_ABOUT
END
END
// Dialog Box
IDD_ABOUTBOX DIALOGEX 0, 0, 235, 120
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION | WS_SYSMENU
CAPTION "About My Application"
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
DEFPUSHBUTTON "OK",IDOK,178,99,50,14
CTEXT "My Application v1.0",IDC_STATIC,0,10,235,20,SS_CENTERIMAGE
END
// String Table
STRINGTABLE
BEGIN
IDS_APPNAME "My Application"
IDS_WELCOME "Welcome to the application!"
END
// Accelerator Table
IDR_ACCELERATOR ACCELERATOR
BEGIN
"N", ID_FILE_NEW, VIRTKEY, CONTROL
"O", ID_FILE_OPEN, VIRTKEY, CONTROL
VK_F1, ID_HELP_ABOUT, NOINVERT
END
Best Practices
- Always use resource IDs (defined in a header file like
resource.h) instead of hardcoded values. - Separate strings into string tables for easy localization.
- Organize your resources logically in
.rcfiles. - Use descriptive names for your resources.
- Consider using resource editor tools for creating and managing complex resources.