winapiwin32gui

In many examples of Win32 API code found on this site and elsewhere, constants like "IDC_OUTPUT" appear. Where did these definitions come from?


When searching for examples of Win32 API code, I see many of examples of constant values with names like "IDC_OUTPUT" in resource scripts, resource.h files or defined at the top of apps, typically being used to identify dialog controls or child windows, etc. However, the value that this constant evaluates to varies quite alot. For example, I see values such as 101, 1001, 1003, 1101, 2015, even 40003.

I suppose this is just a name that represents an arbitrary value. But why "IDC_OUTPUT"? Where did the name come from? Does this name infer some special meaning? Is there a reason that this name would be preferred over some other name such as "FOO_BAR"? For example, is there any good reason why I would want to choose the name IDC_OUTPUT instead of the name I picked in the following snippet?

   #define FOO_BAR 1001
   CreateWindowEx
   (
     WS_EX_WINDOWEDGE,
     "SomeWindow",
     "SomeWindow",
     WS_CHILD|WS_VISIBLE|WS_SIZEBOX|WS_BORDER|WS_CAPTION,
     10,10,200,200,
     hWnd,
     (HMENU)FOO_BAR,  // instead of "IDC_OUTPUT"
     (HINSTANCE)GetWindowLongPtr(hWnd,0),
     0
   );

Solution

  • is there any good reason why I would want to choose the name IDC_OUTPUT?

    Yes. It follows a common convention, making it easier to understand any given piece of code. There is no technical reason to choose IDC_OUTPUT over FOO_BAR; either one works just fine (just like any 16-bit unsigned numeric value would).

    The convention here pertains to resources. Resources are generally identified by ID, a 16-bit unsigned value. That's why all those constants start with ID.

    The next character identifies the resource type. These are the common ones:

    The type is then followed by an underscore (_) to visually separate it from the remainder, which clients use to describe the item. IDC_OUTPUT is thus the ID of a control the program uses for some output.

    It is also conventional to start assigning control IDs with 100 (because the small numbers are already taken). See Why do dialog editors start assigning control IDs with 100? for more information.