1

This is a follow-up to my previous question.

How to handle button held down in WinAPI?

I managed to get the button to update counter continuously while the button is depressed, based on the comments I received, but now I have a new problem.

If I click the button, it increments counter and updates the edit control. If I release then immediately click the button again, it does not increment & update.

It seems that there is a delay associated with the WM_LBUTTONDOWN message independent from the delay I am enforcing under WM_TIMER processing. How to resolve this issue?

#include <Windows.h>

HINSTANCE g_hInst;

HWND hEdit;
HWND hButton;
WNDPROC btnProc;
int counter = 0;
int t = 0;

static LRESULT CALLBACK StaticButtonProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg) {
    case WM_LBUTTONDOWN:
        // increment counter & update Edit Control
        counter++;
        wchar_t buf[40];
        wsprintf(buf, L"%d", counter);
        SetWindowText(hEdit, buf);

        // start the timer
        SetTimer(GetParent(hWnd), 1, 100, 0);

        return btnProc(hWnd, uMsg, wParam, lParam);
    case WM_LBUTTONUP:
        // reset t
        t = 0;

        // stop the timer
        KillTimer(GetParent(hWnd), 1);

        return btnProc(hWnd, uMsg, wParam, lParam);
    default:
        return btnProc(hWnd, uMsg, wParam, lParam);
    }
    return 0;
}

LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
    switch (uMsg) {
    case WM_CREATE:
        hEdit = CreateWindow(L"EDIT", L"0", WS_CHILD | WS_VISIBLE | ES_CENTER | ES_READONLY, 100, 100, 100, 20, hWnd, (HMENU)1, g_hInst, NULL);
        hButton = CreateWindow(L"BUTTON", L"▴", WS_CHILD | WS_VISIBLE | BS_LEFT, 200 + 4, 100, 20, 20, hWnd, (HMENU)2, g_hInst, NULL);

        // subclass the button control
        btnProc = (WNDPROC)SetWindowLongPtr(hButton, GWLP_WNDPROC, (LONG_PTR)StaticButtonProc);
        break;
    case WM_TIMER:
        // 300ms delay
        if (t < 300) {
            t += 100;
        }
        else {
            // increment counter & continuously update Edit Control while button is depressed
            counter += 1;
            wchar_t buf[32];
            wsprintf(buf, L"%d", counter);
            SetWindowText(hEdit, buf);
        }
        break;
    case WM_DESTROY:
        PostQuitMessage(EXIT_SUCCESS);
        break;
    default:
        return DefWindowProc(hWnd, uMsg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(
    HINSTANCE hInst,
    HINSTANCE hPrevInst,
    LPSTR lpCmdLine,
    int iCmdShow)
{
    g_hInst = hInst;

    WNDCLASS wndClass = {
        .style          = CS_HREDRAW | CS_VREDRAW,
        .lpfnWndProc    = WindowProc,
        .hInstance      = hInst,
        .hIcon          = LoadIcon(NULL, IDI_APPLICATION),
        .hCursor        = LoadCursor(NULL, IDC_ARROW),
        .hbrBackground  = (HBRUSH)GetStockObject(LTGRAY_BRUSH),
        .lpszMenuName   = NULL,
        .lpszClassName  = L"app",
    };
    if (!RegisterClass(&wndClass)) {
        OutputDebugString(L"Failed to register window class\n");
        return EXIT_FAILURE;
    }
    if (!CreateWindow(
        L"app",
        L"Counter",
        WS_OVERLAPPEDWINDOW | WS_VISIBLE,
        0, 0,
        1280, 720,
        NULL, NULL,
        hInst, NULL))
    {
        OutputDebugString(L"Failed to create window\n");
        return EXIT_FAILURE;
    }
    MSG msg = {};
    while (GetMessage(&msg, NULL, 0, 0)) {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    return (int)msg.wParam;
}
1
  • BTW: you tagged C++, but your code is actually C. Commented Jul 8, 2024 at 8:56

1 Answer 1

5

If you press the mouse , then release it immediately and press it again, a WM_LBUTTONDBLCLK instead of WM_LBUTTONDOWN is triggered.

As in StaticButtonProc you don't handle that message, nothing happens.

Just handle the WM_LBUTTONDBLCLK the same way as you handle WM_LBUTTONDOWN:

static LRESULT CALLBACK StaticButtonProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
  switch (uMsg) {
  case WM_LBUTTONDOWN:
  case WM_LBUTTONDBLCLK:  // <<<<<<<<<< add this line
    // increment counter & update Edit Control
    counter++;
    ...
Sign up to request clarification or add additional context in comments.

2 Comments

Wow, I didn't imagine the fix would be that easy. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.