03. 资源

一. 资源的命名

资源编辑器中可以将资源的ID命名为字符串,调用时使用:

wndclass.hIcon = LoadIcon(hInstance, TEXT("icon1"));

 

也可以命名为非字符串,调用时使用:

wndclass.hCursor = LoadCursor(hInstance, MAKEINTRESOURCE(IDC_CURSOR1));

 

二. 菜单

菜单可以在窗口类中设置,例如:

wndclass.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);

 

也可以在CreateWindow时设置,调用LoadMenu函数。

 

三. WM_COMMAND消息

case WM_COMMAND: {
        WORD wFrom = HIWORD(wParam);
        WORD wId = LOWORD(wParam);

        if (wFrom == 0) {//来自菜单
            switch (wId) {
            case IDM_FILE:
                MessageBox(0, TEXT("IDM_FILE"), 0, 0);
                break;
            case IDM_FILE_OPEN:
                MessageBox(0, TEXT("IDM_FILE_OPEN"), 0, 0);
                break;
            case IDM_40001:
                MessageBox(0, TEXT("IDM_40001"), 0, 0);
                break;
            default:
                break;
            }
        }
        else if (wFrom == 1) {//来自加速键
            switch (wId) {
            case IDC_CTRL_A:
                MessageBox(0, TEXT("IDA_CTRL_A"), 0, 0);
                break;
            case IDA_CTRL_SHIFT_B:
                MessageBox(0, TEXT("IDA_CTRL_SHIFT_B"), 0, 0);
                break;
            default:
                break;
            }
        }
        return 0;
}

  

HACCEL hAccel = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDR_ACCELERATOR1));

    MSG msg = {};
    while (GetMessage(&msg, nullptr, 0, 0)) {
        if (!TranslateAccelerator(hWnd,hAccel,&msg)) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
            //WndProc(msg.hwnd, msg.message, msg.wParam, msg.lParam); 
        }        
}

  

四. 手动编译链接带资源的程序

cl /c WindowsProject1.cpp

rc WindowsProject1.rc

link /subsystem:windows WindowsProject1.res WindowsProject1.obj user32.lib

posted @ 2020-06-25 12:06  八转达人  阅读(123)  评论(0编辑  收藏  举报