#include <windows.h>
HINSTANCE hInst;
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
PSTR szCmdLine,
int iCmdShow) {
static TCHAR szAppName[] = TEXT("scrollDemo");
HWND hwnd;
WNDCLASS wndclass;
MSG msg;
hInst = hInstance;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = WndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = 0;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);
wndclass.hbrBackground = GetStockObject(WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if(!RegisterClass(&wndclass)) {
MessageBox(NULL, TEXT("Register failure..."),
szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName,
szAppName,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
0,
NULL);
ShowWindow(hwnd, iCmdShow);
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) {
static HBRUSH hBrush;
static int cxChar, cyChar;
static HWND hwndScroll, hwndStatic;
static int iVertPos;
TCHAR buffer[5];
switch(message) {
case WM_CREATE:
cxChar = LOWORD(GetDialogBaseUnits());
cyChar = HIWORD(GetDialogBaseUnits());
hwndScroll = CreateWindow(TEXT("scrollbar"),
NULL,
WS_CHILD | WS_VISIBLE | WS_TABSTOP | SBS_VERT,
cxChar, cyChar, 5 * cxChar, 5 * cyChar,
hwnd,
(HMENU)1,
hInst,
NULL);
hwndStatic = CreateWindow(TEXT("static"),
TEXT("0"),
WS_CHILD | WS_VISIBLE | SS_CENTER,
cxChar, 6 * cyChar, 5 * cxChar, cyChar,
hwnd,
(HMENU)2,
hInst,
NULL);
hBrush = CreateSolidBrush(0);
SetScrollRange(hwndScroll, SB_CTL, 0, 255, FALSE);
SetScrollPos(hwndScroll, SB_CTL, 0, TRUE);
return 0;
case WM_VSCROLL:
switch(LOWORD(wParam)) {
case SB_LINEUP:
iVertPos = max(0, min(iVertPos - 1, 255));
break;
case SB_LINEDOWN:
iVertPos = max(0, min(iVertPos + 1, 255));
break;
case SB_PAGEUP:
iVertPos = max(0, min(iVertPos - 15, 255));
break;
case SB_PAGEDOWN:
iVertPos = max(0, min(iVertPos + 15, 255));
break;
case SB_TOP:
iVertPos = 0;
break;
case SB_BOTTOM:
iVertPos = 255;
break;
case SB_THUMBTRACK:
iVertPos = HIWORD(wParam);
break;
default:
break;
}
SetScrollPos(hwndScroll, SB_CTL, iVertPos, TRUE);
wsprintf(buffer, "%i", iVertPos);
SetWindowText(hwndStatic, buffer);
hBrush = CreateSolidBrush(RGB(0, iVertPos, iVertPos));
DeleteObject((HBRUSH)SetClassLong(hwnd, GCL_HBRBACKGROUND,
hBrush));
InvalidateRect(hwnd, NULL, TRUE);
return 0;
case WM_CTLCOLORSCROLLBAR:
return (LRESULT)hBrush;
case WM_CTLCOLORSTATIC:
SetTextColor((HDC)wParam, RGB(255, 0, 0));
SetBkColor((HDC)wParam, RGB(0, iVertPos, iVertPos));
return (LRESULT)hBrush;
case WM_DESTROY:
DeleteObject(hBrush);
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
|