home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / win_lrn / cursor / gcurpos.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-11  |  2.9 KB  |  115 lines

  1. /*
  2.  *  GetCursorPos
  3.  *
  4.  *  This program demonstrates the use of the function GetCursorPos.
  5.  *  GetCursorPos retrieves the cursor position in screen coordinates.
  6.  *  This means that even if the cursor is not in the current window,
  7.  *  it's position will be retrieved. GetCursorPos is called from
  8.  *  WinMain () in this application.
  9.  *
  10.  */
  11.  
  12. #include "windows.h"
  13. #include <stdio.h>
  14.  
  15. long    FAR PASCAL WndProc (HWND, unsigned, WORD, LONG);
  16. extern int      FAR PASCAL lstrlen (LPSTR);
  17.  
  18. static char    szAppName [] = "GCURPOS";
  19. static char    szFuncName [] = "GetCursorPos";
  20.  
  21. int     PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, cmdShow)
  22. HANDLE hInstance, hPrevInstance;
  23. LPSTR lpszCmdLine;
  24. int    cmdShow;
  25.   {
  26.   HWND      hWnd;
  27.   WNDCLASS  wndclass;
  28.   MSG       msg;
  29.  
  30.   if (!hPrevInstance)
  31.     {
  32.     wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  33.     wndclass.lpfnWndProc   = WndProc;
  34.     wndclass.cbClsExtra    = 0;
  35.     wndclass.cbWndExtra    = 0;
  36.     wndclass.hInstance     = hInstance;
  37.     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION);
  38.     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW);
  39.     wndclass.hbrBackground = GetStockObject (WHITE_BRUSH);
  40.     wndclass.lpszMenuName  = NULL;
  41.     wndclass.lpszClassName = szAppName;
  42.  
  43.     if (!RegisterClass (&wndclass))
  44.       return FALSE;
  45.     }
  46.  
  47.   hWnd = CreateWindow (szAppName,
  48.                       szFuncName,
  49.                       WS_OVERLAPPEDWINDOW,
  50.                       CW_USEDEFAULT,
  51.                       0,
  52.                       CW_USEDEFAULT,
  53.                       0,
  54.                       NULL,
  55.                       NULL,
  56.                       hInstance,
  57.                       NULL);
  58.  
  59.   ShowWindow (hWnd, cmdShow);
  60.   UpdateWindow (hWnd);
  61.  
  62.   while (GetMessage (&msg, NULL, 0, 0))
  63.     {
  64.     TranslateMessage (&msg);
  65.     DispatchMessage (&msg);
  66.     }
  67.   return (msg.wParam);
  68.   }
  69.  
  70.  
  71. long    FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  72. HWND     hWnd;
  73. unsigned    iMessage;
  74. WORD     wParam;
  75. LONG     lParam;
  76.   {
  77.   HDC   hDC;
  78.   HMENU hMenu;
  79.   POINT CursorPos;
  80.   char    szBuffer[30];
  81.  
  82.   switch (iMessage)
  83.     {
  84.     case WM_CREATE:
  85.       hMenu = CreateMenu ();
  86.       ChangeMenu (hMenu, NULL, (LPSTR)"Curse Tracking", 100, MF_APPEND);
  87.       SetMenu (hWnd, hMenu);
  88.       break;
  89.  
  90.     case WM_INITMENU:
  91.       InvalidateRect (hWnd, NULL, TRUE);
  92.       break;
  93.  
  94.     case WM_COMMAND:
  95.       if (wParam == 100)
  96.         {
  97.         hDC = GetDC (hWnd);
  98.         GetCursorPos ( (LPPOINT) & CursorPos);
  99.         sprintf (szBuffer, "Cursor Position: x = %d y = %d", CursorPos.x,
  100.             CursorPos.y);
  101.         TextOut (hDC, 10, 10,  (LPSTR)szBuffer, lstrlen ( (LPSTR)szBuffer));
  102.         ReleaseDC (hWnd, hDC);
  103.         }
  104.       break;
  105.  
  106.     case WM_DESTROY:
  107.       PostQuitMessage (0);
  108.       break;
  109.  
  110.     default:
  111.       return DefWindowProc (hWnd, iMessage, wParam, lParam);
  112.     }
  113.   return (0L);
  114.   }
  115.