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

  1. /*
  2.  *
  3.  *   gnextwin.c
  4.  *
  5.  *   This program demonstrates the use of the GetNextWindow() function.
  6.  *   GetNextWindow() searches for a handle that identifies the next (or
  7.  *   previous window, depending on the second parameter) in the window-
  8.  *   manager's list. GetNextWindow() is called from WinMain() in this
  9.  *   application, and if a non-null handle is returned, CloseWindow() is
  10.  *   is called to make the window whose handle was returned iconic. If
  11.  *   this application's window is not the active window, but no other
  12.  *   window is over it, then that may mean the previous window is the
  13.  *   blank window that you see in the background.
  14.  *
  15.  */
  16.  
  17. #include "windows.h"
  18.  
  19. long FAR PASCAL HelloWndProc(HWND, unsigned, WORD, LONG);
  20.  
  21. /* Procedure called when the application is loaded for the first time */
  22. BOOL HelloInit( hInstance )
  23. HANDLE hInstance;
  24. {
  25.     PWNDCLASS   pHelloClass;
  26.  
  27.     pHelloClass = (PWNDCLASS)LocalAlloc( LPTR, sizeof(WNDCLASS) );
  28.  
  29.     pHelloClass->hCursor        = LoadCursor( NULL, IDC_ARROW );
  30.     pHelloClass->hIcon          = LoadIcon( hInstance,NULL);
  31.     pHelloClass->lpszMenuName   = (LPSTR)NULL;
  32.     pHelloClass->lpszClassName  = (LPSTR)"GetNextWindow";
  33.     pHelloClass->hbrBackground  = (HBRUSH)GetStockObject( WHITE_BRUSH );
  34.     pHelloClass->hInstance      = hInstance;
  35.     pHelloClass->style          = CS_HREDRAW | CS_VREDRAW;
  36.     pHelloClass->lpfnWndProc    = HelloWndProc;
  37.  
  38.     if (!RegisterClass( (LPWNDCLASS)pHelloClass ) )
  39.         /* Initialization failed.
  40.          * Windows will automatically deallocate all allocated memory.
  41.          */
  42.         return FALSE;
  43.  
  44.     LocalFree( (HANDLE)pHelloClass );
  45.     return TRUE;        /* Initialization succeeded */
  46. }
  47.  
  48. int PASCAL WinMain( hInstance, hPrevInstance, lpszCmdLine, cmdShow )
  49. HANDLE hInstance, hPrevInstance;
  50. LPSTR lpszCmdLine;
  51. int cmdShow;
  52. {
  53.     MSG   msg;
  54.     HWND  hWnd;
  55.     HMENU hMenu;
  56.     HWND  hNextWindow;   /*** return value from GetNextWindow() ***/
  57.  
  58.     HelloInit( hInstance );
  59.     hWnd = CreateWindow((LPSTR)"GetNextWindow",
  60.                         (LPSTR)"GetNextWindow()",
  61.                         WS_OVERLAPPEDWINDOW,
  62.                         CW_USEDEFAULT,
  63.                         CW_USEDEFAULT,
  64.                         CW_USEDEFAULT,
  65.                         CW_USEDEFAULT,
  66.                         (HWND)NULL,        /* no parent */
  67.                         (HMENU)NULL,       /* use class menu */
  68.                         (HANDLE)hInstance, /* handle to window instance */
  69.                         (LPSTR)NULL        /* no params to pass on */
  70.                         );
  71.  
  72.     /* Make window visible according to the way the app is activated */
  73.     ShowWindow( hWnd, cmdShow );
  74.     UpdateWindow( hWnd );
  75.  
  76.     MessageBox(NULL,
  77.                (LPSTR)"     If a non-null handle is returned, the window\n\
  78. corresponding to this handle is brought to the\n\
  79. top.",
  80.                (LPSTR)"This application calls GetNextWindow().",
  81.                MB_OK);
  82.  
  83. /***   get the handle to the next window in window-manager's list  ***/
  84.  
  85.     hNextWindow = GetNextWindow(hWnd,GW_HWNDNEXT);
  86.  
  87.     if (!hNextWindow)
  88.         MessageBox(NULL,
  89.                    (LPSTR)"There is no next window",
  90.                    (LPSTR)"GetNextWindow() info:",
  91.                    MB_OK);
  92.     else if (hNextWindow == hWnd)
  93.         MessageBox(NULL,
  94.                    (LPSTR)"which is already the top window",
  95.                    (LPSTR)"GetNextWindow returned this window...",
  96.                    MB_OK);
  97.     else if ( !IsIconic(hNextWindow) )
  98.         BringWindowToTop(hNextWindow);
  99.     else
  100.         ShowWindow(hNextWindow,SW_SHOWMAXIMIZED);
  101.  
  102.     /* Polling messages from event queue */
  103.     while (GetMessage((LPMSG)&msg, NULL, 0, 0)) {
  104.         TranslateMessage((LPMSG)&msg);
  105.         DispatchMessage((LPMSG)&msg);
  106.         }
  107.  
  108.     return (int)msg.wParam;
  109. }
  110.  
  111. /* Procedures which make up the window class. */
  112. long FAR PASCAL HelloWndProc( hWnd, message, wParam, lParam )
  113. HWND hWnd;
  114. unsigned message;
  115. WORD wParam;
  116. LONG lParam;
  117. {
  118.     PAINTSTRUCT ps;
  119.  
  120.     switch (message)
  121.     {
  122.  
  123.     case WM_DESTROY:
  124.         PostQuitMessage( 0 );
  125.         break;
  126.  
  127.     case WM_PAINT:
  128.         BeginPaint( hWnd, (LPPAINTSTRUCT)&ps );
  129.         EndPaint( hWnd, (LPPAINTSTRUCT)&ps );
  130.         break;
  131.  
  132.     default:
  133.         return DefWindowProc( hWnd, message, wParam, lParam );
  134.         break;
  135.     }
  136.     return(0L);
  137. }
  138. 
  139.