home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / cursor / clipcurs.c next >
C/C++ Source or Header  |  1988-08-10  |  3KB  |  113 lines

  1. /*
  2.  *   This program demonstrates use of the ClipCursor () function.
  3.  *   ClipCursor () confines movement of the cursor to the rectangle specified
  4.  *   by the "lpRect" parameter . In WinMain (), GetClientRect () is
  5.  *   called, then ClientToScreen () is called to convert the client area
  6.  *   coordinates to screen coordinates (because ClipCursor () is a screen-
  7.  *   coordinate relative function).  ClipCursor () is then called to confine
  8.  *   the cursor to the left half of the client area.
  9.  */
  10.  
  11. #include "windows.h"
  12.  
  13. long    FAR PASCAL HelloWndProc (HWND, unsigned, WORD, LONG);
  14.  
  15. /* Procedure called when the application is loaded for the first time */
  16. BOOL HelloInit (hInstance)
  17. HANDLE hInstance;
  18.   {
  19.   PWNDCLASS   pHelloClass;
  20.  
  21.   pHelloClass = (PWNDCLASS)LocalAlloc (LPTR, sizeof (WNDCLASS));
  22.  
  23.   pHelloClass->hCursor        = LoadCursor (NULL, IDC_ARROW);
  24.   pHelloClass->hIcon          = LoadIcon (hInstance, NULL);
  25.   pHelloClass->lpszMenuName   = (LPSTR)NULL;
  26.   pHelloClass->lpszClassName  = (LPSTR)"ClipCursor";
  27.   pHelloClass->hbrBackground  = (HBRUSH)GetStockObject (WHITE_BRUSH);
  28.   pHelloClass->hInstance      = hInstance;
  29.   pHelloClass->style          = CS_HREDRAW | CS_VREDRAW;
  30.   pHelloClass->lpfnWndProc    = HelloWndProc;
  31.  
  32.   if (!RegisterClass ( (LPWNDCLASS)pHelloClass))
  33.     return FALSE;
  34.  
  35.   LocalFree ( (HANDLE)pHelloClass);
  36.   return TRUE;        /* Initialization succeeded */
  37.   }
  38.  
  39.  
  40. int     PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, cmdShow)
  41. HANDLE  hInstance, hPrevInstance;
  42. LPSTR   lpszCmdLine;
  43. int    cmdShow;
  44.   {
  45.   MSG   msg;
  46.   HWND  hWnd;
  47.   RECT  rClientRect;
  48.   POINT UpperLeft;
  49.   POINT LowerRight;
  50.  
  51.   HelloInit (hInstance);
  52.   hWnd = CreateWindow ( (LPSTR)"ClipCursor",
  53.                       (LPSTR)"ClipCursor ()",
  54.                       WS_OVERLAPPEDWINDOW,
  55.                       CW_USEDEFAULT,
  56.                       CW_USEDEFAULT,
  57.                       CW_USEDEFAULT,
  58.                       CW_USEDEFAULT,
  59.                       (HWND)NULL,        /* no parent */
  60.                       (HMENU)NULL,       /* use class menu */
  61.                       (HANDLE)hInstance, /* handle to window instance */
  62.                       (LPSTR)NULL);      /* no params to pass on */
  63.  
  64.   ShowWindow (hWnd, cmdShow);
  65.   UpdateWindow (hWnd);
  66.  
  67.   GetClientRect (hWnd, (LPRECT) & rClientRect);
  68.   UpperLeft.x  = rClientRect.left;
  69.   UpperLeft.y  = rClientRect.top;
  70.   ClientToScreen (hWnd, (LPPOINT) & UpperLeft);
  71.  
  72.   LowerRight.x = rClientRect.bottom;
  73.   LowerRight.y = rClientRect.right / 2;
  74.   ClientToScreen (hWnd, (LPPOINT) & LowerRight);
  75.  
  76.   rClientRect.left   = UpperLeft.x;
  77.   rClientRect.top    = UpperLeft.y;
  78.   rClientRect.bottom = LowerRight.x;
  79.   rClientRect.right  = LowerRight.y;
  80.  
  81.   ClipCursor ( (LPRECT) & rClientRect);
  82.  
  83.   while (GetMessage ( (LPMSG) & msg, NULL, 0, 0))
  84.     {
  85.     TranslateMessage ( (LPMSG) & msg);
  86.     DispatchMessage ( (LPMSG) & msg);
  87.     }
  88.   return (int)msg.wParam;
  89.   }
  90.  
  91.  
  92. /* Procedures which make up the window class. */
  93. long    FAR PASCAL HelloWndProc (hWnd, message, wParam, lParam)
  94. HWND hWnd;
  95. unsigned    message;
  96. WORD wParam;
  97. LONG lParam;
  98.   {
  99.   switch (message)
  100.     {
  101.   case WM_DESTROY:
  102. /*** release the cursor (a shared resource) to the rest of the system ***/
  103.     ClipCursor ( (LPRECT)NULL);
  104.     PostQuitMessage (0);
  105.     break;
  106.  
  107.   default:
  108.     return DefWindowProc (hWnd, message, wParam, lParam);
  109.     break;
  110.     }
  111.   return (0L);
  112.   }
  113.