unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, sysutils, Forms, Controls, Graphics, ExtCtrls, StdCtrls;
type
{ TForm1 }
TForm1 = class(TForm)
Button1: TButton;
RadioGroup1: TRadioGroup;
Shape1: TShape;
procedure MouseInput(Sender: TObject; Msg: Cardinal);
procedure Button1MouseEnter(Sender: TObject);
procedure Button1MouseLeave(Sender: TObject);
procedure FormCreate(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
procedure TForm1.MouseInput(Sender: TObject; Msg: Cardinal);
var
LeftTop, RightBottom: TPoint;
inRange: Boolean;
begin
if not(RadioGroup1.ItemIndex = 0) then Exit;
// Button Position
LeftTop := Point(Button1.Left, Button1.Top);
RightBottom := Point(Button1.Left+Button1.Width, Button1.Top+Button1.Height);
// Convert to screen coordinate
LeftTop := ClientToScreen(LeftTop);
RightBottom := ClientToScreen(RightBottom);
// Is mouse position in the range of button?
with Mouse.CursorPos do
inRange := (X > LeftTop.X) and (X < RightBottom.X) and
(Y > LeftTop.Y) and (Y < RightBottom.Y);
// Change color
case inRange of
True: Shape1.Brush.Color := clGreen;
False: Shape1.Brush.Color := clWhite;
end;
end;
procedure TForm1.Button1MouseEnter(Sender: TObject);
begin
if not(RadioGroup1.ItemIndex = 1) then Exit;
Shape1.Brush.Color := clBlue;
end;
procedure TForm1.Button1MouseLeave(Sender: TObject);
begin
if not(RadioGroup1.ItemIndex = 1) then Exit;
Shape1.Brush.Color := clWhite;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Application.AddOnUserInputHandler(@MouseInput, True);
RadioGroup1.ItemIndex := 0;
end;
end.