WH_GETMESSAGE, WH_CALLWNDPROC 훅 걸고 사용하는 원형 예제.
대충 DLL에 구겨넣고 쓰면 됨...




// 전역변수
var
  gHotKeyHook, gWndProcHook: HHOOK;


// WM_HOTKEY Hook
function MsgProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
var
  Msg: PMSG;
  ModKey, VK: Integer;
begin
  if nCode=HC_ACTION then begin
    Msg:=PMSG(lParam);
    case Msg^.message of
      // Ctrl + Space
      WM_HOTKEY: begin
        ModKey:=LOWORD(Msg^.lParam);
        VK        :=HIWORD(Msg^.lParam);
        if (ModKey=MOD_CONTROL) and (VK=VK_SPACE) then begin
          OutputDebugString('HotKey Pressed [Ctrl+Space]');
        end;
      end;
    end;
  end;
 
  Result:=CallNextHookEx(gHotKeyHook, nCode, wParam, lParam);
end;

// WM_WINDOWPOSCHANGING Hook
function WndProc(nCode: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
var
  cwps: PCWPSTRUCT;
  WinPos: PWINDOWPOS;
begin
  cwps:=PCWPSTRUCT(lParam);
  case cwps.message of 
    WM_WINDOWPOSCHANGING: begin
      WinPos:=PWINDOWPOS(cwps.lParam);
      OutputDebugString(Format('[WinPos] X=%d : Y=%d (cx=%d, cy=%d)', [WinPos.x, WinPos.y, WinPos.cx, WinPos.cy]));
    end;
  end;

  Result:=CallNextHookEx(gWndProcHook, nCode, wParam, lParam);
end;



// Hook 걸기
gHotKeyHook:=SetWindowsHookEx(WH_GETMESSAGE, @MsgProc, hInstance(알아서..), 알아서..);
gWndProcHook:=SetWindowsHookEx(WH_CALLWNDPROC, @WndProc, hInstance(알아서..), 알아서..);

// Hook 뽑기
UnHookWindowsHookEx(gHotKeyHook);
UnHookWindowsHookEx(gWndProcHook);












Posted by bloodguy
,