Re 2: Limiting mouse cursor movement

Posted by webmaster Guido on February 19, 2007

In Reply to Re: Limiting mouse cursor movement posted by Graham J Hadlington on February 17, 2007

I wish to use a round rectangle has it represents something I need in the application. As there is no OnClick event, should I use a MouseMove event instead?

It's best to use the OnMouseDown event of the TShape component, your round rectangle. Below is an example of the source code:

var
  SavedColor: TColor;
 
procedure TForm1.Shape1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if not Timer1.Enabled then begin
    Timer1.Interval := 1000 + Random(5000);
    Timer1.Enabled := True;
    SavedColor := Shape1.Brush.Color; // save current color
    Shape1.Brush.Color := clRed;
  end;
end;
 
procedure TForm1.Timer1Timer(Sender: TObject);
begin
  Timer1.Enabled := False;
  Shape1.Brush.Color := SavedColor; // restore previous color
end;
 
procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
var
  MPClient: TPoint; // mouse-cursor position X and Y in CLIENT coordinates
begin
  if Timer1.Enabled then begin
    MPClient := ScreenToClient(Mouse.CursorPos);
    with Shape1 do begin
      if X  Left + Width then MPClient.X := Left + Width - 2;
      if Y  Top + Height then MPClient.Y := Top + Height - 2;
    end;
    Mouse.CursorPos := ClientToScreen(MPClient);
  end;
end;
Good luck!
Guido

Related articles

       

Follow Ups