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

  1. /*
  2.  *   This program demonstrates the use of the ClientToScreen () function.
  3.  *   ClientToScreen () converts client area coordinates to screen coordinates
  4.  *   (upper left of the screen being the origin). ClientToScreen () is called
  5.  *   from ParentWndProc () in response to a WM_SIZE or WM_MOVE message.
  6.  *
  7.  */
  8.  
  9. #include <windows.h>
  10.  
  11. int     PASCAL WinMain (HANDLE, HANDLE, LPSTR, int);
  12. long    FAR PASCAL ParentWndProc (HWND, unsigned, WORD, LONG);
  13. long    FAR PASCAL PopupWndProc (HWND, unsigned, WORD, LONG);
  14. int     sprintf (char *, const char *, ...);
  15.  
  16. HWND    hChild;
  17. char    szBuff[80] = " ";   /*          used for output           */
  18. short   nTextLength = 0;    /*   length of string for TextOut ()  */
  19.  
  20.  
  21. int     PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, cmdShow)
  22. HANDLE hInstance, hPrevInstance;
  23. LPSTR lpszCmdLine;
  24. int    cmdShow;
  25.   {
  26.   MSG      msg;
  27.   HWND     hParent;
  28.   WNDCLASS wndClass;
  29.  
  30.   if (!hPrevInstance)
  31.     {
  32.     wndClass.style          = CS_HREDRAW | CS_VREDRAW;
  33.     wndClass.lpfnWndProc    = ParentWndProc;
  34.     wndClass.cbClsExtra     = 0;
  35.     wndClass.cbWndExtra     = 0;
  36.     wndClass.hInstance      = hInstance;
  37.     wndClass.hIcon          = LoadIcon (NULL, NULL);
  38.     wndClass.hCursor        = LoadCursor (NULL, IDC_ARROW);
  39.     wndClass.hbrBackground  = GetStockObject (WHITE_BRUSH);
  40.     wndClass.lpszMenuName   = NULL;
  41.     wndClass.lpszClassName  = "ClientToScreenParent";
  42.  
  43.     if (!RegisterClass (&wndClass))
  44.       return FALSE;
  45.  
  46.     wndClass.style          = CS_HREDRAW | CS_VREDRAW;
  47.     wndClass.lpfnWndProc    = PopupWndProc;
  48.     wndClass.cbClsExtra     = 0;
  49.     wndClass.cbWndExtra     = 0;
  50.     wndClass.hInstance      = hInstance;
  51.     wndClass.hIcon          = NULL;
  52.     wndClass.hCursor        = LoadCursor (NULL, IDC_ARROW);
  53.     wndClass.hbrBackground  = GetStockObject (WHITE_BRUSH);
  54.     wndClass.lpszMenuName   = NULL;
  55.     wndClass.lpszClassName  = "ClientToScreenPopup";
  56.  
  57.     if (!RegisterClass (&wndClass))
  58.       return FALSE;
  59.     }
  60.  
  61.   hParent = CreateWindow ( (LPSTR)"ClientToScreenParent",
  62.                          (LPSTR)"Parent Window for ClientToScreen () Demo",
  63.                          WS_OVERLAPPEDWINDOW,
  64.                          32,
  65.                          200,
  66.                          500,
  67.                          48,
  68.                          NULL,      /* no parent */
  69.                          NULL,      /* use class menu */
  70.                          hInstance, /* handle to window instance */
  71.                          NULL);     /* no params to pass on */
  72.  
  73.   ShowWindow (hParent, cmdShow);
  74.   UpdateWindow (hParent);
  75.  
  76.   hChild = CreateWindow ( (LPSTR)"ClientToScreenPopup",
  77.                         (LPSTR)"Popup Window for ClientToScreen () Demo",
  78.                         WS_POPUP | WS_CAPTION | WS_VISIBLE,
  79.                         32,
  80.                         256,
  81.                         500,
  82.                         48,
  83.                         hParent,
  84.                         NULL,      /* use class menu */
  85.                         hInstance, /* handle to window instance */
  86.                         NULL);     /* no params to pass on */
  87.  
  88.   while (GetMessage ( (LPMSG) & msg, NULL, 0, 0))
  89.     {
  90.     TranslateMessage ( (LPMSG) & msg);
  91.     DispatchMessage ( (LPMSG) & msg);
  92.     }
  93.  
  94.   return (int)msg.wParam;
  95.   }
  96.  
  97. long    FAR PASCAL ParentWndProc (hWnd, message, wParam, lParam)
  98. HWND      hWnd;
  99. unsigned  message;
  100. WORD      wParam;
  101. LONG      lParam;
  102.   {
  103.   POINT myPoint;       /* point structure used in calls to ClientToScreen () */
  104.  
  105.   switch (message)
  106.     {
  107.     case WM_DESTROY:
  108.       PostQuitMessage (0);
  109.       break;
  110.  
  111.     case WM_MOVE:
  112.     case WM_SIZE:
  113.       DefWindowProc (hWnd, message, wParam, lParam);
  114.       myPoint.x = 0;
  115.       myPoint.y = 0;
  116.       ClientToScreen (hWnd, (LPPOINT) & myPoint);
  117.       nTextLength = sprintf (szBuff,
  118.           "Screen Coordinates of Upper Left-hand Corner: (%d, %d)",
  119.           myPoint.x,
  120.           myPoint.y);
  121.       InvalidateRect (hChild, NULL, TRUE);
  122.       UpdateWindow (hChild);
  123.       break;
  124.  
  125.     default:
  126.       return DefWindowProc (hWnd, message, wParam, lParam);
  127.     break;
  128.     }
  129.   return (0L);
  130.   }
  131.  
  132. long    FAR PASCAL PopupWndProc (hWnd, message, wParam, lParam)
  133. HWND     hWnd;
  134. unsigned message;
  135. WORD     wParam;
  136. LONG     lParam;
  137.   {
  138.   PAINTSTRUCT ps;
  139.  
  140.   switch (message)
  141.     {
  142.     case WM_PAINT:
  143.       BeginPaint (hWnd, &ps);
  144.       TextOut (ps.hdc, 5, 5, szBuff, nTextLength);
  145.       EndPaint (hWnd, &ps);
  146.       break;
  147.     default:
  148.       return DefWindowProc (hWnd, message, wParam, lParam);
  149.       break;
  150.     }
  151.   return (0L);
  152.   }
  153.