Code:
/*---------------------------------------------------
Scroll Bar example
---------------------------------------------------*/
#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int nCmdShow)
{
static TCHAR szAppName[] = TEXT("New Application");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_VREDRAW | CS_HREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.hInstance = hInstance;
wndclass.lpszClassName = szAppName;
if (!RegisterClass(&wndclass))
{
MessageBox(NULL, TEXT("Error registering window class!"),
szAppName, 0);
return 0;
}
hwnd = CreateWindow(szAppName, TEXT("This is the First Window"),
WS_VSCROLL | WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
CW_USEDEFAULT, CW_USEDEFAULT,
CW_USEDEFAULT, NULL,
NULL, hInstance, NULL);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
RECT rect;
HDC hdc;
TEXTMETRIC tm;
static int iVscrollPos, cyChar;
int y;
switch (message)
{
case WM_CREATE:
hdc = GetDC(hwnd);
InvalidateRect(hwnd, &rect, FALSE);
GetTextMetrics(hdc, &tm);
ReleaseDC(hwnd, hdc);
cyChar = tm.tmHeight + tm.tmExternalLeading;
SetScrollRange(hwnd, SB_VERT, 0, 5, TRUE);
SetScrollPos(hwnd, SB_VERT, iVscrollPos, TRUE);
return 0;
case WM_PAINT:
y = cyChar * iVscrollPos;
hdc = BeginPaint(hwnd, &ps);
GetClientRect(hwnd, &rect);
TextOut(hdc, 0, y, TEXT("TESTING1"), 8);
TextOut(hdc, 500, y + 500, TEXT("TESTING2"), 8);
EndPaint(hwnd, &ps);
return 0;
case WM_VSCROLL:
switch (LOWORD (lParam))
{
case SB_LINEUP:
iVscrollPos -= 1;
break;
case SB_LINEDOWN:
iVscrollPos += 1;
break;
default:
break;
}
if (iVscrollPos != GetScrollPos(hwnd, SB_VERT))
{
SetScrollPos(hwnd, SB_VERT, iVscrollPos, TRUE);
InvalidateRect(hwnd, NULL, TRUE);
}
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
Thanks.