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

  1. /*
  2.  *  Function Name:   EnumChildWindows
  3.  *  Program Name:    enchwin.c
  4.  *
  5.  *  Description:
  6.  *   This program will enumerate the child window(s) for the parent
  7.  *   window.  The Class name of the child window will be displayed
  8.  *   in a message box.  This program has only one child window.
  9.  *
  10.  */
  11.  
  12. #include "windows.h"
  13. #include "string.h"
  14. #include "enchwin.h"
  15.  
  16. long FAR PASCAL WndProc(HWND, unsigned, WORD, LONG);
  17. long FAR PASCAL ChildProc(HWND, unsigned, WORD, LONG);
  18.  
  19. char   szAppName [] = "EnChWin" ;
  20. HWND   hWndChild1, hWndChild2;           /*    child window handle   */
  21. HANDLE hInst;
  22.  
  23. /************************************************************************/
  24.  
  25. BOOL FAR PASCAL EnumProc(hChildWindow, lParam)
  26. HWND   hChildWindow;
  27. LONG   lParam;
  28. {
  29.    char szstr[31];
  30.  
  31.    GetWindowText (hChildWindow, szstr, 30);
  32.    MessageBox(hChildWindow, (LPSTR)szstr,
  33.          (LPSTR)"Name of child window", MB_OK);
  34.    return TRUE;
  35. }
  36.  
  37. /***********************************************************************/
  38.  
  39. void CALL_EnumChildWindows(hWnd, hDC)
  40. HWND hWnd;
  41. HDC hDC;
  42. {
  43.   FARPROC lpprocEnumChWin;
  44.   char    szstring[80];
  45.   LONG    lParam=0;
  46.  
  47.     lpprocEnumChWin = MakeProcInstance ((FARPROC) EnumProc, hInst);
  48.     EnumChildWindows (hWnd, lpprocEnumChWin, lParam);
  49.     FreeProcInstance ((FARPROC) lpprocEnumChWin);
  50.  
  51.   return;
  52. }
  53.  
  54. /**************************************************************************/
  55.  
  56. /* Procedure called when the application is loaded for the first time */
  57. BOOL WinInit( hInstance )
  58. HANDLE hInstance;
  59. {
  60.     WNDCLASS   wcClass;
  61.  
  62.     wcClass.style          = CS_HREDRAW | CS_VREDRAW;
  63.     wcClass.lpfnWndProc    = WndProc;
  64.     wcClass.cbClsExtra     = 0;
  65.     wcClass.cbWndExtra     = 0;
  66.     wcClass.hInstance      = hInstance;
  67.     wcClass.hIcon          = LoadIcon( hInstance,NULL );
  68.     wcClass.hCursor        = LoadCursor( NULL, IDC_ARROW );
  69.     wcClass.hbrBackground  = (HBRUSH)GetStockObject( WHITE_BRUSH );
  70.     wcClass.lpszMenuName   = (LPSTR)szAppName;
  71.     wcClass.lpszClassName  = (LPSTR)"EnumChildWindows";
  72.  
  73.     if (!RegisterClass( (LPWNDCLASS)&wcClass ) )
  74.         /* Initialization failed.
  75.          * Windows will automatically deallocate all allocated memory.
  76.          */
  77.         return FALSE;
  78.  
  79.     wcClass.style          = CS_HREDRAW | CS_VREDRAW;
  80.     wcClass.lpfnWndProc    = ChildProc;
  81.     wcClass.cbClsExtra     = 0;
  82.     wcClass.cbWndExtra     = 0;
  83.     wcClass.hInstance      = hInstance;
  84.     wcClass.hIcon          = LoadIcon( hInstance,NULL );
  85.     wcClass.hCursor        = LoadCursor( NULL, IDC_ARROW );
  86.     wcClass.hbrBackground  = (HBRUSH)GetStockObject( WHITE_BRUSH );
  87.     wcClass.lpszMenuName   = (LPSTR)NULL;
  88.     wcClass.lpszClassName  = (LPSTR)"Child";
  89.  
  90.     if (!RegisterClass( (LPWNDCLASS)&wcClass ) )
  91.         return FALSE;
  92.  
  93.     return TRUE;        /* Initialization succeeded */
  94. }
  95.  
  96.  
  97. int PASCAL WinMain( hInstance, hPrevInstance, lpszCmdLine, cmdShow )
  98. HANDLE hInstance, hPrevInstance;
  99. LPSTR lpszCmdLine;
  100. int cmdShow;
  101. {
  102.     MSG   msg;
  103.     HWND  hWnd;
  104.  
  105.     if (!hPrevInstance)
  106.         {
  107.         /* Call initialization procedure if this is the first instance */
  108.         if (!WinInit( hInstance ))
  109.             return FALSE;
  110.         }
  111.  
  112.     hWnd = CreateWindow((LPSTR)"EnumChildWindows",
  113.                         (LPSTR)"EnumChildWindows()",
  114.                         WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
  115.                         CW_USEDEFAULT,
  116.                         CW_USEDEFAULT,
  117.                         CW_USEDEFAULT,
  118.                         CW_USEDEFAULT,
  119.                         (HWND)NULL,        /* no parent */
  120.                         (HMENU)NULL,       /* use class menu */
  121.                         (HANDLE)hInstance, /* handle to window instance */
  122.                         (LPSTR)NULL        /* no params to pass on */
  123.                         );
  124.  
  125.     hWndChild1 = CreateWindow((LPSTR)"Child",
  126.             (LPSTR)"Child Window 1",
  127.                         WS_CHILD | WS_VISIBLE | WS_CAPTION,
  128.                         50,
  129.             20,
  130.                         120,
  131.             50,
  132.                         (HWND)hWnd,        /* specify hWnd as parent */
  133.                         (HMENU)NULL,       /* use class menu */
  134.             (HANDLE)hInst, /* handle to window instance */
  135.                         (LPSTR)NULL        /* no params to pass on */
  136.                         );
  137.  
  138.      hInst = hInstance;    /*  save Instance             */
  139.  
  140.      hWndChild2 = CreateWindow((LPSTR)"Child",
  141.             (LPSTR)"Child Window 2",
  142.                         WS_CHILD | WS_VISIBLE | WS_CAPTION,
  143.                         50,
  144.             70,
  145.             120,
  146.             50,
  147.                         (HWND)hWnd,        /* specify hWnd as parent */
  148.                         (HMENU)NULL,       /* use class menu */
  149.             (HANDLE)hInst, /* handle to window instance */
  150.                         (LPSTR)NULL        /* no params to pass on */
  151.                         );
  152.  
  153.      hInst = hInstance;    /*  save Instance             */
  154.  
  155.  
  156.     /* Make window visible according to the way the app is activated */
  157.     ShowWindow( hWnd, cmdShow );
  158.     UpdateWindow( hWnd );
  159.  
  160.     /* Polling messages from event queue */
  161.     while (GetMessage((LPMSG)&msg, NULL, 0, 0))
  162.         {
  163.         TranslateMessage((LPMSG)&msg);
  164.         DispatchMessage((LPMSG)&msg);
  165.         }
  166.  
  167.     return (int)msg.wParam;
  168. }
  169.  
  170. /* Procedures which make up the window class. */
  171. long FAR PASCAL WndProc( hWnd, message, wParam, lParam )
  172. HWND hWnd;
  173. unsigned message;
  174. WORD wParam;
  175. LONG lParam;
  176. {
  177.     PAINTSTRUCT ps;
  178.  
  179.     switch (message)
  180.     {
  181.     case WM_COMMAND:
  182.     switch (wParam)
  183.     {
  184.     case IDM_ENUM:
  185.          ps.hdc = GetDC (hWnd);
  186.          CALL_EnumChildWindows(hWnd, ps.hdc);
  187.          ReleaseDC (hWnd, ps.hdc);
  188.          break ;
  189.     default:
  190.          break ;
  191.     }
  192.     break ;
  193.  
  194.     case WM_DESTROY:
  195.         PostQuitMessage( 0 );
  196.         break;
  197.  
  198.     default:
  199.         return DefWindowProc( hWnd, message, wParam, lParam );
  200.         break;
  201.     }
  202.     return(0L);
  203. }
  204.  
  205.  
  206. long FAR PASCAL ChildProc( hChildWnd, message, wParam, lParam )
  207. HWND hChildWnd;
  208. unsigned message;
  209. WORD wParam;
  210. LONG lParam;
  211. {
  212.     PAINTSTRUCT ps;
  213.     switch (message)
  214.     {
  215.  
  216.     default:
  217.         return DefWindowProc( hChildWnd, message, wParam, lParam );
  218.         break;
  219.     }
  220.     return(0L);
  221. }
  222.