共用方式為


TN020: ID Naming and Numbering Conventions

This note describes the ID naming and numbering conventions used by MFC 2.0 for resources, commands, strings, controls, and child windows.

The Problem

The MFC ID naming and numbering conventions are intended to meet the following requirements:

  • Provide a consistent ID-naming standard used across the MFC library and MFC applications that are supported by the Visual C++ resource editor. This allows the programmer to more readily interpret IDs as to their type and origin.

  • Emphasize the strong 1-to-1 relationship between certain types of IDs. This helps to clarify MFC's application framework architecture.

  • Conform to already widely used standards for naming IDs in Windows.

  • Partition the ID-numbering space, so as to avoid unintentional duplication of ID numbers among those assigned by the programmer, by MFC and Windows, and by Visual C++-edited resources.

Overview of ID Prefix Naming Convention

There are several categories or types of IDs in an application. The MFC ID-naming convention defines different prefixes for different resource types.

MFC uses the prefix "IDR_" to refer to a resource ID that applies to multiple resource types. For example, for a given frame window, the same "IDR_" value is used to refer to a menu, accelerator, string and icon resource all at once.

IDR_

Multiple resource types (Used for Menus, Accelerators primarily).

IDD_

For dialog template resources (for example, IDD_DIALOG1).

IDC_

For Cursor resources.

IDI_

For Icon resources.

IDB_

For Bitmap resources.

IDS_

For String resources.

Note that the IDS_ value for a string resource is the ID passed to LoadString. The actual implementation of string table resources groups together 16 strings into one segment.

Within a DIALOG resource, we follow the convention of:

IDOK,IDCANCEL

For standard push button IDs.

IDC_

For other dialog controls.

The "IDC_" prefix is also used for cursors. This naming conflict is not usually a problem since a typical application will have few cursors and a large number of dialog controls.

Within a Menu resource, we follow the convention of:

IDM_

For menu items not using the MFC command architecture.

ID_

For menu item commands using the MFC command architecture.

Commands that follow the MFC command architecture must have an ON_COMMAND command handler and may have an ON_UPDATE_COMMAND_UI handler. If these command handlers follow the MFC command architecture, they will function correctly whether they are bound to a menu item, a toolbar button or a dialog bar button. The same ID_ is also used for a menu prompt string displayed on the program's message bar. Most of the menu items in your application should follow the MFC command convention. All of the standard command IDs (for example, ID_FILE_NEW) follow this convention.

MFC also uses "IDP_" as a specialized form of strings (that is, instead of "IDS_"). Strings with the "IDP_" prefix are "prompts," that is, strings used in message boxes. "IDP_" strings may contain "%1" and "%2" as place holders of strings determined by the program. "IDP_" strings usually have help topics, while "IDS_" strings do not. "IDP_" strings are always localized, while "IDS_" strings may or may not be localized.

The MFC library also uses the "IDW_" prefix as a specialized form of control IDs (that is, instead of "IDC_"). These IDs are assigned to child windows such as views and splitters by the framework classes. MFC implementation IDs are prefixed with "AFX_".

Overview of ID-Numbering Convention

The following lists the valid ranges for the IDs of the specific types. Some of the limits are technical implementation limits while others are just conventions to prevent your IDs from colliding with Windows predefined IDs or MFC default implementations.

We strongly recommend you do not defined IDs outside the recommended ranges. Even though the lower limit of many of these ranges is 1 (0 is not used), common convention starts practical use of IDs at 100 or 101.

Prefix Resource Type Valid Range
IDR_ multiple 1 -> 0x6FFF
IDD_ dialog templates 1 -> 0x6FFF
IDC_,IDI_,IDB_ cursors, icons, bitmaps 1 -> 0x6FFF
IDS_, IDP_ general strings 1 -> 0x7FFF
ID_ commands 0x8000 -> 0xDFFF
IDC_ controls 8 -> 0xDFFF

Reasons for these range limits:

  • By convention, the ID value of 0 is not used.

  • Windows implementation limitations restrict true resource IDs to be less than or equal to 0x7FFF.

  • MFC's internal framework implementations reserve several ranges: 0xE000->0xEFFF and 0x7000->0x7FFF.

  • Several Windows system commands use the range of 0xF000 -> 0xFFFF.

  • Control IDs of 1->7 are reserved by IDOK, IDCANCEL, and so on.

  • The range of 0x8000->0xFFFF for strings is reserved for menu prompts for commands.

Technical Notes by NumberTechnical Notes by Category