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

  1. /*
  2.  * Function(s) demonstrated in this program: ChildWindowFromPoint
  3.  * Compiler version:  C 5.10
  4.  * Description:  This function determines which, if any, of the child windows
  5.  *               belonging to the given parent window contains the specified
  6.  *               point.
  7.  * Additional Comments:  This program also illustrates the subclassing of
  8.  *                       windows.
  9.  */
  10.  
  11. #define NOMINMAX
  12. #include <windows.h>
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include "childfp.h"
  16.  
  17. char    szMessageBuffer [150] = "    Demo Program (ChildWindowFromPoint).";
  18.  
  19. char    szHelpBuffer [200] = "     Choose to enter point coordinates, or\n\
  20. let the computer choose random coordinates.\n\
  21. The program will use the ChildWindowFromPoint\n\
  22. function to compute which window the point\n\
  23. resides in.";
  24.  
  25. HWND     hWndMain, hWndChild1, hWndChild2, hX, hY, hOK2;
  26. HANDLE   hInstMain;
  27. FARPROC  lpProcEnterPoint;
  28. FARPROC  lpProcNewEnterPoint;
  29. FARPROC  lpProcOldX;
  30. FARPROC  lpProcOldY;
  31.  
  32. char    szXValue [40];
  33. char    szYValue [40];
  34.  
  35. /****************************************************************************/
  36.  
  37. int     PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  38. HANDLE  hInstance, hPrevInstance;
  39. LPSTR   lpszCmdLine;
  40. int     nCmdShow;
  41.   {
  42.   static char   szAppName [] = "ChildFp";
  43.   static char   szChildClass [] = "ChildFpChild";
  44.  
  45.   HWND        hWnd;
  46.   WNDCLASS    wndclass;
  47.   MSG msg;
  48.   short xScreen, yScreen;
  49.  
  50.   if (!hPrevInstance)
  51.     {
  52.     wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  53.     wndclass.lpfnWndProc   = WndProc;
  54.     wndclass.cbClsExtra    = 0;
  55.     wndclass.cbWndExtra    = 0;
  56.     wndclass.hInstance     = hInstance;
  57.     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION);
  58.     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW);
  59.     wndclass.hbrBackground = GetStockObject (WHITE_BRUSH);
  60.     wndclass.lpszMenuName  = szAppName;
  61.     wndclass.lpszClassName = szAppName;
  62.  
  63.     if (!RegisterClass (&wndclass))
  64.       return FALSE;
  65.  
  66.     wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  67.     wndclass.lpfnWndProc   = ChildWndProc;
  68.     wndclass.cbClsExtra    = 0;
  69.     wndclass.cbWndExtra    = 0;
  70.     wndclass.hInstance     = hInstance;
  71.     wndclass.hIcon         = NULL;
  72.     wndclass.hCursor       = LoadCursor (NULL, IDC_CROSS);
  73.     wndclass.hbrBackground = GetStockObject (LTGRAY_BRUSH);
  74.     wndclass.lpszMenuName  = NULL;
  75.     wndclass.lpszClassName = szChildClass;
  76.  
  77.     if (!RegisterClass (&wndclass))
  78.       return FALSE;
  79.     }
  80.  
  81.   xScreen = GetSystemMetrics (SM_CXSCREEN);
  82.   yScreen = GetSystemMetrics (SM_CYSCREEN);
  83.  
  84.   hWnd = CreateWindow (szAppName,                /* window class name       */
  85.                       "Child Window From Point", /* window caption          */
  86.                       WS_OVERLAPPEDWINDOW,       /* window style            */
  87.                       CW_USEDEFAULT,             /* initial x position      */
  88.                       0,                         /* initial y position      */
  89.                       CW_USEDEFAULT,             /* initial x size          */
  90.                       0,                         /* initial y size          */
  91.                       NULL,                      /* parent window handle    */
  92.                       NULL,                      /* window menu handle      */
  93.                       hInstance,                 /* program instance handle */
  94.                       NULL);                     /* create parameters       */
  95.  
  96.   ShowWindow (hWnd, nCmdShow);
  97.   UpdateWindow (hWnd);
  98.  
  99.   hWndMain  = hWnd;
  100.   hInstMain = hInstance;
  101.  
  102.   hWndChild1 = CreateWindow (szChildClass, "Child 1", WS_CHILD |
  103.                              WS_CAPTION | WS_SYSMENU |
  104.                              WS_THICKFRAME | WS_MAXIMIZEBOX |
  105.                              WS_CLIPSIBLINGS,
  106.                              xScreen / 9, yScreen / 8,
  107.                              xScreen / 3, yScreen / 2,
  108.                              hWndMain, 1, hInstance, NULL);
  109.  
  110.   ShowWindow (hWndChild1, SW_SHOWNORMAL);
  111.   UpdateWindow (hWndChild1);
  112.  
  113.   hWndChild2 = CreateWindow (szChildClass, "Child 2", WS_CHILD |
  114.                              WS_CAPTION | WS_SYSMENU |
  115.                              WS_THICKFRAME | WS_MAXIMIZEBOX |
  116.                              WS_CLIPSIBLINGS,
  117.                              5 * xScreen / 9, yScreen / 8,
  118.                              xScreen / 3, yScreen / 2,
  119.                              hWndMain, 1, hInstance, NULL);
  120.  
  121.   ShowWindow (hWndChild2, SW_SHOWNORMAL);
  122.   UpdateWindow (hWndChild2);
  123.  
  124.   lpProcEnterPoint    = MakeProcInstance (EnterPointDlgProc, hInstance);
  125.   lpProcNewEnterPoint = MakeProcInstance (NewEnterPointDlgProc, hInstance);
  126.  
  127.   while (GetMessage(&msg, NULL, 0, 0))
  128.     {
  129.     TranslateMessage(&msg);
  130.     DispatchMessage(&msg);
  131.     }
  132.   return (msg.wParam);
  133.   }
  134.  
  135. /****************************************************************************/
  136.  
  137. long    FAR PASCAL WndProc (hWnd, iMessage, wParam, lParam)
  138. HWND     hWnd;
  139. unsigned iMessage;
  140. WORD     wParam;
  141. LONG     lParam;
  142.   {
  143.   HMENU         hMenu;
  144.   POINT         pt;
  145.   static int    xClient, yClient;
  146.   char          szOutputBuffer [40];
  147.  
  148.   switch (iMessage)
  149.     {
  150.     case WM_CREATE:
  151.       hMenu = GetSystemMenu (hWnd, FALSE);
  152.       ChangeMenu (hMenu, NULL, "&About", IDM_ABOUT, MF_APPEND | MF_STRING);
  153.       break;
  154.  
  155.     case WM_SIZE:
  156.       xClient = LOWORD (lParam);
  157.       yClient = HIWORD (lParam);
  158.       sprintf (szOutputBuffer, "Width = %i, Height = %i", xClient, yClient);
  159.       MessageBox (hWnd, szOutputBuffer, "Client Window Dimensions", MB_OK);
  160.       break;
  161.  
  162.     case WM_LBUTTONDBLCLK:
  163.       break;
  164.  
  165.     case WM_SYSCOMMAND:
  166.       switch (wParam)
  167.         {
  168.         case IDM_ABOUT:
  169.           MessageBox (hWnd, szMessageBuffer, (LPSTR)"About", MB_OK);
  170.           break;
  171.         default:
  172.         return DefWindowProc (hWnd, iMessage, wParam, lParam);
  173.         }
  174.       break;
  175.  
  176.     case WM_COMMAND:
  177.       switch (wParam)
  178.         {
  179.         case IDM_CHOOSERANDOM:
  180.           pt.x = pt.y = -1;
  181.           while (((pt.x < 0) || (pt.x > xClient)) || ((pt.y < 0) ||
  182.                  (pt.y > yClient)))
  183.             {
  184.             pt.x = (rand () / 47);
  185.             pt.y = (rand () / 102);
  186.             }
  187.           hWnd = ChildWindowFromPoint (hWnd, pt);
  188.           sprintf (szOutputBuffer, "The Point [%i, %i]", pt.x, pt.y);
  189.           if (hWnd == hWndChild1)
  190.             MessageBox (hWnd, "is in Child Window 1", szOutputBuffer,
  191.                         MB_OK);
  192.           else
  193.             if (hWnd == hWndChild2)
  194.               MessageBox (hWnd, "is in Child Window 2", szOutputBuffer,
  195.                           MB_OK);
  196.             else
  197.               MessageBox (hWnd, "is not in a Child Window",
  198.                           szOutputBuffer, MB_OK);
  199.           break;
  200.  
  201.         case IDM_CHOOSE:
  202.           pt.x = pt.y = -1;
  203.           while (((pt.x < 0) || (pt.x > xClient)) || ((pt.y < 0) ||
  204.                  (pt.y > yClient)))
  205.             {
  206.             DialogBox (hInstMain, (LPSTR)"EnterPointDlg", hWnd,
  207.                        lpProcEnterPoint);
  208.             pt.x = atoi (szXValue);
  209.             pt.y = atoi (szYValue);
  210.             }
  211.           hWnd = ChildWindowFromPoint (hWnd, pt);
  212.           sprintf (szOutputBuffer, "The Point [%i, %i]", pt.x, pt.y);
  213.           if (hWnd == hWndChild1)
  214.             MessageBox (hWnd, "is in Child Window 1", szOutputBuffer,
  215.                         MB_OK);
  216.           else
  217.             if (hWnd == hWndChild2)
  218.               MessageBox (hWnd, "is in Child Window 2", szOutputBuffer,
  219.                           MB_OK);
  220.           else
  221.             MessageBox (hWnd, "is not in a Child Window",
  222.                         szOutputBuffer, MB_OK);
  223.           break;
  224.  
  225.         case IDM_HELP:
  226.           MessageBox (hWnd, szHelpBuffer, "Help Message", MB_OK);
  227.           break;
  228.         }
  229.       break;
  230.  
  231.     case WM_DESTROY:
  232.       PostQuitMessage(0);
  233.       break;
  234.  
  235.     default:
  236.       return DefWindowProc (hWnd, iMessage, wParam, lParam);
  237.     }
  238.   return (0L);
  239.   }
  240.  
  241. /****************************************************************************/
  242.  
  243. long    FAR PASCAL ChildWndProc (hWnd, iMessage, wParam, lParam)
  244. HWND     hWnd;
  245. unsigned iMessage;
  246. WORD     wParam;
  247. LONG     lParam;
  248.   {
  249.   return DefWindowProc (hWnd, iMessage, wParam, lParam);
  250.   }
  251.  
  252.  
  253. /****************************************************************************/
  254.  
  255. BOOL FAR PASCAL EnterPointDlgProc (hDlg, iMessage, wParam, lParam)
  256. HWND     hDlg;
  257. unsigned iMessage;
  258. WORD     wParam;
  259. LONG     lParam;
  260.   {
  261.   switch (iMessage) 
  262.     {
  263.     case WM_INITDIALOG:
  264.       SendDlgItemMessage (hDlg, IDD_X, EM_LIMITTEXT, (WORD)40, 0L);
  265.       SendDlgItemMessage (hDlg, IDD_Y, EM_LIMITTEXT, (WORD)40, 0L);
  266.  
  267.       hX = GetDlgItem (hDlg, IDD_X);
  268.       lpProcOldX = (FARPROC) GetWindowLong (hX, GWL_WNDPROC);
  269.       SetWindowLong (hX, GWL_WNDPROC, (LONG) lpProcNewEnterPoint);
  270.       SendMessage (hX, EM_SETSEL, 0, MAKELONG (0, 32767));
  271.       hY = GetDlgItem (hDlg, IDD_Y);
  272.       lpProcOldY = (FARPROC) GetWindowLong (hY, GWL_WNDPROC);
  273.       SetWindowLong (hY, GWL_WNDPROC, (LONG) lpProcNewEnterPoint);
  274.       SendMessage (hY, EM_SETSEL, 0, MAKELONG (0, 32767));
  275.       hOK2 = GetDlgItem (hDlg, IDOK);
  276.  
  277.       return TRUE;
  278.       break;
  279.  
  280.     case WM_COMMAND:
  281.       switch (wParam)
  282.         {
  283.         case IDD_X:
  284.           break;
  285.  
  286.         case IDD_Y:
  287.           break;
  288.  
  289.         case IDOK:
  290.           GetDlgItemText (hDlg, IDD_X, szXValue, 10);
  291.           GetDlgItemText (hDlg, IDD_Y, szYValue, 10);
  292.           EndDialog (hDlg, TRUE);
  293.           break;
  294.  
  295.         default:
  296.           return FALSE;
  297.         }
  298.     default:
  299.       return FALSE;
  300.     }
  301.   return TRUE;
  302.   }
  303.  
  304.  
  305. /****************************************************************************/
  306.  
  307. BOOL FAR PASCAL NewEnterPointDlgProc  (hWnd, iMessage, wParam, lParam)
  308. HWND     hWnd;
  309. unsigned iMessage;
  310. WORD     wParam;
  311. LONG     lParam;
  312.   {
  313.   switch (iMessage) 
  314.     {
  315.     case WM_GETDLGCODE:
  316.       return (DLGC_WANTALLKEYS);
  317.  
  318.     case WM_CHAR:
  319.       if ((wParam == VK_TAB) || (wParam == VK_RETURN))
  320.         {
  321.         SendMessage (hWndMain, WM_USER, 0, 0L);
  322.         SetFocus (FindNextWindow (hWnd));
  323.         return TRUE;
  324.         }
  325.       else
  326.         {
  327.         if (hWnd == hX)
  328.           return ((BOOL)CallWindowProc (lpProcOldX, hWnd,
  329.                   iMessage, wParam, lParam));
  330.         if (hWnd == hY)
  331.           return ((BOOL)CallWindowProc (lpProcOldY, hWnd,
  332.                   iMessage, wParam, lParam));
  333.         }
  334.       break;
  335.  
  336.     default:
  337.       if (hWnd == hX)
  338.         return ((BOOL)CallWindowProc (lpProcOldX, hWnd,
  339.                 iMessage, wParam, lParam));
  340.       if (hWnd == hY)
  341.         return ((BOOL)CallWindowProc (lpProcOldY, hWnd,
  342.                 iMessage, wParam, lParam));
  343.     }
  344.   }
  345.  
  346.  
  347. /****************************************************************************/
  348.  
  349. HWND FindNextWindow (hWnd)
  350. HWND   hWnd;
  351.   {
  352.   if (hWnd == hX)      
  353.     return hY;
  354.   if (hWnd == hY)      
  355.     return hOK2;
  356.   return NULL;
  357.   }
  358.