/*--------------------------------------------------
   LINEDEMO.C -- Line-Drawing Demonstration Program
                 (c) Charles Petzold, 1998
  --------------------------------------------------*/

#include <windows.h>
#include<math.h>

#define ID_TIMER    1

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                    PSTR szCmdLine, int iCmdShow)
{
     static TCHAR szAppName[] = TEXT ("LineDemo") ;
     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 = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = szAppName ;
    
     if (!RegisterClass (&wndclass))
     {
          MessageBox (NULL, TEXT ("Program requires Windows NT!"),
                      szAppName, MB_ICONERROR) ;
          return 0 ;
     }
    
     hwnd = CreateWindow (szAppName, TEXT ("Line Demonstration"),
                          WS_OVERLAPPEDWINDOW,
                          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,xo,yo,dx,dy,iCount;
	 static int Bar_xo,Bar_yo,Bar_Width=60,Bar_Height=-10;
     HDC         hdc ;
     PAINTSTRUCT ps ;
     HBRUSH   hBrush;
  HPEN   hPen;
  static int r,x,y;
 
     switch (message)
     {
  case WM_CREATE :
  
   hdc=GetDC(hwnd);
          SetTimer (hwnd, ID_TIMER,10, NULL) ;
      dx=2;
      dy=2;
    r = 40 ;
    ReleaseDC(hwnd,hdc);
         return 0 ;

     case WM_SIZE:
          cxClient = LOWORD (lParam) ;
          cyClient = HIWORD (lParam) ;
    xo= cxClient/2;
    yo= cyClient/2;
  
   Bar_xo=cxClient/2;
   Bar_yo=cyClient-20;

          return 0 ;

  case WM_KEYDOWN:
     switch(wParam)
     {
   case VK_LEFT :
    Bar_xo -= 5 ;
    if( Bar_xo < Bar_Width ) Bar_xo = Bar_Width ;
    break ;
   case VK_RIGHT :
    Bar_xo += 5 ;
    if( Bar_xo > cxClient-Bar_Width ) Bar_xo = cxClient-Bar_Width ;
    break ;

     }
     return 0;

 case WM_LBUTTONDOWN:
 
   x = LOWORD (lParam) ;
   y = HIWORD (lParam) ;

 
   if((xo-x)*(xo-x)+(yo-y)*(yo-y)< r*r){ 
  r=r+60;
 }
   else
    
  r=r-50;
  return 0;

 
 case WM_TIMER :
       InvalidateRect(hwnd, NULL, TRUE ) ;         
     return 0 ;

  case WM_PAINT:
          hdc = BeginPaint (hwnd, &ps) ;
  
    hBrush = CreateSolidBrush(RGB(255,0,0));
    hPen=CreatePen(PS_SOLID,4,RGB(0,255,0));
    SelectObject(hdc,hBrush);
    SelectObject(hdc,hPen);
   xo=xo+dx;
    yo=yo+dy;
                  

    if(xo>cxClient-r) dx=-dx;
    if(xo<r) dx=-dx;
    if(yo>cyClient-r) dy=-dy;
    if(yo<r) dy=-dy;

 
    Ellipse   (hdc,xo-r,yo-r, xo+r,yo+r) ;
          Rectangle (hdc, Bar_xo-Bar_Width, Bar_yo-Bar_Height, Bar_xo+Bar_Width, Bar_yo+Bar_Height) ;
         DeleteObject(hPen);
         DeleteObject(hBrush);

 
 EndPaint (hwnd, &ps) ;
          break;
   
  
case WM_DESTROY:

   KillTimer (hwnd,ID_TIMER) ;
         PostQuitMessage (0) ;
          return 0 ;
     }
     return DefWindowProc (hwnd, message, wParam, lParam) ;
}

         
        
        

    

