home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast.iso / pcmag / vol12n03.zip / RCLICK.C < prev    next >
Text File  |  1992-10-30  |  10KB  |  358 lines

  1. //========================================================================
  2. // 
  3. //  RClick.c -- A popup menu program
  4. //
  5. //  Copyright (c) Douglas Boling, 1993
  6. //
  7. //  For better readability, set tab stops to every 3 characters.
  8. //
  9. //========================================================================
  10. #define  WINVER      0x0300
  11.  
  12. #include <windows.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include "rclick.h"
  16. #include "rchook.h"
  17.  
  18. //------------------------------------------------------------------------
  19. //Function prototypes
  20. //------------------------------------------------------------------------
  21. int    PASCAL         WinMain (HANDLE, HANDLE, LPSTR, int);
  22. long    FAR PASCAL    WndProc (HWND, WORD, WORD, LONG);
  23.  
  24.  
  25. char     szStr[64];
  26. char        szTemp[20];
  27.  
  28. char        szAppName[] = "RClick";
  29. char        szProfileName[] = "RCLICK.INI";
  30. HANDLE      hInst;
  31. HWND        hwndTarg;
  32. HOOKSTAT    hsStatus;
  33. int        xPos, yPos;
  34.  
  35. //=======================================================================
  36. //
  37. // Program Entry Point (WinMain)
  38. //
  39. //=======================================================================
  40. int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
  41.                 LPSTR lpszCmdParam, int nCmdShow) {
  42.  
  43.    HWND            hwnd;
  44.     HANDLE        hAccel;
  45.    MSG            msg;
  46.    WNDCLASS        wndclass;
  47.    int            i, sCount;
  48.  
  49.     if (hPrevInstance) 
  50.        return 0;
  51.        
  52.    wndclass.style          = CS_HREDRAW | CS_VREDRAW;
  53.    wndclass.lpfnWndProc    = WndProc;
  54.    wndclass.cbClsExtra     = 0;
  55.    wndclass.cbWndExtra     = DLGWINDOWEXTRA;
  56.    wndclass.hInstance      = hInstance;
  57.    wndclass.hIcon          = LoadIcon (hInstance, szAppName);
  58.    wndclass.hCursor        = LoadCursor (NULL, IDC_ARROW);
  59.    wndclass.hbrBackground  = COLOR_WINDOW + 1;
  60.    wndclass.lpszMenuName   = 0;
  61.    wndclass.lpszClassName  = szAppName;
  62.    RegisterClass (&wndclass);
  63.  
  64.    hwnd = CreateDialog (hInstance, szAppName, 0, NULL);
  65.     nCmdShow |= SW_MINIMIZE;
  66.  
  67.     ShowWindow (hwnd, nCmdShow);
  68.     hAccel = LoadAccelerators (hInstance, szAppName);
  69.     
  70.     sCount = GetPrivateProfileInt (szAppName, "Count", 0, szProfileName);
  71.     for (i = 0; i < sCount; i++) {
  72.        itoa (i, szTemp, 10);
  73.        GetPrivateProfileString ("NoPopProgs", szTemp, "None", szStr, 
  74.                                  sizeof (szStr), szProfileName);
  75.         if (strcmp (szStr, "None") != 0)                                 
  76.             SetNoPop ((LPSTR) szStr);
  77.     }    
  78.     if (GetPrivateProfileInt (szAppName, "Enabled", 1, szProfileName))
  79.         hsStatus.bEnabled = TRUE;
  80.     else
  81.         hsStatus.bEnabled = FALSE;
  82.        
  83.     hsStatus.hwndMyWin = hwnd;
  84.     SetStatus (&hsStatus);
  85.  
  86.     while (GetMessage (&msg, NULL, 0, 0)) {
  87.        TranslateMessage (&msg);
  88.        DispatchMessage (&msg);
  89.     }
  90.     //Disable tracking
  91.     
  92.    return msg.wParam;
  93. }
  94.  
  95. //========================================================================
  96. //
  97. // Routines used by WndProc
  98. //
  99. //========================================================================
  100. //------------------------------------------------------------------------
  101. // Set Enable Text
  102. //------------------------------------------------------------------------
  103. VOID SetButtons (HWND hwnd, BOOL bEnable) {
  104.  
  105.     if (bEnable) {
  106.         SetWindowText (hwnd, "RClick - Enabled");
  107.         SetDlgItemText (hwnd, IDD_ENABLE, "Disable");
  108.     } else {
  109.         SetWindowText (hwnd, "Rclick - Disabled");
  110.         SetDlgItemText (hwnd, IDD_ENABLE, "Enable");
  111.     }
  112.    if (SendDlgItemMessage (hwnd, IDD_NOPOPLIST, LB_GETCURSEL, 0, 0L) == LB_ERR)
  113.         EnableWindow (GetDlgItem (hwnd, IDD_REMOVE), FALSE);
  114.     else
  115.         EnableWindow (GetDlgItem (hwnd, IDD_REMOVE), TRUE);
  116.     
  117.     return;
  118. }
  119.  
  120. //------------------------------------------------------------------------
  121. //
  122. // About box dialog procedure
  123. //
  124. //------------------------------------------------------------------------
  125. BOOL FAR PASCAL AboutDlgProc (HWND hwnd, WORD message, WORD wParam, 
  126.                                LONG lParam) {
  127.     RECT    rect;
  128.     
  129.     switch (message) {
  130.         case WM_INITDIALOG:
  131.             GetWindowRect  (hwnd, &rect);
  132.             xPos = max (0, xPos - (rect.right - rect.left)/2);
  133.             yPos = max (0, yPos - (rect.bottom - rect.top)/2);
  134.             SetWindowPos (hwnd, 0, xPos, yPos, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
  135.         
  136.             return TRUE;
  137.             
  138.         case WM_COMMAND:
  139.             switch (wParam) {
  140.  
  141.                 case IDOK:
  142.                     EndDialog (hwnd, 1);
  143.                     return TRUE;
  144.             }
  145.             break;
  146.  
  147.         case WM_CLOSE:
  148.             EndDialog (hwnd, 0);
  149.             return TRUE;
  150.     }
  151.     return FALSE;    
  152. }
  153.  
  154. //------------------------------------------------------------------------
  155. // Config box dialog procedure
  156. //------------------------------------------------------------------------
  157. BOOL FAR PASCAL ConfigDlgProc (HWND hwnd, WORD message, WORD wParam, 
  158.                                 LONG lParam) {
  159.     char        szText[128];
  160.     RECT        rect;
  161.     HOOKSTAT    hsHook;
  162.                                      
  163.     switch (message) {
  164.         case WM_INITDIALOG:
  165.             GetWindowRect  (hwnd, &rect);
  166.             xPos = max (0, xPos - (rect.right - rect.left)/2);
  167.             yPos = max (0, yPos - (rect.bottom - rect.top)/2);
  168.             SetWindowPos (hwnd, 0, xPos, yPos, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
  169.         
  170.             GetWindowText (hwndTarg, szStr, sizeof szStr);
  171.             strcpy (szText, "Window Title:\t");
  172.             strcat (szText, szStr);
  173.             SetDlgItemText (hwnd, IDD_WINTITLE, szText);
  174.  
  175.             GetModuleFileName (GetWindowWord (hwndTarg, GWW_HINSTANCE),
  176.                                szStr, sizeof (szStr));
  177.                                            
  178.             strcpy (szText, "Program Name:\t");
  179.             strcat (szText, strrchr(szStr, '\\')+1);
  180.             SetDlgItemText (hwnd, IDD_PROGNAME, szText);
  181.             break;            
  182.             
  183.         case WM_COMMAND:
  184.             GetStatus (&hsHook);
  185.             switch (wParam) {
  186.               //
  187.               // Button controls
  188.               //    
  189.                 case IDD_DELBYTITLE:
  190.                     GetModuleFileName (GetWindowWord (hwndTarg, GWW_HINSTANCE),
  191.                                        szStr, sizeof (szStr));
  192.                     SetNoPop ((LPSTR) strrchr(szStr, '\\')+1);
  193.                     EndDialog (hwnd, 1);
  194.                     return TRUE;
  195.                 
  196.                 case IDD_DISABLERB:
  197.                     if (hsHook.bEnabled) 
  198.                        hsHook.bEnabled = FALSE;
  199.                     else
  200.                        hsHook.bEnabled = TRUE;
  201.                     SetStatus (&hsHook);
  202.                     EndDialog (hwnd, 1);
  203.                     return TRUE;
  204.                 
  205.                 case IDCANCEL:
  206.                     EndDialog (hwnd, 1);
  207.                     return TRUE;
  208.             }
  209.             break;
  210.  
  211.         case WM_CLOSE:
  212.             EndDialog (hwnd, 0);
  213.             return TRUE;
  214.     }
  215.     return FALSE;    
  216. }
  217. //========================================================================
  218. //
  219. // Main Window Procedure.
  220. //
  221. //========================================================================
  222. long FAR PASCAL WndProc (HWND hwnd, WORD message, WORD wParam, LONG lParam) {
  223.  
  224.     FARPROC    lpfnDlgFn;
  225.     HOOKSTAT    hsHook;
  226.     RECT        rect;
  227.     int        i;
  228.  
  229.    switch (message) {
  230.     
  231.       case WM_CREATE:
  232.             hInst = ((LPCREATESTRUCT) lParam)->hInstance;
  233.             return 0;
  234.  
  235.         case WM_SIZE:
  236.             if (wParam == SIZE_RESTORED) {
  237.                 GetStatus (&hsHook);
  238.                 SendDlgItemMessage (hwnd, IDD_NOPOPLIST, LB_RESETCONTENT, 0, 0L);
  239.                 for (i = 0; i < hsHook.sNoPopCnt; i++) {
  240.                    GetNoPop (i, (LPSTR) szStr);
  241.                     SendDlgItemMessage (hwnd, IDD_NOPOPLIST, LB_ADDSTRING, 0, 
  242.                                         (LPARAM)(LPSTR) szStr);
  243.                 }
  244.                 SetButtons (hwnd, hsHook.bEnabled);
  245.             }    
  246.             break;
  247.  
  248.         case RBM_SHOWCONFIGDLG:
  249.             hwndTarg = (HWND) wParam;
  250.             xPos = LOWORD (GetMessagePos());
  251.             yPos = HIWORD (GetMessagePos());
  252.             
  253.             lpfnDlgFn = MakeProcInstance (ConfigDlgProc, hInst);
  254.             DialogBox (hInst, "Config", hwnd, lpfnDlgFn);
  255.             FreeProcInstance (lpfnDlgFn);
  256.  
  257.             GetStatus (&hsHook);
  258.             SendDlgItemMessage (hwnd, IDD_NOPOPLIST, LB_RESETCONTENT, 0, 0L);
  259.             for (i = 0; i < hsHook.sNoPopCnt; i++) {
  260.                GetNoPop (i, (LPSTR) szStr);
  261.                 SendDlgItemMessage (hwnd, IDD_NOPOPLIST, LB_ADDSTRING, 0, 
  262.                                     (LPARAM)(LPSTR) szStr);
  263.             }
  264.             SetButtons (hwnd, hsHook.bEnabled);
  265.             return 0;
  266.         
  267.         case RBM_SHOWABOUTDLG:
  268.             xPos = LOWORD (GetMessagePos());
  269.             yPos = HIWORD (GetMessagePos());
  270.             
  271.             lpfnDlgFn = MakeProcInstance (AboutDlgProc, hInst);
  272.             DialogBox (hInst, "About", hwnd, lpfnDlgFn);
  273.             FreeProcInstance (lpfnDlgFn);
  274.             return 0;
  275.         
  276.         case WM_COMMAND:
  277.  
  278.             switch (wParam) {
  279.             //
  280.             // Listbox control
  281.             //
  282.               case IDD_NOPOPLIST:
  283.                   if (HIWORD (lParam) == LBN_SELCHANGE) {
  284.                         EnableWindow (GetDlgItem (hwnd, IDD_REMOVE), TRUE);
  285.                         SetButtons (hwnd, hsHook.bEnabled);
  286.                   }   
  287.                   break;
  288.               //
  289.               // Button controls
  290.               //    
  291.                 case IDD_REMOVE:
  292.                   if (HIWORD (lParam) == BN_CLICKED) {
  293.                        i = (int) SendDlgItemMessage (hwnd, IDD_NOPOPLIST, 
  294.                                                      LB_GETCURSEL, 0, 0L);
  295.                         if (i != LB_ERR) {
  296.                            SendDlgItemMessage (hwnd, IDD_NOPOPLIST, LB_GETTEXT, 
  297.                                                i, (LPARAM) (LPSTR) szStr);
  298.                            SendDlgItemMessage (hwnd, IDD_NOPOPLIST, 
  299.                                                LB_DELETESTRING, i, 0L);
  300.                             RemoveNoPop ((LPSTR) szStr);
  301.                         }
  302.                     }    
  303.                     break;
  304.  
  305.                 case IDD_ABOUT:
  306.                   if (HIWORD (lParam) == BN_CLICKED) {
  307.                         GetWindowRect (hwnd, &rect);
  308.                         xPos = rect.left + (rect.right - rect.left)/2;
  309.                         yPos = rect.top + (rect.bottom - rect.top)/2;
  310.  
  311.                         lpfnDlgFn = MakeProcInstance (AboutDlgProc, hInst);
  312.                         DialogBox (hInst, "About", hwnd, lpfnDlgFn);
  313.                         FreeProcInstance (lpfnDlgFn);
  314.                     }    
  315.                     break;
  316.  
  317.                 case IDD_ENABLE:
  318.                   if (HIWORD (lParam) == BN_CLICKED) {
  319.                         GetStatus (&hsHook);
  320.                         if (hsHook.bEnabled) 
  321.                            hsHook.bEnabled = FALSE;
  322.                         else
  323.                            hsHook.bEnabled = TRUE;
  324.                         SetButtons (hwnd, hsHook.bEnabled);
  325.                         SetStatus (&hsHook);
  326.                     }    
  327.                     break;
  328.  
  329.                 case IDD_EXIT:                
  330.                   if (HIWORD (lParam) == BN_CLICKED) 
  331.                       SendMessage (hwnd, WM_CLOSE, 0, 0);
  332.                     break;
  333.             }
  334.             break;
  335.  
  336.         case WM_DESTROY:
  337.             GetStatus (&hsHook);
  338.             itoa (hsHook.sNoPopCnt, szStr, 10);
  339.             WritePrivateProfileString (szAppName, "Count", szStr, szProfileName);
  340.             
  341.             for (i = 0; i < hsHook.sNoPopCnt; i++) {
  342.                GetNoPop (i, (LPSTR) szStr);
  343.                 itoa (i, szTemp, 10);
  344.                 WritePrivateProfileString ("NoPopProgs", szTemp, szStr, szProfileName);
  345.             }
  346.             if (GetPrivateProfileInt (szAppName, "Enabled", 1, szProfileName))
  347.             if (hsStatus.bEnabled)
  348.                 WritePrivateProfileString (szAppName, "Enabled", "1", szProfileName);
  349.             else
  350.                 WritePrivateProfileString (szAppName, "Enabled", "0", szProfileName);
  351.  
  352.             PostQuitMessage (0);
  353.             return 0;
  354.     }
  355.     return DefWindowProc (hwnd, message, wParam, lParam);
  356. }
  357. 
  358.