home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / progwin / chap04 / connect.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-11-12  |  3.2 KB  |  100 lines

  1. /*--------------------------------------------------
  2.    CONNECT.C -- Connect-the-Dots Mouse Demo Program
  3.                 (c) Charles Petzold, 1990
  4.   --------------------------------------------------*/
  5.  
  6. #include <windows.h>
  7. #define MAXPOINTS 1000
  8.  
  9. long FAR PASCAL WndProc (HWND, WORD, WORD, LONG) ;
  10.  
  11. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  12.                     LPSTR lpszCmdLine, int nCmdShow)
  13.      {
  14.      static char szAppName[] = "Connect" ;
  15.      HWND        hwnd ;
  16.      MSG         msg ;
  17.      WNDCLASS    wndclass ;
  18.  
  19.      if (!hPrevInstance) 
  20.           {
  21.           wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
  22.           wndclass.lpfnWndProc   = WndProc ;
  23.           wndclass.cbClsExtra    = 0 ;
  24.           wndclass.cbWndExtra    = 0 ;
  25.           wndclass.hInstance     = hInstance ;
  26.           wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
  27.           wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
  28.           wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
  29.           wndclass.lpszMenuName  = NULL ;
  30.           wndclass.lpszClassName = szAppName ;
  31.  
  32.           RegisterClass (&wndclass) ;
  33.           }
  34.  
  35.      hwnd = CreateWindow (szAppName, "Connect-the-Points Mouse Demo",
  36.                          WS_OVERLAPPEDWINDOW,
  37.                          CW_USEDEFAULT, CW_USEDEFAULT,
  38.                          CW_USEDEFAULT, CW_USEDEFAULT,
  39.                          NULL, NULL, hInstance, NULL) ;
  40.  
  41.      ShowWindow (hwnd, nCmdShow) ;
  42.      UpdateWindow (hwnd) ;
  43.  
  44.      while (GetMessage (&msg, NULL, 0, 0))
  45.           {
  46.           TranslateMessage (&msg) ;
  47.           DispatchMessage (&msg) ;
  48.           }
  49.      return msg.wParam ;
  50.      }
  51.  
  52. long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam)
  53.      {
  54.      static POINT points[MAXPOINTS] ;
  55.      static short nCount ;
  56.      HDC          hdc ;
  57.      PAINTSTRUCT  ps ;
  58.      short        i, j ;
  59.  
  60.      switch (message)
  61.           {
  62.           case WM_LBUTTONDOWN:
  63.                nCount = 0 ;
  64.                InvalidateRect (hwnd, NULL, TRUE) ;
  65.                return 0 ;
  66.  
  67.           case WM_MOUSEMOVE:
  68.                if (wParam & MK_LBUTTON && nCount < 1000)
  69.                     {
  70.                     points [nCount++] = MAKEPOINT (lParam) ;
  71.                     hdc = GetDC (hwnd) ;
  72.                     SetPixel (hdc, LOWORD (lParam), HIWORD (lParam), 0L) ;
  73.                     ReleaseDC (hwnd, hdc) ;
  74.                     }
  75.                return 0 ;
  76.  
  77.           case WM_LBUTTONUP:
  78.                InvalidateRect (hwnd, NULL, FALSE) ;
  79.                return 0 ;
  80.  
  81.           case WM_PAINT:
  82.                hdc = BeginPaint (hwnd, &ps) ;
  83.  
  84.                for (i = 0 ; i < nCount - 1 ; i++)
  85.                     for (j = i ; j < nCount ; j++)
  86.                          {
  87.                          MoveTo (hdc, points[i].x, points[i].y) ;
  88.                          LineTo (hdc, points[j].x, points[j].y) ;
  89.                          }
  90.  
  91.                EndPaint (hwnd, &ps) ;
  92.                return 0 ;
  93.  
  94.           case WM_DESTROY:
  95.                PostQuitMessage (0) ;
  96.                return 0 ;
  97.           }
  98.      return DefWindowProc (hwnd, message, wParam, lParam) ;
  99.      }
  100.