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

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