home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / WTREE2.ZIP / WINTREE.C next >
Text File  |  1991-11-10  |  5KB  |  164 lines

  1. /* ----------------------------------------------------------------
  2.    WINLIST -- List the Titles of all the active windows
  3.    ----------------------------------------------------------------*/
  4.  
  5. #define INCL_WIN
  6. #define INCL_GPI
  7. #include <os2.h>
  8. #include <stdlib.h>
  9. #include <malloc.h>
  10. #include <stdio.h>
  11. #include <string.h>
  12.  
  13. #define IDL_LIST    100
  14.  
  15. HAB hab;         /* Handle to an anchor block */
  16.  
  17. MRESULT EXPENTRY ClientWndProc(HWND, USHORT, MPARAM, MPARAM);
  18. extern VOID TraverseWindows(HWND hwnd, HWND hwndList, SHORT sLevel);
  19.  
  20. int main(void)
  21.    {
  22.    static CHAR   szClientClass[] = "Winlist";
  23.    static ULONG  flFrameFlags = FCF_TITLEBAR      |
  24.                                 FCF_SYSMENU       |
  25.                                 FCF_SIZEBORDER    |
  26.                                 FCF_MINMAX        |
  27.                                 FCF_SHELLPOSITION |
  28.                                 FCF_TASKLIST      ;
  29.  
  30.    HMQ           hmq;         /* Handle to a message queue */
  31.    HWND          hwndFrame,   /* Handle to the fram window */
  32.                  hwndClient;  /* Handle to the client window */
  33.    QMSG          qmsg;        /* A queued message */
  34.  
  35.    hab = WinInitialize(0);
  36.    hmq = WinCreateMsgQueue(hab,0);
  37.  
  38.    WinRegisterClass(hab,
  39.                     szClientClass,
  40.                     ClientWndProc,
  41.                     CS_SIZEREDRAW,
  42.                     0);
  43.  
  44.    hwndFrame = WinCreateStdWindow(HWND_DESKTOP,
  45.                                   WS_VISIBLE,
  46.                                   &flFrameFlags,
  47.                                   szClientClass,
  48.                                   NULL,
  49.                                   0L,
  50.                                   NULL,
  51.                                   0,
  52.                                   &hwndClient);
  53.  
  54.    WinSendMsg(hwndFrame, WM_SETICON, WinQuerySysPointer(HWND_DESKTOP, SPTR_APPICON, FALSE), NULL);
  55.  
  56.    while(WinGetMsg(hab, &qmsg, NULL, 0, 0))
  57.       WinDispatchMsg(hab,&qmsg);
  58.  
  59.    WinDestroyWindow(hwndFrame);
  60.    WinDestroyMsgQueue(hmq);
  61.    WinTerminate(hab);
  62.    return 0;
  63.    } /* Main */
  64.  
  65.  
  66.  
  67. MRESULT EXPENTRY ClientWndProc(HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  68.    {
  69.    switch(msg)
  70.       {
  71.       case WM_CREATE:
  72.          WinCreateWindow(hwnd, WC_LISTBOX, "Window List",
  73.                          WS_VISIBLE | LS_NOADJUSTPOS | LS_HORZSCROLL,
  74.                          0, 0, 0, 0,
  75.                          hwnd, HWND_TOP, IDL_LIST, NULL, NULL);
  76.  
  77.          WinSetPresParam(hwnd, PP_FONTNAMESIZE, 1+_fstrlen("8.Courier"), "8.Courier");
  78.  
  79.          /* Traverse the desktop windows */
  80.  
  81.          TraverseWindows(HWND_DESKTOP, WinWindowFromID(hwnd, IDL_LIST), 0);
  82.          return 0;
  83.  
  84.       case WM_SIZE:
  85.          {
  86.          RECTL rclWin;   /* Size of the window */
  87.  
  88.          WinQueryWindowRect(hwnd, &rclWin);
  89.  
  90.          /* Make the MLE fill the entire window */
  91.  
  92.          WinSetWindowPos(WinWindowFromID(hwnd, IDL_LIST),
  93.             HWND_TOP, 0, 0, rclWin.xRight, rclWin.yTop, SWP_SIZE | SWP_MOVE);
  94.          return 0;
  95.          }
  96.  
  97.       case WM_PAINT:
  98.          {
  99.          HPS   hps;
  100.          RECTL rclInvalid;
  101.  
  102.          hps = WinBeginPaint(hwnd, NULL, &rclInvalid);
  103.          GpiErase(hps);
  104.          WinEndPaint(hps);
  105.          return 0;
  106.          } /* case block */
  107.       }
  108.    return WinDefWindowProc(hwnd, msg, mp1, mp2);
  109.    } /* ClientWndProc */
  110.  
  111.  
  112. VOID TraverseWindows(HWND hwnd, HWND hwndList, SHORT sLevel)
  113.    {
  114.    HWND   hwndNext;     /* Handle to a window frame      */
  115.    HENUM  hEnum;        /* Handle to an enumeration list */
  116.    CHAR   szLine[512];  /* Composed line for the listbox */
  117.    CHAR   szTitle[80];  /* Window title                  */
  118.    CHAR   szClass[80];  /* Window class                  */
  119.    CHAR   szLeader[90]; /* Leading blanks                */
  120.    USHORT i;            /* Loop index                    */
  121.  
  122.    /* Enumerate the Windows on the desktop */
  123.  
  124.    hEnum = WinBeginEnumWindows(hwnd);
  125.    while(NULL != (hwndNext = WinGetNextWindow(hEnum)))
  126.       {
  127.  
  128.       /* Unlock the Window */
  129.  
  130.       WinLockWindow(hwndNext,FALSE);
  131.  
  132.       /* Build the proper indentation string */
  133.  
  134.       for(i = 0; (i < 89) & (i < sLevel); i++)
  135.          szLeader[i] = ' ';
  136.       szLeader[i] = '\0';
  137.  
  138.       /* Get the window's title */
  139.  
  140.       WinQueryWindowText(hwndNext, 80, szTitle);
  141.       if (!strlen(szTitle))
  142.          _fstrcpy(szTitle,"[No Title]");
  143.  
  144.       /* Get the class name */
  145.  
  146.       WinQueryClassName(hwndNext, 80, szClass);
  147.  
  148.       /* Append the handle to the window */
  149.  
  150.       sprintf(szLine,"%s%p...%s...%s",szLeader, hwndNext, szTitle, szClass);
  151.       WinSendMsg(hwndList, LM_INSERTITEM, MPFROMSHORT(LIT_END), MPFROMP(szLine));
  152.  
  153.  
  154.       /* Go find the children */
  155.  
  156.       sLevel += 3;
  157.       TraverseWindows(hwndNext, hwndList, sLevel);
  158.       sLevel -= 3;
  159.       } /* while */
  160.  
  161.    WinEndEnumWindows(hEnum);
  162.    return;
  163.    } /* TraverseWindows */
  164.