home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / SHUTDWN3.ZIP / SHUTMENU.C < prev    next >
C/C++ Source or Header  |  1991-01-26  |  7KB  |  217 lines

  1. /*
  2.     This version attempts to find the id of the Shutdown menuitem
  3.     dynamically.  The idea is to search the Switch List using
  4.     WinQuerySwitchList and search for the title "Desktop Manager";
  5.     I then assume that the hwnd member of the SWTCTL structure is
  6.     points to the Desktop Manager's frame window.  From this, I
  7.     try to get the handle of it's action bar by using WinWindowFromID.
  8.     Then I want to search the text of the action bar items to find
  9.     the "Desktop" item.  HOWEVER, when I try to get the MENUITEMs
  10.     of the Desktop Manager, WinQueryItem returns TRUE but
  11.     the MENUITEM appears to be trash. It never changes even though
  12.     the id does.  It's also strange that MM_QUERYITEM returns the
  13.     correct length for each of the items.
  14.  
  15.     Any assistance or comments would be greatly appreciated.  I
  16.     usually check the OS/2 Shareware Bulletin board once a week. (703) 385-4325
  17.     I also sign on to CompuServe about once a week, but I can't
  18.     say that I always visit a certain forum, which means you'd
  19.     have to use the CIS mail system to reach me.
  20.  
  21.     Chuck Hipschman 71041,1542
  22. */
  23.  
  24. #define INCL_WIN
  25. #define INCL_GPI
  26.  
  27. #include <os2.h>
  28. #include <string.h>
  29. #include <stdio.h>
  30. #include "shutmenu.h"
  31.  
  32. #define SZ_DESKTOP_SWTITLE    "Desktop Manager"
  33. #define IDM_SHUTDOWN_CMD    0x131
  34.  
  35. #if defined( DEBUG )
  36.     #define D( x ) x
  37. #else
  38.     #define D( x )
  39. #endif
  40.  
  41. MRESULT EXPENTRY ClientWndProc( HWND, USHORT, MPARAM, MPARAM );
  42.  
  43. HAB hab;
  44.  
  45. int main( void )
  46.     {
  47.     HMQ hmq;
  48.     HWND hwndFrame, hwndClient;
  49.     QMSG qmsg;
  50.     static CHAR szClass[] = "Shutdown";
  51.     ULONG ctlData = FCF_STANDARD & ~FCF_ACCELTABLE;
  52.  
  53.     hab = WinInitialize( 0 );
  54.     hmq = WinCreateMsgQueue(
  55.         hab,    /* HAB        hab;    handle of the anchor block    */
  56.         0);     /* SHORT    cmsg;    size of the message queue    */
  57.  
  58.     if ( ! WinRegisterClass(
  59.         hab,            /* HAB        hab;            handle of anchor block        */
  60.         szClass,        /* PSZ        pszClassName;    points to class name        */
  61.         ClientWndProc,    /* PFNWP    pfnWndProc;     address of window procedure */
  62.         CS_SIZEREDRAW,    /* ULONG    flStyle;        window-style flags            */
  63.         0 )             /* USHORT    cbWindowData;    amount of reserved data     */
  64.         )
  65.         return ( FALSE );
  66.  
  67.     hwndFrame = WinCreateStdWindow(
  68.         HWND_DESKTOP,    /* HWND     hwndParent;         handle of the parent window   */
  69.         WS_VISIBLE,     /* ULONG    flStyle;            frame-window style          */
  70.         &ctlData,        /* PULONG    pflCreateFlags;     creation flags              */
  71.         szClass,        /* PSZ        pszClientClass;     client-window class name          */
  72.         "",             /* PSZ      pszTitle;           address of title-bar text         */
  73.         0L,             /* ULONG    flClientStyle;        client-window style       */
  74.         0,                /* HMODULE    hmod;                handle of the resource module    */
  75.         ID_RESOURCE,    /* USHORT    idResources;        frame-window identifier       */
  76.         &hwndClient );    /* PHWND    phwndClient;        address of client-window handle */
  77.  
  78.     while ( WinGetMsg( hab, &qmsg, NULL, 0, 0 ) )
  79.         WinDispatchMsg( hab, &qmsg );
  80.  
  81.     WinDestroyWindow( hwndFrame );
  82.     WinDestroyMsgQueue( hmq );
  83.     WinTerminate( hab );
  84.  
  85.     return ( 0 );
  86.     }
  87.  
  88. MRESULT EXPENTRY ClientWndProc( HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2 )
  89.     {
  90.     static SHORT    cxClient, cyClient;
  91.     static SHORT    cxChar, cyChar, cxCaps, cyDesc;
  92.     static USHORT    cTasks;
  93.     static PSWBLOCK pswblk;
  94.     static CHAR     szMenuText[ 10 ];
  95.  
  96.     HPS     hps;
  97.     SEL     sel;
  98.     USHORT    cbTaskListBuf;
  99.     SHORT    i;
  100.     POINTL    ptl;
  101.     LONG    lRc;
  102.     HWND    hwndDesktopMgr, hwndDesktopMenu;
  103.     SHORT    csMenuItems;
  104.     SHORT    idItem;
  105.  
  106.     switch ( msg )
  107.         {
  108.         case WM_CREATE:
  109.             {
  110.             FONTMETRICS fm;
  111.  
  112.             hps = WinGetPS( hwnd );
  113.             GpiQueryFontMetrics( hps, (LONG) sizeof fm, &fm );
  114.             cxChar = (SHORT) fm.lAveCharWidth;
  115.             cyChar = (SHORT) fm.lEmInc;
  116.             cxCaps = (SHORT) fm.lMaxBaselineExt;
  117.             cyDesc = (SHORT) fm.lMaxDescender;
  118.             WinReleasePS( hps );
  119.             }
  120.  
  121.             return 0;
  122.  
  123.         case WM_SIZE:
  124.             cxClient = SHORT1FROMMP( mp2 );
  125.             cyClient = SHORT2FROMMP( mp2 );
  126.             return 0;
  127.  
  128.         case WM_PAINT:
  129.             hps = WinBeginPaint( hwnd, NULL, NULL );
  130.             GpiErase( hps );
  131.             ptl.x    = cxCaps;
  132.             ptl.y    = cyClient - cyChar * 2 + cyDesc;
  133.  
  134.             cTasks = WinQuerySwitchList( hab, NULL, 0 );
  135.             cbTaskListBuf = ( cTasks * sizeof (SWENTRY) ) + sizeof (HSWITCH);
  136.             DosAllocSeg( cbTaskListBuf, &sel, SEG_NONSHARED );
  137.             pswblk = MAKEP( sel, 0 );
  138.             WinQuerySwitchList( hab, pswblk, cbTaskListBuf );
  139.  
  140.             for ( i = 0; (USHORT) i < cTasks; i++ )
  141.                 {
  142.                 if ( _fstrcmp( pswblk -> aswentry[ i ].swctl.szSwtitle,
  143.                         SZ_DESKTOP_SWTITLE ) == 0 )
  144.                     break;
  145.                 }
  146.  
  147.             if ( (USHORT) i >= cTasks )
  148.                 {
  149.                 WinMessageBox( HWND_DESKTOP, hwnd,
  150.                             "Desktop Manager not found in Task List", NULL, 0,
  151.                             MB_ENTER | MB_ICONEXCLAMATION );
  152.                 WinPostMsg( hwnd, WM_QUIT, 0L, 0L );
  153.                 }
  154.  
  155.             hwndDesktopMgr    = pswblk -> aswentry[ i ].swctl.hwnd;
  156.  
  157.             /* Try to access the text of the action bar items
  158.                It works for the program's own menu, but not for the
  159.                Desktop Manager action bar. */
  160.  
  161.             // hwndDesktopMenu = WinWindowFromID( WinQueryWindow( hwnd, QW_PARENT, FALSE ), FID_MENU );
  162.             hwndDesktopMenu = WinWindowFromID( hwndDesktopMgr, FID_MENU );
  163.             csMenuItems = SHORT1FROMMR (WinSendMsg( hwndDesktopMenu, MM_QUERYITEMCOUNT, 0L, 0L ));
  164.  
  165.             D( printf( "*050* hwndDesktopMenu = %Fp, Items = %d\n", hwndDesktopMenu, csMenuItems ); )
  166.             for ( i = 0; i < csMenuItems; i++ )
  167.                 {
  168.                 MENUITEM mi;
  169.  
  170.                 idItem = SHORT1FROMMR (WinSendMsg( hwndDesktopMenu, MM_ITEMIDFROMPOSITION,
  171.                     MPFROMSHORT( i ), 0L ));
  172.                 D( printf( "*060* idItem = %d\n", idItem    ); )
  173.  
  174.                 lRc = (LONG) WinSendMsg( hwndDesktopMenu,
  175.                     MM_QUERYITEM,
  176.                     MPFROM2SHORT( idItem, (BOOL) TRUE ),
  177.                     (MPARAM) (PMENUITEM) &mi );
  178.                 D( printf( "*070* MM_QUERYITEM = %ld, Style = %x %x %lx\n", lRc, mi.afStyle, MIS_TEXT, mi.hItem ); )
  179.                 D( printf( "*071* \t mi.iPosition   = %d\n",  mi.iPosition      ); )
  180.                 D( printf( "*072* \t mi.afStyle     = %x\n",  mi.afStyle        ); )
  181.                 D( printf( "*073* \t mi.afAttribute = %x\n",  mi.afAttribute    ); )
  182.                 D( printf( "*074* \t mi.id          = %d\n",  mi.id             ); )
  183.                 D( printf( "*075* \t mi.hwndSubMenu = %Fp\n", mi.hwndSubMenu    ); )
  184.                 D( printf( "*076* \t mi.hItem       = %ld\n", mi.hItem          ); )
  185.  
  186.                 lRc = (LONG) WinSendMsg( hwndDesktopMenu, MM_QUERYITEMTEXT,
  187.                     MPFROM2SHORT( idItem, 10 ),
  188.                     MPFROMP( (PSZ) szMenuText ) );
  189.  
  190.                 #if defined( DEBUG )
  191.                     if ( lRc == 0 )
  192.                         printf( "*081* Menu Item Text Length == 0\n" );
  193.                     else
  194.                         printf( "*082* Len = %ld, Item = %s\n", lRc, szMenuText );
  195.                 #endif
  196.                 }
  197.  
  198.             #if !defined( DEBUG )
  199.                 lRc = (LONG) WinSetWindowPos( hwndDesktopMgr, hwnd,
  200.                     0, 0, 0, 0, SWP_RESTORE );
  201.                 lRc = (LONG) WinPostMsg( hwndDesktopMgr, WM_COMMAND, MPFROM2SHORT( IDM_SHUTDOWN_CMD, 0 ), 0L );
  202.             #endif
  203.  
  204.             WinEndPaint( hps );
  205.             return 0;
  206.  
  207.         case WM_CLOSE:
  208.             WinPostMsg( hwnd, WM_QUIT, 0L, 0L );
  209.             break;
  210.  
  211.         }
  212.  
  213.     return WinDefWindowProc( hwnd, msg, mp1, mp2 );
  214.     }
  215.  
  216.  
  217.