[Engine Notes] Part 2: Creating Resources and Menus

1. Resource overview

  • Resources will eventually be packaged into .res files.


- Custom resources:
- Create .rc file and .h file
myresource.h
myres.rc
- Define the const id of the resource in the .h file
eg. Content in myresource.h: - In the .rc file, import .h and define Resource type and location Content in eg.myres.rc file IDX_XXX Resource type Resource name/Resource full path - or in .rc file, use name to define the resource content in eg.myres.rc file RES_NAME Resource type Resource name/Resource full path - loaded resource - defined by string name - defined by ID, use MAKEINTRESOURCE(ID)

#define IDX_XXX 1000









2. Resource Types and Resource Loading

1 .ico icon resource ( ICON )

1. Define the icon resource
icon_name/ICON_ID     ICON     icoName.ico
2. Load the icon resource

    #include "myresource.h"
    LoadICON(hInstance,icon_name);                      // 资源定义的名称是字符串
    LoadICON(hInstance,MAKEINTRESOURCE(ICON_ID));       // 资源用ID定义的
2.cur cursor resource ( CURSOR )

1. Define the cursor resource resource
cur_name/CUR_ID     CURSOR     curName.ico
2. Load the icon resource

    #include "myresource.h"
    LoadCursor(hInstance,cur_name);                      // 资源定义的名称是字符串
    LoadCursor(hInstance,MAKEINTRESOURCE(CUR_ID));       // 资源用ID定义的
3 String resources

1. Define a string resource: In the .rc file, the format is:

STRINGTABLE
{
    ID_STRING_1, "string 1"
    ID_STRING_2, "string 2"
}

The length of each string resource cannot exceed 255 characters.

2. Load the string resource

    #include "myresource.h"
    int bufferCount=80;
    char load_str[bufferCount]
    LoadString(hInstance,
        ID_STRING_1,                // 字符串的id
        load_str,                   // 加载字符串的指针
        bufferCount                 // buffer的长度
        )
4. WAV sound resource ( WAVE )

1. Define the sound resource
wave_name/WAVE_ID WAVE file_name.wav

2. Load/play sound resources

    #include "myresource.h"
    PlaySound("wave_name",hInstance,
        SND_XXX | SND_YYY);

    PlaySound(MAKEINTRESOURCE(WAVE_ID),hInstance,
        SND_XXX | SND_YYY);
    // 停止所有声音
    PlaySound(NULL,hInstance,
        SND_PURGE);

3. Menu resources ( MENU )

1. Create a menu resource

In the .rc file, create a common menu containing File and Help

MainMenu  MENU  DISCRADABLE
{
    POPUP  "&File"
    {
        MENUITEM "Open",    MENU_FILE_ID_OPEN
        // ......

        MENUITEM "E&xit",    MENU_FILE_ID_EXIT
    }
    POPUP "Help"
    {
        MENUITEM "About",    MENU_FILE_ID_EXIT
    }
}
  • POPUP: popup menu
  • MENUITEM: menu item
  • Use the & symbol to define the letter after & as the Alt shortcut key. Define the id used
    in myres.h:
#define  MENU_FILE_ID_OPEN    20000
#define  MENU_FILE_ID_XXXX    20000
// ......
2. Load menu resources
  1. Method 1: In the Windows class winClass definition:
    winClass.lpszMenuName = "MainMenu"
    winClass.lpszMenuName = MAKEINTRESOURCE(MAIN_MENU_ID)
  2. Method 2: Load the menu resource first, then assign the value
    HMENU hMenu = LoadMenu(hInstance, "MainMenu");

or:

    HMENU hMenu = LoadMenu(hInstance, MAKEINTRESOURCE(MAIN_MENU_ID));

Assignment:
Assign hMenu menu to hWnd

    SetMenu(hWnd,hMenu);
3. Responding to menu events (page 82)
  • 1. WM_COMMAND message
    • Responding to System Keys
    • Message structure: msg, lparam, wparam
      For responding to menu click events:
      • msg:WM_COMMAND
      • lparam: window handle that issued the message
      • wparam: ID of the selected menu item (LOWORD(wparam))
  • 2. Example code
    In the WinProc handler:
case  WM_COMMAND:
{
    switch(LOWORD(wparam)
    {
        case MENU_FILE_ID_OPEN:
        {
            // clicked open
        }
        break;
        case MENU_FILE_ID_EXIT:
        {
            // clicked exit
        }
        break;

        // ......

    }
}
break;


Please indicate the author and source for reprinting. The author of this article is Yan Chezuo. The link to
this article is https://yanchezuo.com/en-8.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324630958&siteId=291194637
Recommended