home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / xray10.zip / source.zip / xray.c < prev    next >
C/C++ Source or Header  |  1996-12-22  |  8KB  |  193 lines

  1. /**
  2. File:   xray.c
  3. Notes:  main interface - uses xrayhook dll
  4.         This sample code provided by CodeSmith Software. Use as you wish.
  5. **/
  6.  
  7. #define INCL_WIN
  8. #define INCL_GPI
  9. #define INCL_DOSPROCESS
  10.  
  11. #include <os2.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <ctype.h>
  15.  
  16. #include "xray.h"
  17. #include "xrayrc.h"
  18.  
  19.  
  20. HWND hwndFrame;             // xrays frame
  21. BOOL OnTop = 0;             // if set, then display xray window at top of screen
  22. ULONG cxDesktop,            // width of desktop
  23.       cyDesktop,            // height of desktop
  24.       cyXray;               // height of our xray window
  25. GLOBALS *Global;        // pointer to global data in the dll
  26.  
  27. MRESULT EXPENTRY AboutDlgProc (HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  28. {
  29.     CHAR version[30];
  30.  
  31.     switch (msg) {
  32.  
  33.         case WM_COMMAND:
  34.                     switch (COMMANDMSG(&msg)->cmd) {
  35.                                 case DID_OK:
  36.                                 case DID_CANCEL:    WinDismissDlg (hwnd, TRUE);
  37.                                                     return 0;
  38.                     }
  39.                     break;
  40.     }
  41.     return WinDefDlgProc (hwnd, msg, mp1, mp2) ;
  42. }
  43.  
  44. MRESULT EXPENTRY ClientWndProc (HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
  45. {
  46.     CHAR buf[128];
  47.     HPS hps;
  48.     RECTL rcl;
  49.     static HWND hwndMenu;
  50.     static ULONG yOffset = 0;
  51.     POINTL ptlMouse;
  52.  
  53.     switch (msg) {
  54.  
  55.         case WM_CREATE:
  56.                     hwndMenu = WinLoadMenu(HWND_OBJECT, 0, IDM_POPUP);
  57.                     if(OnTop)
  58.                         WinSendMsg(hwndMenu, MM_SETITEMATTR, MPFROM2SHORT(IDM_MOVE, TRUE), MPFROM2SHORT(MIA_CHECKED, MIA_CHECKED));
  59.                     if(Global->floatOnTop)
  60.                         WinSendMsg(hwndMenu, MM_SETITEMATTR, MPFROM2SHORT(IDM_FLOAT, TRUE), MPFROM2SHORT(MIA_CHECKED, MIA_CHECKED));
  61.                     return 0;
  62.  
  63.         case WM_CONTEXTMENU:
  64.                     if(hwndMenu != NULLHANDLE) {
  65.                         WinQueryPointerPos(HWND_DESKTOP, &ptlMouse);
  66.                         WinPopupMenu(HWND_DESKTOP, hwnd, hwndMenu, ptlMouse.x, ptlMouse.y, 0,
  67.                                      PU_HCONSTRAIN | PU_VCONSTRAIN | PU_MOUSEBUTTON1 | PU_MOUSEBUTTON2 | PU_KEYBOARD);
  68.                         return 0;
  69.                     }
  70.                     break;
  71.  
  72.         case WM_COMMAND:
  73.                     switch(COMMANDMSG(&msg)->cmd) {
  74.                         case IDM_CLOSE:
  75.                                     WinSendMsg(hwnd, WM_CLOSE, 0L, 0L);
  76.                                     return 0;
  77.                         case IDM_MOVE:
  78.                                     if(WinSendMsg(hwndMenu, MM_QUERYITEMATTR, MPFROM2SHORT(IDM_MOVE, TRUE), MPFROMSHORT(MIA_CHECKED)) != 0) {
  79.                                         yOffset = 0;
  80.                                         WinSendMsg(hwndMenu, MM_SETITEMATTR, MPFROM2SHORT(IDM_MOVE, TRUE), MPFROM2SHORT(MIA_CHECKED, 0));
  81.                                     } else {
  82.                                         yOffset = cyDesktop - cyXray;
  83.                                         WinSendMsg(hwndMenu, MM_SETITEMATTR, MPFROM2SHORT(IDM_MOVE, TRUE), MPFROM2SHORT(MIA_CHECKED, MIA_CHECKED));
  84.                                     }
  85.                                     WinSetWindowPos(hwndFrame, 0, 0, yOffset, cxDesktop, cyXray, SWP_MOVE | SWP_SIZE);
  86.                                     return 0;
  87.                         case IDM_FLOAT:
  88.                                     if(WinSendMsg(hwndMenu, MM_QUERYITEMATTR, MPFROM2SHORT(IDM_FLOAT, TRUE), MPFROMSHORT(MIA_CHECKED)) != 0) {
  89.                                         Global->floatOnTop = 0;
  90.                                         WinSendMsg(hwndMenu, MM_SETITEMATTR, MPFROM2SHORT(IDM_FLOAT, TRUE), MPFROM2SHORT(MIA_CHECKED, 0));
  91.                                     } else {
  92.                                         Global->floatOnTop = 1;
  93.                                         WinSendMsg(hwndMenu, MM_SETITEMATTR, MPFROM2SHORT(IDM_FLOAT, TRUE), MPFROM2SHORT(MIA_CHECKED, MIA_CHECKED));
  94.                                     }
  95.                                     return 0;
  96.                         case IDM_ABOUT:
  97.                                     WinDlgBox(HWND_DESKTOP, hwnd, AboutDlgProc, NULLHANDLE, DLG_ABOUT, NULL);
  98.                                     return 0;
  99.                     }
  100.                     break;
  101.  
  102.         case WM_PAINT:
  103.                     hps = WinBeginPaint(hwnd, NULLHANDLE, NULL);
  104.                     WinQueryWindowRect (hwnd, &rcl);
  105.                     WinDrawText (hps, -1, Global->winInfo, &rcl, CLR_NEUTRAL, CLR_BACKGROUND,
  106.                                 DT_LEFT | DT_VCENTER | DT_ERASERECT);
  107.  
  108.                     WinEndPaint(hps);
  109.                     return 0;
  110.         default:                    // if this is our custom float-to-top msg
  111.                     if(msg == Global->wmu_FloatToTop) {
  112.                          WinSetWindowPos(hwndFrame, HWND_TOP, 0, 0, 0, 0, SWP_ZORDER);
  113.                          WinInvalidateRect(hwnd, NULL, FALSE);       // refresh window
  114.                          return 0;
  115.                     }
  116.                     break;
  117.  
  118.     }
  119.     return WinDefWindowProc (hwnd, msg, mp1, mp2);
  120. }
  121.  
  122. INT ProcessArgs(int argc, char **argv)
  123. {
  124.     INT i, gotBadArg = 0;
  125.     CHAR *s;
  126.  
  127.     for(i=1; i < argc; i++)  {                          // process each argument
  128.         if(argv[i][0] == '/' || argv[i][0] == '-') {    // an option ?
  129.             for(s = argv[i]; *s; s++) {
  130.                 switch(toupper(*s)) {
  131.                     case 'F':   Global->floatOnTop = 1;
  132.                                 break;
  133.                     case 'T':   OnTop = 1;
  134.                                 break;
  135.                     case '/':
  136.                     case '-':   break;          // start of next arg
  137.  
  138.                     default:    gotBadArg = 1;
  139.                 }
  140.             }
  141.         }
  142.     }
  143.     return gotBadArg;
  144. }
  145.  
  146. VOID APIENTRY ExeTrap()
  147. {
  148.     XrayKill();
  149.     DosExitList(EXLST_EXIT, (PFNEXITLIST)ExeTrap);
  150. }
  151.  
  152. INT main (INT argc, CHAR **argv) {
  153.     HMQ  hmq ;
  154.     HWND hwndClient;
  155.     QMSG qmsg ;
  156.     HAB habXray;
  157.     ULONG frameFlags = FCF_BORDER | FCF_TASKLIST;
  158.     static CHAR *clientClass = "Xray";
  159.     FONTMETRICS fm;
  160.     ULONG cyBorder;
  161.     HPS hps;
  162.  
  163.     habXray = WinInitialize(0);
  164.     hmq = WinCreateMsgQueue(habXray, 0);
  165.  
  166.     DosExitList(EXLST_ADD, (PFNEXITLIST)ExeTrap);   // trap exceptions to ensure hooks released
  167.     Global = XraySetGlobals();                        // initialize global data pointer
  168.     ProcessArgs(argc, argv);
  169.     WinRegisterClass (habXray, clientClass, ClientWndProc, 0L, 0);
  170.     hwndFrame = WinCreateStdWindow (HWND_DESKTOP, WS_VISIBLE, &frameFlags, clientClass,
  171.                                     "X-Ray", 0L, 0, ID_ICON, &hwndClient);
  172.                         // now set up frame size, based on a small font
  173.     cxDesktop = WinQuerySysValue(HWND_DESKTOP, SV_CXSCREEN);
  174.     cyDesktop = WinQuerySysValue(HWND_DESKTOP, SV_CYSCREEN);
  175.     cyBorder = WinQuerySysValue(HWND_DESKTOP, SV_CYBORDER);
  176.     WinSetPresParam(hwndClient, PP_FONTNAMESIZE, 7, (PVOID)"8.Helv");
  177.     hps = WinGetPS(hwndClient);
  178.     GpiQueryFontMetrics(hps, sizeof(fm), &fm);
  179.     WinReleasePS(hps);
  180.     cyXray = fm.lMaxBaselineExt + cyBorder * 2;
  181.     WinSetWindowPos(hwndFrame, 0, 0, (OnTop) ? cyDesktop - cyXray : 0,
  182.                                   cxDesktop, cyXray, SWP_MOVE | SWP_SIZE);
  183.  
  184.     XrayInit(hwndClient);                       // enable hooks
  185.     while (WinGetMsg(habXray, &qmsg, NULLHANDLE, 0, 0))
  186.          WinDispatchMsg(habXray, &qmsg);
  187.     XrayKill();                                 // disable hooks
  188.     WinDestroyWindow (hwndFrame);
  189.     WinDestroyMsgQueue(hmq);
  190.     WinTerminate(habXray);
  191.     return 0;
  192. }
  193.