home *** CD-ROM | disk | FTP | other *** search
/ Media Share 13 / mediashare_13.zip / mediashare_13 / ZIPPED / PROGRAM / WTJ9403.ZIP / WILDASS / SOURCE / HOOKSDLL.CPP < prev    next >
C/C++ Source or Header  |  1993-08-14  |  6KB  |  172 lines

  1. // mshoook.cpp
  2. //
  3. // (de)install the mouse hook fxn
  4. //   
  5. // update: 1.00 08-Aug-93 tw
  6. //         1.01 14-Aug-93 tw removed bug in MouseHookProc
  7.  
  8. #include <windows.h>
  9. #include "hooksdll.h"
  10.  
  11. // dll fxns
  12.  
  13. int  __export CALLBACK LibMain( HANDLE hModule, WORD wDataSeg, WORD cbHeapSize, LPSTR lpszCmdLine);
  14. // int  __export CALLBACK _WEP( int bSystemExit);
  15. // WEP is optional in win 3.1
  16. void __export CALLBACK MouseHookFunc( int nCode, WORD wParam, DWORD lParam);
  17.  
  18. // global vars for inside mosue hook dll
  19.  
  20. HANDLE  msHookDLL_hInstance;     // Global instance handle for  DLL
  21. HHOOK   msHookDLL_hhookHooks;    // last mouse hook
  22. HWND    msHookDLL_wndDektop;     // Handle of DesktopWindow
  23. HWND    msHookDLL_wndApplication;// Handle of Application Window          
  24. BOOL    msHookDLL_fComeUpAllways = FALSE; // default: only come up when dsktop is clicked
  25.  
  26. #ifdef _DEBUG
  27. BOOL    msHookDLL_fInitCalled = FALSE; // in debug version, make sure init was called
  28. #endif
  29.  
  30. // dll do-nothint stuff
  31. int __export CALLBACK 
  32. LibMain( HANDLE hModule, WORD wDataSeg, WORD cbHeapSize, LPSTR lpszCmdLine)
  33. {
  34.     msHookDLL_hInstance = hModule;   // store instance handle
  35.     return 1;
  36. }
  37.  
  38. // WEP is optional in win 3.1
  39. // as we don`t need no cleanup,.... skip it
  40. //int __export 
  41. //CALLBACK _WEP (int bSystemExit)
  42. //{
  43. //    return 1;
  44. //}
  45.  
  46.  
  47. void __export CALLBACK
  48. MsHookDLL_InitDLL( HWND wndApplication )
  49. {                                   
  50.    // make sure debugging version doesnt assert
  51. #ifdef _DEBUG
  52.    msHookDLL_fInitCalled = TRUE;
  53. #endif   
  54.  
  55.    // store handle to Desktop window
  56.    msHookDLL_wndDektop = GetDesktopWindow();
  57.    
  58.    // store handle to application window
  59.    msHookDLL_wndApplication = wndApplication;
  60.    
  61. }
  62.    
  63. // InstallFilter()
  64. //
  65. // (de)install the mouse message filter.
  66. // store the previous mouse hook fxn 
  67. //
  68. // in the debugging version, make sure InitMsHook() has been called
  69. // 
  70. void __export CALLBACK 
  71. MsHookDLL_InstallFilter( BOOL fCode )
  72. {  
  73.    // make sure init was called before
  74. #ifdef _DEBUG
  75.    if( ! msHookDLL_fInitCalled )
  76.       OutputDebugString("MsHookDLL_SetComUpParam: InitDLL not called prior to usage\n");
  77. #endif   
  78.  
  79.    if ( fCode )   // install the filter
  80.    {
  81.       msHookDLL_hhookHooks = SetWindowsHookEx( WH_MOUSE, (FARPROC) MouseHookFunc, msHookDLL_hInstance, NULL ); 
  82.    }
  83.    else           // remove the filter
  84.    {
  85.       UnhookWindowsHookEx( msHookDLL_hhookHooks );
  86.    }
  87. }
  88.  
  89. // set the behaviour: should we come up on every right
  90. // mouse click or only when clicked upon the desktop...
  91. void __export CALLBACK 
  92. MsHookDLL_SetComeUpAllways( BOOL fCode )
  93. {
  94.    // make sure init was called before
  95. #ifdef _DEBUG
  96.    if( ! msHookDLL_fInitCalled )
  97.       OutputDebugString("MsHookDLL_SetComUpParam: InitDLL not called prior to usage\n");
  98. #endif   
  99.    
  100.    // set the behaviour
  101.    msHookDLL_fComeUpAllways = fCode;
  102. }
  103.  
  104. // MouseHookFunc
  105. // 
  106. // Handle mouse messages
  107. //          
  108. // update 1.01 - removed bug that caused the windowpos to be calculated
  109. //               relativ to the window beeing clicked upon; not relativ
  110. //               to the desktop window
  111. void __export CALLBACK 
  112. MouseHookFunc( int nCode, WORD wParam, DWORD lParam )
  113. {
  114.    if ( nCode >= 0 ) 
  115.    {
  116.       if ( wParam == WM_RBUTTONDOWN )
  117.       {                          
  118.          LPMOUSEHOOKSTRUCT pMSH = (LPMOUSEHOOKSTRUCT)lParam;
  119.          
  120.          // if this is the desktop beeing clicked, come up ...
  121.          // that is, come up always when the behaviour is set this way ...
  122.          if ( ( msHookDLL_fComeUpAllways ) || 
  123.               ( ! msHookDLL_fComeUpAllways && (pMSH->hwnd == msHookDLL_wndDektop) ) )
  124.          {                    
  125.             // get our size
  126.             RECT rectWindow;
  127.             GetWindowRect( msHookDLL_wndApplication, &rectWindow );
  128.             
  129.             // take care all of us is _visible_, not parts
  130.             // outside the desktop ...
  131.             // the point clicked can`t possibly be out of of
  132.             // view, so check if the right and lower edges are within
  133.             // the desktop window ranges. if not, move the window so 
  134.             // it is completelyy visible 
  135.             RECT rectDesktop;            
  136.             GetWindowRect( msHookDLL_wndDektop, &rectDesktop );
  137.             int cxWindow = rectWindow.right - rectWindow.left;
  138.             int cyWindow = rectWindow.bottom - rectWindow.top;
  139.             int x = pMSH->pt.x;
  140.             int y = pMSH->pt.y;
  141.             int dx = rectDesktop.right - (x+cxWindow);                              
  142.             dx = dx < 0 ? dx : 0;
  143.             int dy = rectDesktop.bottom - (y+cyWindow);
  144.             dy = dy < 0 ? dy : 0;
  145.                     
  146.             // move the window to some appropriate place                              
  147.             MoveWindow( msHookDLL_wndApplication, x+dx, y+dy, 
  148.                         cxWindow, cyWindow, TRUE );
  149.                         
  150.             // if we are already visible, no need to call ShowWindow ...
  151.             if ( ! IsWindowVisible( msHookDLL_wndApplication ) )                        
  152.                ShowWindow( msHookDLL_wndApplication, SW_SHOWNORMAL );  
  153.             
  154.             // make sure we have focus               
  155.             SetFocus( msHookDLL_wndApplication ); 
  156.             
  157.             // throw away the message ....
  158.             // this will make sure noone else is coming up
  159.             // because of this message
  160.             return;
  161.          }            
  162.       }
  163.    }
  164.      
  165.    // make sure other hooks are called for
  166.    // the other messages ...     
  167.    CallNextHookEx( msHookDLL_hhookHooks, nCode, wParam, lParam);
  168. }
  169.  
  170.  
  171.  
  172.