Frage

I have been learning Win32 GUI programming today and I have come across a question I have not found the answer to.

Why do resource.hpp files define resources with such high ID numbers? For instance, the tutorial I am following has 2 resources in resources.rc - A MENU and an ICON. The MENU has 3 MENUITEMs total.

Here is the tutorial's resource.hpp (which defines the int IDs for these resources):

    #define IDR_MYMENU               101    // MENU ID
    #define IDI_MYICON               102    // ICON ID
    #define ID_FILE_EXIT             40001  // MENUITEM 1 ID
    #define ID_STUFF_GO              40002  // MENUITEM 2 ID
    #define ID_STUFF_GOSOMEWHEREELSE 40003  // MENUITEM 3 ID

Why not instead use understandable IDs such as:

    #define IDR_MYMENU               1  // MENU ID
    #define IDI_MYICON               2  // ICON ID
    #define ID_FILE_EXIT             3  // MENUITEM 1 ID
    #define ID_STUFF_GO              4  // MENUITEM 2 ID
    #define ID_STUFF_GOSOMEWHEREELSE 5  // MENUITEM 3 ID

As more resources are added, you simply increase the count. I can understand a buffer to separate MENUITEMS from ICONS and so on; but why this much of a buffer with so few resources?

I also understand this resource.hpp file (tutorial's) was generated by a program, but the question still stands for me.

Could someone please enlighten me? Thank you.

War es hilfreich?

Lösung

It's fine that they are high because there is still no chance that you'll run out.

So, why are higher numbers better than low numbers?

You want IDs to be as unique as possible. Not just between each other, but when you see "40089" anywhere in your program, it's nice to have a good hint that it's a resource ID. This can help debugging.

I have seen a similar pattern in databases, where there is no type safety and every object has an ID. So for example if you see an ID of "20032" somewhere, you know already that it's an Employee.ID, while "22011" is most likely a Department.ID etc.

Personally I don't understand the point of separating icon resource IDs from menu items. They are all just resource IDs, and this distinction seems pretty arbitrary. Jumping from 100 to 40000 guarantees that you'll never run out of IDs for icons.

Of course this is not a complete answer. Why the specific pattern? Why the specific numbers? I doubt you'll find a real answer to this stuff; it's just how it's implemented.

Andere Tipps

Please look in the windows headers. Very many IDs are predefined and have a specific meaning, many supported by the native APIs directly. Because MS defined those IDs, they are in their private space, which starts at the low end. You really don't want to redefine them and have to support code where you cannot be sure which definition counts now.

Programs get changed all the time. If you happen to add a menu command some year you really don't want to have to renumber all of the subsequent IDs. Numbers are free - leave some room for the future.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top