#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
PSTR szCmdLine,
int iCmdShow) {
static TCHAR szAppName[] = TEXT("Draw 100 circles");
HWND hwnd;
MSG msg;
WNDCLASS wndclass;
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 | WS_VSCROLL | WS_HSCROLL,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
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 int cxClient, cyClient;
HDC hdc;
PAINTSTRUCT ps;
SCROLLINFO si;
int i, j, iVertPos, iHorzPos;
HBRUSH hBrush;
switch(message) {
case WM_SIZE:
cxClient = LOWORD(lParam);
cyClient = HIWORD(lParam);
si.cbSize = sizeof(si);
si.fMask = SIF_PAGE | SIF_RANGE;
si.nPage = cyClient;
si.nMin = 0;
si.nMax = 10 * cyClient;
SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
si.nPage = cxClient;
si.nMin = 0;
si.nMax = 10 * cxClient;
SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
return 0;
case WM_VSCROLL:
si.cbSize = sizeof(si);
si.fMask = SIF_ALL;
GetScrollInfo(hwnd, SB_VERT, &si);
iVertPos = si.nPos;
switch(LOWORD(wParam)) {
case SB_LINEUP:
si.nPos -= 10;
break;
case SB_LINEDOWN:
si.nPos += 10;
break;
case SB_PAGEUP:
si.nPos -= si.nPage;
break;
case SB_PAGEDOWN:
si.nPos += si.nPage;
break;
case SB_THUMBTRACK:
si.nPos = si.nTrackPos;
break;
default:
break;
}
si.fMask = SIF_POS;
SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
GetScrollInfo(hwnd, SB_VERT, &si);
if(si.nPos != iVertPos) {
ScrollWindow(hwnd, 0, iVertPos - si.nPos, NULL, NULL);
UpdateWindow(hwnd);
}
return 0;
case WM_HSCROLL:
si.cbSize = sizeof(si);
si.fMask = SIF_ALL;
GetScrollInfo(hwnd, SB_HORZ, &si);
iHorzPos = si.nPos;
switch(LOWORD(wParam)) {
case SB_LINELEFT:
si.nPos -= 20;
break;
case SB_LINERIGHT:
si.nPos += 20;
break;
case SB_PAGELEFT:
si.nPos -= cxClient;
break;
case SB_PAGERIGHT:
si.nPos += cxClient;
break;
case SB_THUMBTRACK:
si.nPos = si.nTrackPos;
default:
break;
}
si.fMask = SIF_POS;
SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
GetScrollInfo(hwnd, SB_HORZ, &si);
if(si.nPos != iHorzPos) {
ScrollWindow(hwnd, iHorzPos - si.nPos, 0, NULL, NULL);
UpdateWindow(hwnd);
}
return 0;
case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
si.cbSize = sizeof(si);
si.fMask = SIF_POS;
GetScrollInfo(hwnd, SB_VERT, &si);
iVertPos = si.nPos;
GetScrollInfo(hwnd, SB_HORZ, &si);
iHorzPos = si.nPos;
SetWindowOrgEx(hdc, iHorzPos, iVertPos, NULL);
hBrush = CreateSolidBrush(RGB(0, 100, 100));
SelectObject(hdc, hBrush);
for(i = 0; i < 10; i++)
for(j = 0; j < 10; j++)
{
Ellipse(hdc, j * cxClient + cxClient / 4, i * cyClient + (cyClient / 2 - cxClient / 4),
j * cxClient + 3 * cxClient / 4, i * cyClient + (cyClient / 2 + cxClient / 4));
}
EndPaint(hwnd, &ps);
DeleteObject(hBrush);
return 0;
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
|