Article Hook WndProc (ImGui)

rushensky

Newbie
Newbie

rushensky

Newbie
Newbie
Status
Offline
Joined
Jul 11, 2019
Messages
5
Reaction score
6
Code:
LRESULT __stdcall CALLBACK hkWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    if (uMsg == WM_KEYDOWN)
    {
        if (wParam == VK_INSERT)
            m_pDrawMenu->IsVisible = !m_pDrawMenu->IsVisible;
    }

    if (ImGui_ImplWin32_WndProcHandler(hWnd, uMsg, wParam, lParam) && m_pDrawMenu->IsVisible)
        return true;

    return CallWindowProc(g_WndProc_o, hWnd, uMsg, wParam, lParam);
}

// your entry point

    if (!GetHWND(&g_hWnd))
        ErrorMessage("Couldn't get hWnd.");

    g_WndProc_o = (WNDPROC)SetWindowLong(g_hWnd, GWL_WNDPROC, (LRESULT)hkWndProc);


// And Simple WndProcHandler
RESULT ImGui_ImplWin32_WndProcHandler(HWND, UINT msg, WPARAM wParam, LPARAM lParam)
{
    ImGuiIO& io = ImGui::GetIO();

    switch (msg)
    {
    case WM_LBUTTONDOWN:
        io.MouseDown[0] = true;
        return true;

    case WM_LBUTTONUP:
        io.MouseDown[0] = false;
        return true;

    case WM_RBUTTONDOWN:
        io.MouseDown[1] = true;
        return true;

    case WM_RBUTTONUP:
        io.MouseDown[1] = false;
        return true;

    case WM_MBUTTONDOWN:
        io.MouseDown[2] = true;
        return true;

    case WM_MBUTTONUP:
        io.MouseDown[2] = false;
        return true;

    case WM_MOUSEWHEEL:
        io.MouseWheel += GET_WHEEL_DELTA_WPARAM(wParam) > 0 ? +1.f : -1.f;
        return true;

    case WM_MOUSEHWHEEL:
        io.MouseWheelH += GET_WHEEL_DELTA_WPARAM(wParam) > 0 ? +1.f : -1.f;
        return true;

    case WM_MOUSEMOVE:
        io.MousePos.x = LOWORD(lParam);
        io.MousePos.y = HIWORD(lParam);
        return true;

    case WM_KEYDOWN:
    case WM_SYSKEYDOWN:
        if (wParam < 256)
            io.KeysDown[wParam] = true;
        return true;

    case WM_KEYUP:
    case WM_SYSKEYUP:
        if (wParam < 256)
            io.KeysDown[wParam] = false;
        return true;

    case WM_CHAR:
        wchar_t wch;
        MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, (char*)& wParam, 1, &wch, 1);
        io.AddInputCharacter(wch);
        return true;
    }

    return false;
}
 
Top