請教DLL中如何註冊熱鍵(HotKey)? |
尚未結案
|
allen-86
一般會員 ![]() ![]() 發表:28 回覆:44 積分:19 註冊:2008-02-01 發送簡訊給我 |
unit muHotKey; interface uses windows,SysUtils,Messages; var OldHook: HHOOK; //用来保存HOOK的返回值 OldProc: FARPROC; //用来指向窗口消息 hwnd_main:Cardinal; procedure F12; function SetHook: Boolean; stdcall; function HookProc(nCode, wParam, lParam: Integer): Integer; stdcall; function WinProc(Hwnd, Msg, wParam, lParam: longint): LRESULT; stdcall; implementation function GetMainHandle:Cardinal; type PEnumInfo = ^TEnumInfo; TEnumInfo = record ProcessID: DWORD; HWND: THandle; end; function EnumWindowsProc(Wnd: DWORD; var EI: TEnumInfo): BOOL; stdcall; var PID: DWORD; begin GetWindowThreadProcessID(Wnd, @PID); result := (PID <> EI.ProcessID) or (not IsWindowVisible(Wnd)) or (not IsWindowEnabled(Wnd)); if not result then EI.HWND := Wnd; end; function FindMainWindow(PID: DWORD): DWORD; var EI: TEnumInfo; begin EI.ProcessID := PID; EI.HWND := 0; EnumWindows(@EnumWindowsProc, Integer(@EI)); result := EI.HWND; end; var hPID:Cardinal; begin hPID :=GetCurrentProcessId; if hPID <> 0 then result := FindMainWindow(hPID) else result := 0; end; {###################################################################################} //安装HOOK function SetHook: Boolean; stdcall; var Histance: Cardinal; begin hwnd_main:=GetMainHandle; MessageBox (0,PChar(IntToStr(hwnd_main)),'',0); //安装HOOK OldHook := SetWindowsHookEx(WH_KEYBOARD, HookProc, Histance, 0); if (OldHook = 0) then begin MessageBox (0,'hook error','error',0); exit; end else Result := True; end; {###################################################################################} //HOOK回调函数 function HookProc(nCode, wParam, lParam: Integer): Integer; stdcall; var WinStr: HWND; begin //设置热键 if (wParam = VK_F12) then begin //WinStr := FindWindow(nil, '窗口的标题文字'); WinStr :=hwnd_main; OldProc := FARPROC(GetWindowLong(WinStr, GWL_WNDPROC)); SetWindowLong(WinStr, GWL_WNDPROC, Longint(@WinProc)); end; //将HOOK传递给Windows处理 Result := CallNextHookEx(OldHook, nCode, wParam, lParam); end; {###################################################################################} //自定义Windows消息处理函数 function WinProc(Hwnd, Msg, wParam, lParam: longint): LRESULT; stdcall; begin {在这做出对消息的处理 case Msg of WM_ACTIVATEAPP:exit; WM_ACTIVATE:exit; WM_KILLFOCUS:exit; WM_SETFOCUS:exit; end; 上面这些消息是窗口失去焦点和获得焦点的屏蔽 } if Msg=vk_f12 then begin MessageBox (0,'f12','f12',0); end; //将窗口消息传递给Windows处理 Result := CallWindowProc(OldProc, Hwnd, Msg, wParam, lParam); end; procedure F12; var c:Cardinal; procedure dothread; begin Sleep(1000); SetHook; end; begin CloseHandle(BeginThread(nil,1024,@dothread,nil,0,c)); end; end. 但是在 SetWindowsHookEx 的時候就出錯了,請教各位大大。
------
The empty vessels make the greatest sound. |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |