WinAPI 中的 WM_LBUTTONDOWN 延迟问题

问题描述 投票:0回答:1

这是我之前问题的后续。根据我收到的评论,我设法让按钮在按下按钮时不断更新计数器,但现在我遇到了新问题。

如果我单击该按钮,它会增加计数器并更新编辑控件。如果我释放然后立即再次单击按钮,它不会增加和更新。

似乎存在与 WM_LBUTTONDOWN 消息相关的延迟,与我在 WM_TIMER 处理下强制执行的延迟无关。如何解决这个问题:

#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;
}
c++ winapi button windows-messages
1个回答
0
投票

您会在

WM_LBUTTONDBLCLK
中收到
StaticButtonProc
消息。您需要处理该事件,否则双击不会执行任何操作。

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++;
    ...
© www.soinside.com 2019 - 2024. All rights reserved.