Global Mouse Hook in Delphi
#1
السلام عليكم

وجدت في احدى المدونات كود للاستفادة من الزر الاوسط للماوس في الويندوز ككل.
وهو يعمل بشكل ممتاز بإستثناء بعض البرامج مكتوبة بلغة Visual C++

اولا ملف DLL
كود :
[COLOR=#000000]library MiddleButton; [/COLOR][COLOR=#0000ff]uses[/COLOR][COLOR=#000000] Windows, Messages; [/COLOR][COLOR=#0000ff]const[/COLOR][COLOR=#000000] MemMapFile [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800000]'[/COLOR][COLOR=#800000]Igor_thief[/COLOR][COLOR=#800000]'[/COLOR][COLOR=#000000]; [/COLOR][COLOR=#0000ff]type[/COLOR][COLOR=#000000] PDLLGlobal [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] ^TDLLGlobal; TDLLGlobal [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#0000ff]packed[/COLOR][COLOR=#0000ff]record[/COLOR][COLOR=#000000] HookHandle: HHOOK; [/COLOR][COLOR=#0000ff]end[/COLOR][COLOR=#000000]; [/COLOR][COLOR=#0000ff]var[/COLOR][COLOR=#000000] GlobalData: PDLLGlobal; MMF: THandle; [/COLOR][COLOR=#008000]{[/COLOR][COLOR=#008000]$R *.res[/COLOR][COLOR=#008000]}[/COLOR][COLOR=#000000] [/COLOR][COLOR=#0000ff]function[/COLOR][COLOR=#000000] HookProc(Code: integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall; [/COLOR][COLOR=#0000ff]var[/COLOR][COLOR=#000000] CurrWND: THandle; [/COLOR][COLOR=#0000ff]begin[/COLOR][COLOR=#000000] [/COLOR][COLOR=#0000ff]if[/COLOR][COLOR=#000000] Code [/COLOR][COLOR=#000000]<[/COLOR][COLOR=#800080]0[/COLOR][COLOR=#0000ff]then[/COLOR][COLOR=#000000] [/COLOR][COLOR=#0000ff]begin[/COLOR][COLOR=#000000] Result :[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] CallNextHookEx(GlobalData^.HookHandle, Code, wParam, lParam); exit; [/COLOR][COLOR=#0000ff]end[/COLOR][COLOR=#000000]; [/COLOR][COLOR=#000000]//[/COLOR][COLOR=#0000ff]if[/COLOR][COLOR=#000000] [/COLOR][COLOR=#0000ff]if[/COLOR][COLOR=#000000] (wParam [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] WM_NCMBUTTONDOWN) [/COLOR][COLOR=#0000ff]or[/COLOR][COLOR=#000000] (wParam [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] WM_MBUTTONDOWN) [/COLOR][COLOR=#0000ff]then[/COLOR][COLOR=#000000] [/COLOR][COLOR=#0000ff]begin[/COLOR][COLOR=#000000] CurrWND :[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] PMouseHookStruct(lParam)^.hwnd; CurrWND :[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] GetAncestor(CurrWND, GA_ROOTOWNER); SendMessage(CurrWND, WM_SYSCOMMAND, SC_MINIMIZE, [/COLOR][COLOR=#800080]0[/COLOR][COLOR=#000000]); [/COLOR][COLOR=#0000ff]end[/COLOR][COLOR=#000000]; [/COLOR][COLOR=#000000]//[/COLOR][COLOR=#0000ff]if[/COLOR][COLOR=#000000] Result :[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] CallNextHookEx(GlobalData^.HookHandle, Code, wParam, lParam); [/COLOR][COLOR=#0000ff]end[/COLOR][COLOR=#000000]; [/COLOR][COLOR=#0000ff]procedure[/COLOR][COLOR=#000000] CreateGlobalHeap; [/COLOR][COLOR=#0000ff]begin[/COLOR][COLOR=#000000] MMF:[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] CreateFileMapping(INVALID_HANDLE_VALUE, [/COLOR][COLOR=#0000ff]nil[/COLOR][COLOR=#000000], PAGE_READWRITE, [/COLOR][COLOR=#800080]0[/COLOR][COLOR=#000000], SizeOf(TDLLGlobal), MemMapFile); [/COLOR][COLOR=#0000ff]if[/COLOR][COLOR=#000000] MMF [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800080]0[/COLOR][COLOR=#0000ff]then[/COLOR][COLOR=#0000ff]begin[/COLOR][COLOR=#000000] MessageBox([/COLOR][COLOR=#800080]0[/COLOR][COLOR=#000000], [/COLOR][COLOR=#800000]'[/COLOR][COLOR=#800000]CreateFileMapping -[/COLOR][COLOR=#800000]'[/COLOR][COLOR=#000000], [/COLOR][COLOR=#800000]''[/COLOR][COLOR=#000000], [/COLOR][COLOR=#800080]0[/COLOR][COLOR=#000000]); exit; [/COLOR][COLOR=#0000ff]end[/COLOR][COLOR=#000000]; GlobalData:[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] MapViewOfFile(MMF, FILE_MAP_ALL_ACCESS, [/COLOR][COLOR=#800080]0[/COLOR][COLOR=#000000], [/COLOR][COLOR=#800080]0[/COLOR][COLOR=#000000], SizeOf(TDLLGlobal)); [/COLOR][COLOR=#0000ff]if[/COLOR][COLOR=#000000] GlobalData [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#0000ff]nil[/COLOR][COLOR=#0000ff]then[/COLOR][COLOR=#0000ff]begin[/COLOR][COLOR=#000000] [/COLOR][COLOR=#000000]//[/COLOR][COLOR=#000000] ***1085;***1077; ***1089;***1084;***1086;***1075;***1076;***1080; ***1089;***1086;***1079;***1076;***1072;***1090;***1100; ***1086;***1090;***1086;***1073;***1088;***1072;***1078;***1077;***1085;***1080;***1077; CloseHandle(MMF); MessageBox([/COLOR][COLOR=#800080]0[/COLOR][COLOR=#000000], [/COLOR][COLOR=#800000]'[/COLOR][COLOR=#800000]MapViewOfFile -[/COLOR][COLOR=#800000]'[/COLOR][COLOR=#000000], [/COLOR][COLOR=#800000]''[/COLOR][COLOR=#000000], [/COLOR][COLOR=#800080]0[/COLOR][COLOR=#000000]); [/COLOR][COLOR=#0000ff]end[/COLOR][COLOR=#000000]; [/COLOR][COLOR=#0000ff]end[/COLOR][COLOR=#000000]; [/COLOR][COLOR=#0000ff]procedure[/COLOR][COLOR=#000000] DeleteGlobalHeap; [/COLOR][COLOR=#0000ff]begin[/COLOR][COLOR=#000000] [/COLOR][COLOR=#0000ff]if[/COLOR][COLOR=#000000] GlobalData[/COLOR][COLOR=#000000]<>[/COLOR][COLOR=#0000ff]nil[/COLOR][COLOR=#0000ff]then[/COLOR][COLOR=#000000] UnmapViewOfFile(GlobalData); [/COLOR][COLOR=#0000ff]if[/COLOR][COLOR=#000000] MMF[/COLOR][COLOR=#000000]<>[/COLOR][COLOR=#000000] INVALID_HANDLE_VALUE [/COLOR][COLOR=#0000ff]then[/COLOR][COLOR=#000000] CloseHandle(MMF); [/COLOR][COLOR=#0000ff]end[/COLOR][COLOR=#000000]; [/COLOR][COLOR=#0000ff]procedure[/COLOR][COLOR=#000000] RunHook; stdcall; [/COLOR][COLOR=#0000ff]begin[/COLOR][COLOR=#000000] GlobalData^.HookHandle:[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] SetWindowsHookEx(WH_MOUSE, @HookProc, HInstance, [/COLOR][COLOR=#800080]0[/COLOR][COLOR=#000000]); [/COLOR][COLOR=#0000ff]if[/COLOR][COLOR=#000000] GlobalData^.HookHandle [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] INVALID_HANDLE_VALUE [/COLOR][COLOR=#0000ff]then[/COLOR][COLOR=#000000] [/COLOR][COLOR=#0000ff]begin[/COLOR][COLOR=#000000] MessageBox([/COLOR][COLOR=#800080]0[/COLOR][COLOR=#000000], [/COLOR][COLOR=#800000]'[/COLOR][COLOR=#800000]Error :)[/COLOR][COLOR=#800000]'[/COLOR][COLOR=#000000] , [/COLOR][COLOR=#800000]''[/COLOR][COLOR=#000000] , MB_OK); Exit; [/COLOR][COLOR=#0000ff]end[/COLOR][COLOR=#000000]; [/COLOR][COLOR=#0000ff]end[/COLOR][COLOR=#000000]; [/COLOR][COLOR=#0000ff]procedure[/COLOR][COLOR=#000000] KillHook; stdcall; [/COLOR][COLOR=#0000ff]begin[/COLOR][COLOR=#000000] [/COLOR][COLOR=#0000ff]if[/COLOR][COLOR=#000000] (GlobalData[/COLOR][COLOR=#000000]<>[/COLOR][COLOR=#0000ff]nil[/COLOR][COLOR=#000000]) [/COLOR][COLOR=#0000ff]and[/COLOR][COLOR=#000000] (GlobalData^.HookHandle[/COLOR][COLOR=#000000]<>[/COLOR][COLOR=#000000]INVALID_HANDLE_VALUE) [/COLOR][COLOR=#0000ff]then[/COLOR][COLOR=#000000] UnhookWindowsHookEx(GlobalData^.HookHandle); [/COLOR][COLOR=#0000ff]end[/COLOR][COLOR=#000000]; [/COLOR][COLOR=#0000ff]procedure[/COLOR][COLOR=#000000] DLLEntry(dwReason: DWORD); [/COLOR][COLOR=#0000ff]begin[/COLOR][COLOR=#000000] [/COLOR][COLOR=#0000ff]case[/COLOR][COLOR=#000000] dwReason [/COLOR][COLOR=#0000ff]of[/COLOR][COLOR=#000000] DLL_PROCESS_ATTACH: CreateGlobalHeap; DLL_PROCESS_DETACH: DeleteGlobalHeap; [/COLOR][COLOR=#0000ff]end[/COLOR][COLOR=#000000]; [/COLOR][COLOR=#0000ff]end[/COLOR][COLOR=#000000]; exports KillHook, RunHook; [/COLOR][COLOR=#0000ff]begin[/COLOR][COLOR=#000000] DLLProc:[/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] @DLLEntry; DLLEntry(DLL_PROCESS_ATTACH); [/COLOR][COLOR=#0000ff]end[/COLOR][COLOR=#000000].[/COLOR]
طريقة الاستخددام في دلفي

كود :
[COLOR=#0000ff]unit[/COLOR][COLOR=#000000] RunMiddleButton; [/COLOR][COLOR=#0000ff]interface[/COLOR][COLOR=#000000] [/COLOR][COLOR=#0000ff]uses[/COLOR][COLOR=#000000] Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls; [/COLOR][COLOR=#0000ff]type[/COLOR][COLOR=#000000] TfrmMain [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] class(TForm) btnRunHook: TButton; btnKillHook: TButton; [/COLOR][COLOR=#0000ff]procedure[/COLOR][COLOR=#000000] btnRunHookClick(Sender: TObject); [/COLOR][COLOR=#0000ff]procedure[/COLOR][COLOR=#000000] btnKillHookClick(Sender: TObject); private [/COLOR][COLOR=#008000]{[/COLOR][COLOR=#008000] Private declarations [/COLOR][COLOR=#008000]}[/COLOR][COLOR=#000000] public [/COLOR][COLOR=#008000]{[/COLOR][COLOR=#008000] Public declarations [/COLOR][COLOR=#008000]}[/COLOR][COLOR=#000000] [/COLOR][COLOR=#0000ff]end[/COLOR][COLOR=#000000]; [/COLOR][COLOR=#0000ff]procedure[/COLOR][COLOR=#000000] RunHook; stdcall; external [/COLOR][COLOR=#800000]'[/COLOR][COLOR=#800000]MiddleButton.dll[/COLOR][COLOR=#800000]'[/COLOR][COLOR=#000000] name [/COLOR][COLOR=#800000]'[/COLOR][COLOR=#800000]RunHook[/COLOR][COLOR=#800000]'[/COLOR][COLOR=#000000]; [/COLOR][COLOR=#0000ff]procedure[/COLOR][COLOR=#000000] KillHook; stdcall; external [/COLOR][COLOR=#800000]'[/COLOR][COLOR=#800000]MiddleButton.dll[/COLOR][COLOR=#800000]'[/COLOR][COLOR=#000000] name [/COLOR][COLOR=#800000]'[/COLOR][COLOR=#800000]KillHook[/COLOR][COLOR=#800000]'[/COLOR][COLOR=#000000]; [/COLOR][COLOR=#0000ff]var[/COLOR][COLOR=#000000] frmMain: TfrmMain; [/COLOR][COLOR=#0000ff]implementation[/COLOR][COLOR=#000000] [/COLOR][COLOR=#008000]{[/COLOR][COLOR=#008000]$R *.dfm[/COLOR][COLOR=#008000]}[/COLOR][COLOR=#000000] [/COLOR][COLOR=#0000ff]procedure[/COLOR][COLOR=#000000] TfrmMain.btnRunHookClick(Sender: TObject); [/COLOR][COLOR=#0000ff]begin[/COLOR][COLOR=#000000] RunHook; [/COLOR][COLOR=#0000ff]end[/COLOR][COLOR=#000000]; [/COLOR][COLOR=#0000ff]procedure[/COLOR][COLOR=#000000] TfrmMain.btnKillHookClick(Sender: TObject); [/COLOR][COLOR=#0000ff]begin[/COLOR][COLOR=#000000] KillHook; [/COLOR][COLOR=#0000ff]end[/COLOR][COLOR=#000000]; [/COLOR][COLOR=#0000ff]end[/COLOR][COLOR=#000000]. [/COLOR]

اتمنى ممن لديه فكرة عن كيفية تعديل هذا الكود ليعمل مع كل النوافذ والبرامج
افادتنا
ولكم الشكر
الرد
#2
و عليكم السلام و رحمة الله و بركاته
1- راجع الدالة SetWindowsHookEx , التي تتيح لنا إظافة Hook الى الـ Hook queue
2- البارامتر الأخير DwThreadId ا:

PHP كود :
The identifier of the thread with which the hook procedure is to be  associated. If this parameter is zero, the hook procedure is associated  with all existing threads running in the same desktop as the calling  thread. 
3- مثال بسيط :
PHP كود :
function SetHookUp: Boolean; stdcall;
begin
  CurrentHook 
:= SetWindowsHookEx(WH_KEYBOARD //Hook Type .
    
, @HookProc // Hook Proc Address .
    
, Hinstance //dll Handle .
    
, GetTrgProcessId('Explorer.exe') //if 0 then it is a Global Hook ; No specified Thread !
    
);
  if 
CurrentHook <> 0 then
    Result 
:= True else
    
Result := False;
end; 
بالتوفيق,,
I'M Fly Like A G6

الرد
#3
Hook the Mouse Outside of your application
مثال من موقع delphi.about مشابه:
كود :
http://delphi.about.com/od/windowsshellapi/a/mousehook.htm
في المرفقات.


الملفات المرفقة
.rar   MouseSystemHook.rar (الحجم : 30.54 ك ب / التحميلات : 49)
الرد


التنقل السريع :


يقوم بقرائة الموضوع: بالاضافة الى ( 1 ) ضيف كريم