程式語言 - Wine - C/C++ - Add Accelerator



參考資訊:
http://www.winprog.org/tutorial/
http://winapi.freetechsecrets.com/win32/
https://github.com/gammasoft71/Examples_Win32
http://masm32.com/board/index.php?topic=3584.0
https://learn.microsoft.com/en-us/windows/win32/winmsg/window-styles

main.c

#include <windows.h>
#include "resource.h"
 
static HWND hWin = NULL;
static WNDPROC defWndProc = NULL;
 
static LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg) {
    case WM_CLOSE:
        DestroyWindow(hWnd);
        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    case WM_COMMAND:
        switch (LOWORD(wParam)) {
        case IDA_EXIT:
            DestroyWindow(hWnd);
            break;
        }
        return 0;
    }

    return CallWindowProc(defWndProc, hWnd, uMsg, wParam, lParam);
}
 
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    hWin = CreateWindow(WC_DIALOG, "main", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0, 0, 320, 240, NULL, NULL, NULL, NULL);
    defWndProc = (WNDPROC)SetWindowLongPtr(hWin, GWLP_WNDPROC, (long int)WndProc);
    HACCEL hAcc = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDA_MAIN));
 
    MSG msg = { 0 };
    while (GetMessage(&msg, NULL, 0, 0)) {
        if (TranslateAccelerator(hWin, hAcc, &msg) == 0) {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return 0;
}

main.rc

#include <windows.h>
#include "resource.h"

IDA_MAIN ACCELERATORS
BEGIN
    "X", IDA_EXIT, VIRTKEY, SHIFT
END

resource.h

#define IDA_MAIN 101
#define IDA_EXIT 102

編譯、執行

$ x86_64-w64-mingw32-windres main.rc -o main.res
$ winegcc main.c main.res -o main
$ wine main.exe.so


P.S. 按下Shift+X就會送出IDA_EXIT命令