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

  1. /*
  2.  *
  3.  *   This program demonstrates use of the GetParent() function.
  4.  *   GetParent() returns the parent (if one exists) of the given window.
  5.  *   GetParent() is called three times in WinMain() in this application.
  6.  *
  7.  *   Windows Version 2.0 function demonstration application
  8.  *
  9.  */
  10.  
  11. #include "windows.h"
  12.  
  13. long FAR PASCAL HelloWndProc(HWND, unsigned, WORD, LONG);
  14. long FAR PASCAL ChildAProc(HWND, unsigned, WORD, LONG);
  15. long FAR PASCAL ChildBProc(HWND, unsigned, WORD, LONG);
  16.  
  17. HWND hChildAWnd = NULL;     /*    handle to child window         */
  18. HWND hChildBWnd = NULL;     /*    handle to grandchild window  */
  19. HWND hMainWnd = NULL;       /*   handle to main window        */
  20.  
  21. /* Procedure called when the application is loaded for the first time */
  22.  
  23. BOOL HelloInit( hInstance )
  24. HANDLE hInstance;
  25. {
  26.     PWNDCLASS   pHelloClass;
  27.  
  28.     pHelloClass = (PWNDCLASS)LocalAlloc( LPTR, sizeof(WNDCLASS) );
  29.  
  30.     pHelloClass->hCursor        = LoadCursor( NULL, IDC_ARROW );
  31.     pHelloClass->hIcon        = LoadIcon( hInstance,NULL);
  32.     pHelloClass->lpszMenuName   = (LPSTR)NULL;
  33.     pHelloClass->lpszClassName    = (LPSTR)"GetParent";
  34.     pHelloClass->hbrBackground  = (HBRUSH)GetStockObject( WHITE_BRUSH );
  35.     pHelloClass->hInstance      = hInstance;
  36.     pHelloClass->style          = CS_HREDRAW | CS_VREDRAW;
  37.     pHelloClass->lpfnWndProc    = HelloWndProc;
  38.  
  39.     if (!RegisterClass( (LPWNDCLASS)pHelloClass ) )
  40.         /* Initialization failed.
  41.          * Windows will automatically deallocate all allocated memory.
  42.          */
  43.         return FALSE;
  44.  
  45.     pHelloClass->hCursor        = LoadCursor( NULL, IDC_ARROW );
  46.     pHelloClass->hIcon        = LoadIcon( hInstance,NULL);
  47.     pHelloClass->lpszMenuName   = (LPSTR)NULL;
  48.     pHelloClass->lpszClassName    = (LPSTR)"CHILD A";
  49.     pHelloClass->hbrBackground  = (HBRUSH)GetStockObject( WHITE_BRUSH );
  50.     pHelloClass->hInstance      = hInstance;
  51.     pHelloClass->style          = CS_HREDRAW | CS_VREDRAW;
  52.     pHelloClass->lpfnWndProc    = ChildAProc;
  53.  
  54.     if (!RegisterClass( (LPWNDCLASS)pHelloClass ) )
  55.         /* Initialization failed.
  56.          * Windows will automatically deallocate all allocated memory.
  57.          */
  58.         return FALSE;
  59.  
  60.     pHelloClass->hCursor        = LoadCursor( NULL, IDC_ARROW );
  61.     pHelloClass->hIcon        = LoadIcon( hInstance,NULL);
  62.     pHelloClass->lpszMenuName   = (LPSTR)NULL;
  63.     pHelloClass->lpszClassName    = (LPSTR)"CHILD B";
  64.     pHelloClass->hbrBackground  = (HBRUSH)GetStockObject( WHITE_BRUSH );
  65.     pHelloClass->hInstance      = hInstance;
  66.     pHelloClass->style          = CS_HREDRAW | CS_VREDRAW;
  67.     pHelloClass->lpfnWndProc    = ChildBProc;
  68.  
  69.     if (!RegisterClass( (LPWNDCLASS)pHelloClass ) )
  70.         /* Initialization failed.
  71.          * Windows will automatically deallocate all allocated memory.
  72.          */
  73.         return FALSE;
  74.  
  75.     LocalFree( (HANDLE)pHelloClass );
  76.     return TRUE;        /* Initialization succeeded */
  77. }
  78.  
  79. int PASCAL WinMain( hInstance, hPrevInstance, lpszCmdLine, cmdShow )
  80. HANDLE hInstance, hPrevInstance;
  81. LPSTR lpszCmdLine;
  82. int cmdShow;
  83. {
  84.     MSG   msg;
  85.     HWND  hWnd;
  86.     HMENU hMenu;
  87.  
  88.     HWND  hParent;  /* return value from GetParent() */
  89.  
  90.     HelloInit( hInstance );
  91.  
  92.     hMainWnd = CreateWindow((LPSTR)"GetParent",
  93.             (LPSTR)"GetParent() [hMainWnd]",
  94.             WS_OVERLAPPEDWINDOW,
  95.             CW_USEDEFAULT,
  96.             CW_USEDEFAULT,
  97.             CW_USEDEFAULT,
  98.             CW_USEDEFAULT,
  99.                         (HWND)NULL,        /* no parent */
  100.                         (HMENU)NULL,       /* use class menu */
  101.                         (HANDLE)hInstance, /* handle to window instance */
  102.                         (LPSTR)NULL        /* no params to pass on */
  103.                         );
  104.  
  105.     /* Make window visible according to the way the app is activated */
  106.     ShowWindow( hMainWnd, cmdShow );
  107.     UpdateWindow( hMainWnd );
  108.  
  109.     hChildAWnd = CreateWindow((LPSTR)"CHILD A",
  110.             (LPSTR)"Child A [hChildAWnd]",
  111.             WS_CHILD | WS_SIZEBOX | WS_VISIBLE | WS_CAPTION |
  112.             WS_CLIPSIBLINGS,
  113.             5,
  114.             5,
  115.             400,
  116.             300,
  117.             (HWND)hMainWnd,
  118.             (HMENU)1,       /* child: use constant */
  119.                         (HANDLE)hInstance, /* handle to window instance */
  120.                         (LPSTR)NULL        /* no params to pass on */
  121.                         );
  122.  
  123.     hChildBWnd = CreateWindow((LPSTR)"CHILD B",
  124.             (LPSTR)"Child B [hChildBWnd]",
  125.             WS_CHILD | WS_SIZEBOX | WS_VISIBLE | WS_CAPTION |
  126.             WS_CLIPSIBLINGS,
  127.             5,
  128.             5,
  129.             300,
  130.             250,
  131.             (HWND)hChildAWnd,
  132.             (HMENU)2,       /* child: use constant */
  133.                         (HANDLE)hInstance, /* handle to window instance */
  134.                         (LPSTR)NULL        /* no params to pass on */
  135.                         );
  136.  
  137. /*
  138.  *     hWnd is the parent of hChildAWnd and hChildAWnd is the parent of
  139.  *     hChildBWnd. The following calls to GetParent() should confirm this
  140.  */
  141.  
  142. hParent = GetParent(hMainWnd);
  143. if (!hParent)
  144.     MessageBox(NULL,
  145.            (LPSTR)"because hMainWnd has no parent",
  146.            (LPSTR)"GetParent(hMainWnd) returned NULL, which is correct...",
  147.            MB_OK);
  148. else
  149.     MessageBox(NULL,
  150.            (LPSTR)"which is an ERROR",
  151.            (LPSTR)"GetParent(hWnd) did not return NULL...",
  152.            MB_OK);
  153.  
  154. hParent = GetParent(hChildAWnd);
  155. if (hParent == hMainWnd)
  156.     MessageBox(NULL,
  157.            (LPSTR)"because hMainWnd is hChildAWnd's parent",
  158.            (LPSTR)"GetParent(hChildAWnd) returned hMainWnd, which is correct...",
  159.            MB_OK);
  160. else
  161.     MessageBox(NULL,
  162.            (LPSTR)"which is an ERROR",
  163.            (LPSTR)"GetParent(hChildAWnd) did not return hMainWnd,",
  164.            MB_OK);
  165.  
  166. hParent = GetParent(hChildBWnd);
  167. if (hParent == hChildAWnd)
  168.     MessageBox(NULL,
  169.            (LPSTR)"because hChildAWnd is hChildBWnd's parent",
  170.            (LPSTR)"GetParent(hChildBWnd) returned hChildAWnd, which is correct...",
  171.            MB_OK);
  172. else
  173.     MessageBox(NULL,
  174.            (LPSTR)"which is an ERROR",
  175.            (LPSTR)"GetParent(hChildBWnd) did not return hChildAWnd",
  176.            MB_OK);
  177.  
  178.     /* Polling messages from event queue */
  179.     while (GetMessage((LPMSG)&msg, NULL, 0, 0)) {
  180.         TranslateMessage((LPMSG)&msg);
  181.         DispatchMessage((LPMSG)&msg);
  182.         }
  183.  
  184.     return (int)msg.wParam;
  185. }
  186.  
  187. /* Procedures which make up the window class. */
  188. long FAR PASCAL HelloWndProc( hWnd, message, wParam, lParam )
  189. HWND hWnd;
  190. unsigned message;
  191. WORD wParam;
  192. LONG lParam;
  193. {
  194.     PAINTSTRUCT ps;
  195.  
  196.     switch (message)
  197.     {
  198.     case WM_SYSCOMMAND:
  199.     return DefWindowProc( hWnd, message, wParam, lParam );
  200.     break;
  201.  
  202.     case WM_LBUTTONDOWN:
  203.     MessageBox(hChildBWnd,(LPSTR)"Left Button Click",
  204.                   (LPSTR)"PARENT WINDOW",
  205.                   MB_OK);
  206.     break;
  207.  
  208.     case WM_DESTROY:
  209.         PostQuitMessage( 0 );
  210.         break;
  211.  
  212.     case WM_PAINT:
  213.         BeginPaint( hWnd, (LPPAINTSTRUCT)&ps );
  214.         EndPaint( hWnd, (LPPAINTSTRUCT)&ps );
  215.         break;
  216.  
  217.     default:
  218.         return DefWindowProc( hWnd, message, wParam, lParam );
  219.         break;
  220.     }
  221.     return(0L);
  222. }
  223.  
  224. long FAR PASCAL ChildAProc( hChildAWnd, message, wParam, lParam )
  225. HWND hChildAWnd;
  226. unsigned message;
  227. WORD wParam;
  228. LONG lParam;
  229. {
  230.     PAINTSTRUCT ps;
  231.  
  232.     switch (message)
  233.     {
  234.  
  235.     case WM_LBUTTONDOWN:
  236.     MessageBox(hChildBWnd,(LPSTR)"Left Button Click",
  237.                   (LPSTR)"CHILD A",
  238.                   MB_OK);
  239.     break;
  240.  
  241.     case WM_DESTROY:
  242.         PostQuitMessage( 0 );
  243.         break;
  244.  
  245.     case WM_PAINT:
  246.     BeginPaint( hChildAWnd, (LPPAINTSTRUCT)&ps );
  247.     EndPaint( hChildAWnd, (LPPAINTSTRUCT)&ps );
  248.         break;
  249.  
  250.     default:
  251.     return DefWindowProc( hChildAWnd, message, wParam, lParam );
  252.         break;
  253.     }
  254.     return(0L);
  255. }
  256.  
  257. long FAR PASCAL ChildBProc( hChildBWnd, message, wParam, lParam )
  258. HWND hChildBWnd;
  259. unsigned message;
  260. WORD wParam;
  261. LONG lParam;
  262. {
  263.     PAINTSTRUCT ps;
  264.  
  265.     switch (message)
  266.     {
  267.  
  268.     case WM_LBUTTONDOWN:
  269.     MessageBox(hChildBWnd,(LPSTR)"Left Button Click",
  270.                   (LPSTR)"CHILD B",
  271.                   MB_OK);
  272.     break;
  273.  
  274.     case WM_DESTROY:
  275.         PostQuitMessage( 0 );
  276.         break;
  277.  
  278.     case WM_PAINT:
  279.     BeginPaint( hChildBWnd, (LPPAINTSTRUCT)&ps );
  280.     EndPaint( hChildBWnd, (LPPAINTSTRUCT)&ps );
  281.         break;
  282.  
  283.     default:
  284.     return DefWindowProc( hChildBWnd, message, wParam, lParam );
  285.         break;
  286.     }
  287.     return(0L);
  288. }
  289.