Mouse Wheel Scroll Example
#include <windows.h>
#include <tchar.h>
#define WM_MOUSEWHEEL 0x020A // For older Visual C++
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int scrollPosition = 0;
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASS wc;
MSG msg;
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = TEXT("ScrollDemo");
RegisterClass(&wc);
HWND hwnd = CreateWindow(
TEXT("ScrollDemo"),
TEXT("Mouse Wheel Scrolling Demo"),
WS_OVERLAPPEDWINDOW,
100,
100,
400,
300,
NULL,
NULL,
hInstance,
NULL);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int)msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd,
UINT message,
WPARAM wParam,
LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc;
TCHAR text[100];
int i;
switch(message)
{
case WM_MOUSEWHEEL:
if((short)HIWORD(wParam) > 0)
scrollPosition -= 20;
else
scrollPosition += 20;
// Limit scrolling range
if(scrollPosition < 0)
scrollPosition = 0;
if(scrollPosition > 600)
scrollPosition = 600;
InvalidateRect(hwnd, NULL, TRUE);
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
for(i = 0; i < 40; i++)
{
wsprintf(text,
TEXT("This is line number %d"),
i + 1);
TextOut(hdc,
50,
(i * 25) - scrollPosition,
text,
lstrlen(text));
}
EndPaint(hwnd, &ps);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}