Code:
#include <windows.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
HWND hwnd ;
MSG msg ;
WNDCLASS wc ;
wc.style = CS_HREDRAW | CS_VREDRAW ;
wc.lpfnWndProc = WndProc ;
wc.cbClsExtra = 0 ;
wc.cbWndExtra = sizeof(long) ;
wc.hInstance = hInstance ;
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wc.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wc.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wc.lpszMenuName = NULL ;
wc.lpszClassName = "My class";
if (!RegisterClass (&wc))
{
MessageBox (NULL, "Error in registration",
"Error", MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow ("My class",
"Problem 1",
WS_OVERLAPPEDWINDOW,
100,
100,
300,
300,
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 Msg, WPARAM wParam, LPARAM lParam)
{
HDC hDC;
PAINTSTRUCT ps;
POINT Pt[4] = { { 0, 130 }, { 30, 130 }, { 260, 130 }, { 290, 130} };
int side = 8;
int k;
int x;
int y;
RECT square[4];
switch(Msg)
{
case WM_CREATE:
SetWindowLong(hWnd,0,0);
break;
case WM_LBUTTONDOWN:
x = LOWORD(lParam);
y = HIWORD(lParam);
for(k = 0; k < 4; k++)
{
square[k].left = Pt[k].x - side/2;
square[k].top = Pt[k].y - side/2;
square[k].right = Pt[k].x + side/2;
square[k].bottom = Pt[k].y + side/2;
}
for(k = 0; k < 4; k++)
{
if((square[k].left <= x) && (x <= square[k].right))
if((square[k].top <= y) && (y <= square[k].bottom))
{
SetWindowLong(hWnd,0,!GetWindowLong(hWnd,0));
InvalidateRect(hWnd, &square[k], TRUE);
break;
}
}
break;
case WM_PAINT:
hDC = BeginPaint(hWnd, &ps);
for(k = 0; k < 4; k++)
{
Rectangle(hDC, Pt[k].x - side/2,Pt[k].y - side/2 , Pt[k].x + side/2 , Pt[k].y + side/2);
PolyBezier(hDC, Pt, 4);
}
for(k = 0; k < 4; k++)
{
square[k].left = Pt[k].x - side/2;
square[k].top = Pt[k].y - side/2;
square[k].right = Pt[k].x + side/2;
square[k].bottom = Pt[k].y + side/2;
if(GetWindowLong(hWnd, 0))
FillRect(hDC,&square[k],(HBRUSH)GetStockObject(BLACK_BRUSH));
}
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(WM_QUIT);
break;
}
return DefWindowProc(hWnd, Msg, wParam, lParam);
}