Delphi/CB
•  Greatis •  Security •  AppDatabase •  Utilities •  Visual Basic • .NET •  just4fun
Delphi / C++ Builder
Components, utilities and tips for Delphi and C++ Builder developers
Hot Offer
Save your money!
Featured
Ultimate Pack  hot!
Image Editor  new!
Runtime Fusion
Form Designer
Object Inspector
Print Suite Pro
Commented Image
Delphi Toys
WinDowse
Delphi Bonus
TMS Scripter Studio
Form Designer VB
Form Designer .NET

...get more...
for Delphi.NET, C#, VB.NET
for Delphi VCL, BCB 3-6

News Tips .NET Software VCL Software Partners  

Hook the mouse
This example shows a creation of a hook for the WM_RBUTTONDOWN and WM_RBUTTONUP messages of the mouse. By the first message we change color of the Panel1 and by second message we change color of the Panel2.

...
type
  TMessageList = class(TList);

var
  Form1: TForm1;
  MessageList: TMessageList = nil;
  MessageBuffer: TEventMsg;
  HookHandle: hHook = 0;
  MessageCount: Word = 0;
  Go: Boolean = False;
  Pan: array[0..5] of TPanel;

implementation

{$R *.DFM}

procedure Stop;
begin
  if Go then UnHookWindowsHookEx(HookHandle);
  MessageList.Free;
  Go:=False;
end;

function FBack(Code: Integer; wParam, lParam: LongInt): LongInt; stdcall;
begin
  Inc(MessageCount);
  Randomize;
  if MessageCount>=MessageList.Count then Stop
  else MessageBuffer:=TEventMsg(MessageList.Items[MessageCount]^);
  Result:=CallNextHookEx(HookHandle, Code, wParam, lParam);
  Pan[MessageCount].Color:=RGB(Random(255), Random(255), Random(255))
end;

procedure SetHook;
begin
  MessageBuffer:=TEventMsg(MessageList.Items[0]^);
  MessageCount:=0;
  HookHandle:=SetWindowsHookEx(WH_MOUSE, FBack, hInstance, 0);
  Go:=True;
end;

procedure MakeMessage(Mes: Cardinal);
var
  MyEvent: PEventMsg;
begin
  New(MyEvent);
  with MyEvent^ do
  begin
    message:=Mes;
    ParamL:=50;
    ParamH:=50;
    Time:=GetTickCount;
    hWnd:=Form1.Handle;
  end;
  MessageList.Add(MyEvent);
end;

function SendMouse: Integer;
begin
  try
    MessageList:=TMessageList.Create;
    MakeMessage(WM_RBUTTONDOWN);
    MakeMessage(WM_RBUTTONUP);
    SetHook;      // set hook
  except
  end;
  Result:=0;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Pan[1]:=panel1;
  Pan[2]:=Panel2;
  SendMouse;
end;

Related chapters
Application

Related topics
Get data from application to Word
Hook the keyboard

For more
Win32 programmer's reference

Download source


Our Partner
DevArchive.com
Recommended
just4fun
Useful Resources
Win32.hlp Online
Unix Manual Pages
MegaDetailed.NET
in3steps.com
Newsletters
Subscribe to our news!    Subscribe to our news!


Greatis Software Greatis | Security | AppDatabase | Utilities | Delphi/CB | Visual Basic | .NET | just4fun

Contacts | Add to Favorites | Recommend to a Friend | Privacy Policy | Copyright © 1998-2008 Greatis Software