Code:
LRESULT CALLBACK HelloWndProc (HWND, UINT, WPARAM, LPARAM);
void DrawRect(HWND hwnd);
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCMLine, int iCmdShow){
static TCHAR szAppName[] = TEXT ("HelloApplication");
static HWND hwnd;
MSG msg;
WNDCLASS wndclass;
wndclass.style = CS_HREDRAW | CS_VREDRAW;
wndclass.lpfnWndProc = HelloWndProc;
wndclass.cbClsExtra = 0;
wndclass.cbWndExtra = sizeof(long)*2;
wndclass.hInstance = hInstance;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW);
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
wndclass.lpszMenuName = NULL;
wndclass.lpszClassName = szAppName;
if (!RegisterClass (&wndclass)){
MessageBox (NULL, TEXT ("This program requires Windows 95/98/NT"),
szAppName, MB_ICONERROR);
return 0;
}
hwnd = CreateWindow(szAppName,
TEXT("Quiz 2"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
800,
600,
NULL,
NULL,
hInstance,
NULL);
SetWindowLong(hwnd,0,0);
SetWindowLong(hwnd,4,0);
ShowWindow(hwnd, iCmdShow);
UpdateWindow(hwnd);
while (TRUE){
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else{
DrawRect(hwnd);
Sleep(500);
}
}
return msg.wParam;
}
void DrawRect(HWND hwnd)
{
HDC hdc;
HBRUSH hBrush = CreateSolidBrush(RGB(0, 2, 255));
HPEN hPen = CreatePen(PS_DASH, 1,(RGB(250, 5, 5)));
static int cxClient = 400;
static int cyClient = 300;
static int EndX, EndY;
long x, y;
RECT rect;
PAINTSTRUCT ps ;
hdc = GetDC(hwnd);
SelectObject(hdc, hBrush);
SelectObject(hdc, hPen);
x = GetWindowLong(hwnd,0);
y = GetWindowLong(hwnd,4);
BeginPaint(hwnd, &ps);
SetMapMode(hdc, MM_ISOTROPIC);
SetWindowExtEx(hdc, cxClient, -cyClient, NULL);
SetViewportExtEx(hdc, 20, 40, NULL);
SetViewportOrgEx(hdc, 400, 600, NULL);
x *=2;
y *=2;
if ((x>= 6000) || (y>= 160000)){
x = 200;
y = 600;
}
if ((x==0) || (y==0)){
x = 200;
y = 600;
}
Rectangle(hdc,0,0,x,y);
SetWindowLong(hwnd,0,x);
SetWindowLong(hwnd,4,y);
EndPaint (hwnd, &ps) ;
}
LRESULT CALLBACK HelloWndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam){
switch (message)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, message, wParam, lParam);
}