(************************************************************************
*    This File is Part of the AHM Triton Tools                          *
*    for Borland Delphi and C++ Builder.                                *
*************************************************************************
* Copyright © 1995-1999 Alexander H. Mehlhorn (AHM) and Tritontools.com *
* All Rights reserved.                                                  *
*                                                                       *
*    For more Information contact us at                                 *
*    E-Mail : support@tritontools.com                                   *
*    URL : http://www.tritontools.com                                   *
*                                                                       *
*    AHM is copyright by Alexander H. Mehlhorn                          *
*    All Parts of this Collection were developed,                       *
*    designed and implemented by AHM                                    *
*                                                                       *
*    Under no Circumstances may you distribute this file                *
*    in this or any other format. This File is supplied                 *
*    as Evaluation of Source only. No Delphi Components may be developed*
*    using extracts or subsets of this code without expressed permission*
*    by AHM.                                                            *
*                                                                       *
*************************************************************************)
unit AHMTMouseController;

interface

uses Windows,Messages,Classes,Controls,AHMSystemClass,AHMSystemConst,AHMTHookClass;

type
  TAHMMouseController = class(TAHMSystemsComponent)
  private
    FOwner: TWinControl;
    FHookClass : TAHMHookClass;
    procedure Hooked(var AMsg: TMessage;var Handled : Boolean);
  public
    Constructor Create(AOwner : TComponent); override;
    procedure Loaded; override;
    Destructor Destroy; override;
    Procedure FenceAt(ARect : TRect);
    Procedure StopFencing;
    Procedure PositionAt(X,Y : Integer);
    Procedure PositionAtControl(AControl : TControl);
    Procedure FenceAtControl(AControl: TControl);
  published
  end;

implementation
uses Forms;

constructor TAHMMouseController.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  if (AOwner is TWinControl) then
  begin
    FOwner:=AOwner as TWinControl;
    FHookClass:=TAHMHookClass(HookList.Add(Self,AOwner,true));
    FHookClass.BeforeMessage:=Hooked;
  end else raise EAHMOwnerFormNeeded.Create(self.ClassName+ahmscownerform);
end;

destructor TAHMMouseController.Destroy;
begin
  StopFencing;
  HookList.Remove(Self);
  inherited Destroy;
end;

procedure TAHMMouseController.Hooked(var AMsg: TMessage;var Handled : Boolean);
begin
  try
    if AMsg.Msg = WM_CANCELMODE then ClipCursor(nil);
  except
    Application.HandleException(Self);
  end;
end;

Procedure TAHMMouseController.FenceAtControl(AControl : TControl);
begin
  FenceAt(Rect(AControl.ClientOrigin.X,AControl.ClientOrigin.Y,
               AControl.ClientOrigin.X+AControl.Clientrect.Right,
               AControl.ClientOrigin.Y+AControl.ClientRect.Bottom));
end;

Procedure TAHMMouseController.PositionAtControl(AControl : TControl);
begin
  PositionAt(AControl.ClientOrigin.X,AControl.ClientOrigin.Y);
end;

Procedure TAHMMouseController.FenceAt(ARect : TRect);
var MousePoint:TPoint;
begin
  if not(IsRectEmpty(ARect)) then
  begin
    GetCursorPos(MousePoint);
    if not PtInRect(ARect,MousePoint) then SetCursorPos(ARect.Left,ARect.Top);
    ClipCursor(@ARect);
  end;
end;

procedure TAHMMouseController.StopFencing;
begin
  ClipCursor(nil);
end;

Procedure TAHMMouseController.PositionAt(X,Y : Integer);
begin
  SetCursorPos(X,Y);
end;


procedure TAHMMouseController.Loaded;
begin
  inherited Loaded;
  FHookClass.UpdateHooks;
end;

end.
