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

  1. /*
  2.  *  Function Name:   EnumWindows
  3.  *  Program Name:    enwin.c
  4.  *  SDK Version:         2.03
  5.  *  Runtime Version:     2.03
  6.  *  Microsoft C Version: 5.1
  7.  *
  8.  *  Description:
  9.  *   This function will enumerate all the windows on the screen.  The
  10.  *   name of each window enumerated will be displayed.
  11.  */
  12.  
  13. #include "windows.h"
  14. #include "string.h"
  15.  
  16. long FAR PASCAL WndProc(HWND, unsigned, WORD, LONG);
  17.  
  18. HANDLE hInst;
  19. HWND   hWndGlobal;      /*  for TextOut during enumeration  */
  20.  
  21. /*************************************************************************/
  22.  
  23. BOOL FAR PASCAL EnumProc(hWnd, lParam)
  24. HWND   hWnd;
  25. LONG   lParam;
  26. {
  27.   HDC  hDC;
  28.   char szstr[80];
  29.   static int inc = 10;
  30.  
  31.   GetClassName(hWnd, (LPSTR)szstr, 80);
  32.   hDC = GetDC(hWndGlobal);
  33.   TextOut(hDC, 10, inc+=15, (LPSTR)szstr, strlen(szstr));
  34.   ReleaseDC(hWndGlobal, hDC);
  35.   return (1);                    /* returning 0 will stop the enumeration */
  36. }
  37.  
  38. /***********************************************************************/
  39.  
  40. void CALL_EnumWindows(hWnd, hDC)
  41. HWND hWnd;
  42. HDC hDC;
  43. {
  44.   FARPROC lpprocEnumWindow;
  45.   LONG lParam;
  46.  
  47.     lpprocEnumWindow = MakeProcInstance ((FARPROC) EnumProc, hInst);
  48.     EnumWindows (lpprocEnumWindow, lParam);   /*  function demonstrated   */
  49.     FreeProcInstance ((FARPROC) lpprocEnumWindow);
  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)NULL;
  71.     wcClass.lpszClassName  = (LPSTR)"EnumWindows";
  72.  
  73.     if (!RegisterClass( (LPWNDCLASS)&wcClass ) )
  74.         /* Initialization failed.
  75.          * Windows will automatically deallocate all allocated memory.
  76.          */
  77.         return FALSE;
  78.  
  79.     return TRUE;        /* Initialization succeeded */
  80. }
  81.  
  82.  
  83. int PASCAL WinMain( hInstance, hPrevInstance, lpszCmdLine, cmdShow )
  84. HANDLE hInstance, hPrevInstance;
  85. LPSTR lpszCmdLine;
  86. int cmdShow;
  87. {
  88.     MSG   msg;
  89.     HWND  hWnd;
  90.  
  91.     if (!hPrevInstance)
  92.         {
  93.         /* Call initialization procedure if this is the first instance */
  94.         if (!WinInit( hInstance ))
  95.             return FALSE;
  96.         }
  97.  
  98.     hWnd = CreateWindow((LPSTR)"EnumWindows",
  99.                         (LPSTR)"EnumWindows()",
  100.                         WS_OVERLAPPEDWINDOW,
  101.                         CW_USEDEFAULT,
  102.                         CW_USEDEFAULT,
  103.                         CW_USEDEFAULT,
  104.                         CW_USEDEFAULT,
  105.                         (HWND)NULL,        /* no parent */
  106.                         (HMENU)NULL,       /* use class menu */
  107.                         (HANDLE)hInstance, /* handle to window instance */
  108.                         (LPSTR)NULL        /* no params to pass on */
  109.                         );
  110.     hInst = hInstance;
  111.     hWndGlobal = hWnd;
  112.  
  113.     /* Make window visible according to the way the app is activated */
  114.     ShowWindow( hWnd, cmdShow );
  115.     UpdateWindow( hWnd );
  116.  
  117.     /* Polling messages from event queue */
  118.     while (GetMessage((LPMSG)&msg, NULL, 0, 0))
  119.         {
  120.         TranslateMessage((LPMSG)&msg);
  121.         DispatchMessage((LPMSG)&msg);
  122.         }
  123.  
  124.     return (int)msg.wParam;
  125. }
  126.  
  127. /* Procedures which make up the window class. */
  128. long FAR PASCAL WndProc( hWnd, message, wParam, lParam )
  129. HWND hWnd;
  130. unsigned message;
  131. WORD wParam;
  132. LONG lParam;
  133. {
  134.     PAINTSTRUCT ps;
  135.  
  136.     switch (message)
  137.     {
  138.  
  139.     case WM_PAINT:
  140.         BeginPaint( hWnd, (LPPAINTSTRUCT)&ps );
  141.         CALL_EnumWindows(hWnd, ps.hdc);
  142.         EndPaint( hWnd, (LPPAINTSTRUCT)&ps );
  143.         break;
  144.  
  145.     case WM_DESTROY:
  146.         PostQuitMessage( 0 );
  147.         break;
  148.  
  149.     default:
  150.         return DefWindowProc( hWnd, message, wParam, lParam );
  151.         break;
  152.     }
  153.     return(0L);
  154. }
  155.