Search code examples
c++visual-studiowinapiwin32guiresource-files

Where to code Win32 Menu


I am new to the Win32 API and I am trying to learn on my free time. I'm progressing well, but today I came across menus. I realized that there are 2 different ways (that I currently am aware of) I could implement a menu in Win32. I could use AppendMenu in WM_CREATE inside my WndProc, or I could create a menu in my resource.rc, such as:

    IDR_MYMENU MENU
    BEGIN
        POPUP "File"
        BEGIN
            MENUITEM "Exit", ID_FILE_EXIT
        END
        POPUP "Help"
        BEGIN
            MENUITEM "About", ID_HELP_ABOUT
        END
    END

My question is this: is either method of implementation "better" than the other. And by "better" I mean is code more modular or scalable in either form than the other? Are there benefits/drawbacks to using either?

Thanks in advance for any contributions!


Solution

  • Resource files tend to be much cleaner statements of what is being done when compared with code, menus for instance it is far easier to see layout with the BEGIN ... END blocks compared with AppendMenu/InsertMenu calls.

    Using resources also gives you the option of much easier translation compared with doing the same tasks in code. In that scenario you create resources with the strings translated into the target language while the numeric identifiers are the same across all versions. This works for all resource types (dialogs, string and message tables, even icons), not just menus.