home *** CD-ROM | disk | FTP | other *** search
/ PC World 2003 May / PCWorld_2003-05_cd.bin / Software / Vyzkuste / rbtray / rbhook.c < prev    next >
C/C++ Source or Header  |  2000-04-20  |  3KB  |  99 lines

  1. #include <windows.h>
  2. #include "rbtray.h"
  3.  
  4. #ifdef DBG
  5.  #define dbg(X,Y) PostMessage(FindWindow(NAME, NAME),WM_DBG,(WPARAM)Y,(LPARAM)X);
  6. #else
  7.  #define dbg(X,Y)
  8. #endif
  9.  
  10.  
  11. static HHOOK hMouse=NULL,
  12.              hCall =NULL,
  13.              hMsg  =NULL;
  14. static HWND  LastHit;
  15.  
  16. LRESULT CALLBACK
  17. MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
  18. {
  19.     if (nCode<0)return CallNextHookEx(hMouse, nCode, wParam, lParam);
  20.     if (wParam == WM_NCRBUTTONDOWN
  21.         && ((MOUSEHOOKSTRUCT*)lParam)->wHitTestCode == HTREDUCE)
  22.     {
  23.         LastHit=((MOUSEHOOKSTRUCT*)lParam)->hwnd;
  24.     };
  25.     if (wParam == WM_NCRBUTTONUP && LastHit==((MOUSEHOOKSTRUCT*)lParam)->hwnd
  26.         && ((MOUSEHOOKSTRUCT*)lParam)->wHitTestCode == HTREDUCE)
  27.     {
  28.         LastHit=(HWND)NULL;
  29.         PostMessage(FindWindow(NAME, NAME), WM_MYCMD, wParam,
  30.             (LPARAM)(((MOUSEHOOKSTRUCT*)lParam)->hwnd));
  31.     }
  32.     return CallNextHookEx(hMouse, nCode, wParam, lParam);
  33. };
  34.  
  35. LRESULT CALLBACK
  36. GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam)
  37. {
  38.     int i;
  39.     HMENU hSysMenu;
  40.     if (nCode<0)return CallNextHookEx(hMsg, nCode, wParam, lParam);
  41.     if (((MSG*)lParam)->message==WM_SIZE)
  42.     {
  43.        dbg("WM_SIZE",((MSG*)lParam)->hwnd);
  44.     };
  45.     if (((MSG*)lParam)->message==WM_SYSCOMMAND   &&
  46.         LOWORD(((MSG*)lParam)->wParam) == IDM_TRAY)
  47.     {
  48.         PostMessage(FindWindow(NAME, NAME), WM_MYCMD,((MSG*)lParam)->message,(LPARAM)(((MSG*)lParam)->hwnd));
  49.         dbg("WM_SYSCOMMAND",((MSG*)lParam)->hwnd);
  50.     };
  51.     if (((MSG*)lParam)->message==WM_PAINT)
  52.     {
  53.         hSysMenu=GetSystemMenu(((MSG*)lParam)->hwnd, FALSE);
  54.         for(i=0;i<GetMenuItemCount(hSysMenu) && hSysMenu;i++)
  55.             if(GetMenuItemID(hSysMenu,i)==IDM_TRAY) hSysMenu=0;
  56.         if (hSysMenu)
  57.         {
  58.             InsertMenu (hSysMenu, GetMenuItemID(hSysMenu,0),MF_STRING,IDM_TRAY,  "Minimize in tray");
  59.             InsertMenu (hSysMenu, GetMenuItemID(hSysMenu,1),MF_SEPARATOR,IDM_TRAY+1, NULL) ;
  60.         }
  61.     };
  62.     return 0;
  63. };
  64. LRESULT CALLBACK
  65. CallWndProc(int hCode,WPARAM wParam,LPARAM lParam)
  66. {
  67.     CWPSTRUCT *pcwps;
  68.  
  69.     pcwps = (CWPSTRUCT*)lParam;
  70.  
  71.     if (hCode >= 0 && pcwps && pcwps->hwnd && pcwps->message==WM_SHOWWINDOW)
  72.     {
  73.        PostMessage(FindWindow(NAME, NAME),WM_SHOWWINDOW,0,(WPARAM)(pcwps->hwnd));
  74.        return 0;
  75.     }
  76.     return CallNextHookEx(NULL, hCode, wParam, lParam);
  77. }
  78.  
  79. BOOL DLLIMPORT
  80. RegisterHook(HMODULE hLib)
  81. {
  82.     hMouse = SetWindowsHookEx(WH_MOUSE,     (HOOKPROC)MouseProc,  hLib, 0);
  83.     hMsg   = SetWindowsHookEx(WH_GETMESSAGE,(HOOKPROC)GetMsgProc, hLib, 0);
  84.     hCall  = SetWindowsHookEx(WH_CALLWNDPROC,(HOOKPROC)CallWndProc, hLib, 0);
  85.     if(hMouse==NULL || hMsg==NULL || hCall==NULL)
  86.     {
  87.         UnRegisterHook();
  88.         return FALSE;
  89.     }
  90.     return TRUE;
  91. }
  92. void DLLIMPORT
  93. UnRegisterHook(void)
  94. {
  95.     if(hMouse)UnhookWindowsHookEx(hMouse);
  96.     if(hMsg)  UnhookWindowsHookEx(hMsg);
  97.     if(hCall) UnhookWindowsHookEx(hCall);
  98. }
  99.