Recent

Author Topic: Cursor position.  (Read 243 times)

seghele0

  • Full Member
  • ***
  • Posts: 222
Cursor position.
« on: October 01, 2024, 01:06:38 pm »
I try to place the mouse cursor on Button1, but it doesn't work.
Please help.
 :-[
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ExtCtrls;
  6. type
  7.   { TForm1 }
  8.   TForm1 = class(TForm)
  9.     Button1: TButton;
  10.     Panel1: TPanel;
  11.     procedure FormCreate(Sender: TObject);
  12.   private
  13.   public
  14.   end;
  15. var
  16.   Form1: TForm1;
  17. implementation
  18. {$R *.lfm}
  19. { TForm1 }
  20.  
  21. procedure TForm1.FormCreate(Sender: TObject);
  22. var
  23.     ButtonPosition: TPoint;
  24. begin
  25.   ButtonPosition:= Button1.ClientToScreen(Point(0,0));
  26.   Mouse.CursorPos:= ButtonPosition;
  27. end;
  28. end.
  29.      

Hansvb

  • Hero Member
  • *****
  • Posts: 701
Re: Cursor position.
« Reply #1 on: October 01, 2024, 04:56:18 pm »
hi,

a first attempt (not good enough but works a little)

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3.   pt1 : TPoint;
  4. begin
  5.   pt1.x:= 0;
  6.   pt1.y:= 0;
  7.   pt1:= Button1.Parent.ControlToScreen(pt1);
  8.  
  9.   pt1.X:= pt1.X + Button1.Left + 50;
  10.   pt1.Y:= pt1.Y + Button1.Top + 50;
  11.  
  12.   mouse.CursorPos:= pt1;
  13. end;

seghele0

  • Full Member
  • ***
  • Posts: 222
Re: Cursor position.
« Reply #2 on: October 01, 2024, 05:44:56 pm »
 :)
Hansvb,
It works perfectly.
A sincere thank you for your code.

wp

  • Hero Member
  • *****
  • Posts: 12353
Re: Cursor position.
« Reply #3 on: October 01, 2024, 06:03:09 pm »
I try to place the mouse cursor on Button1,
Sincerely? Software which takes over control of the mouse cursor, moves it to some place, or locks it in some window, will not be loved a lot...

Hansvb

  • Hero Member
  • *****
  • Posts: 701
Re: Cursor position.
« Reply #4 on: October 01, 2024, 06:21:45 pm »
If you work with Windows, you can set the options so that a mouse should snap on the default button. That is indeed not very useful, but I did enjoy figuring out whether it could be made. So far I haven't been able to do it well.

Hansvb

  • Hero Member
  • *****
  • Posts: 701
Re: Cursor position.
« Reply #5 on: October 01, 2024, 06:39:25 pm »
Found it.

I was looking at the wrong place.

@seghele0 this is better then my first attempt:


Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormShow(Sender: TObject);
  2. var
  3.   pt1 : TPoint;
  4. begin
  5.   pt1.x:= 0;
  6.   pt1.y:= 0;
  7.   pt1:= Button1.ControlToScreen(pt1);
  8.   pt1.X:= pt1.X + (Button1.Width div 2);
  9.   pt1.Y:= pt1.Y +(Button1.Height div 2);
  10.   mouse.CursorPos:= pt1;
  11. end;

Sieben

  • Sr. Member
  • ****
  • Posts: 326
Re: Cursor position.
« Reply #6 on: October 01, 2024, 11:51:23 pm »
Or as a one-liner:

Code: Pascal  [Select][+][-]
  1.   Mouse.CursorPos:= Button1.ControlToScreen(Point(Button1.Width div 2, Button1.Height div 2));
« Last Edit: October 01, 2024, 11:56:16 pm by Sieben »
Lazarus 2.2.0, FPC 3.2.2, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

seghele0

  • Full Member
  • ***
  • Posts: 222
Re: Cursor position.
« Reply #7 on: October 02, 2024, 08:17:25 am »
Many thanks to all for the support.
 ;)

 

TinyPortal © 2005-2018