My program increments counter once if the button is depressed, but it does not increment continuously if the button is held down. How do I acheive that?
Here’s a simple example code:
#include <Windows.h>
HINSTANCE g_hInst;
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
static int n = 0;
static HWND hEdit;
static HWND hButton;
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)1, g_hInst, NULL);
break;
case WM_COMMAND: {
HWND ctlHwnd = (HWND)lParam;
int ctlId = LOWORD(wParam);
int notificationCode = HIWORD(wParam);
if (notificationCode == BN_CLICKED) {
n++;
wchar_t buf[40] = {0};
wsprintf(buf, L"%d", n);
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 classn");
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 windown");
return EXIT_FAILURE;
}
MSG msg = {};
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}