-2

I want the user to be able to set the mouse cursor position over an item in a TDBGrid of their choice at start up.

I have a Popup menu, and CursorPoint is a global TPoint the X and Y of which are saved to an .ini file and loaded at start up. But currently it is doing nothing with the cursor.

On menu popup...

procedure TfrmMain.mnuGridPopup(Sender: TObject);
begin
  Windows.GetCursorPos(CursorPoint);
end;

Then, to test the position I have a TButton

procedure TfrmMain.Button1Click(Sender: TObject);
begin
  Mouse.CursorPos:=ClientToScreen(CursorPoint);
end;

That too, does not move the cursor, so, what am I doing wrong?

1 Answers1

2

When you call Windows.GetCursorPos(CursorPoint); you get mouse cursor position using screen coordinates and not window specific coordinates. But later on you treat theese coordinates as if they were window-specific coordinates ClientToScreen(CursorPoint);. This of course results in you trying to move the mouse cursor to a wrong position.

So when you are saving mouse cursor position do make sure to convert those coordinates into window-specific ones prior to saving them using ScreenToClient() method.

But otherwise, as mentioned by Warren P, not all users would like for your application to move their mouse cursor. Why? Because they won't be able to find it and will eventually do erratic mouse movements to find out where the cursor is. So as soon as they do this your approach loses its purpose.

Wolf
  • 9,679
  • 7
  • 62
  • 108
SilverWarior
  • 7,372
  • 2
  • 16
  • 22
  • Also, Windows has a feature which can automatically move the mouse cursor too for accessibility. If your application did this too, they would interfere if one of your users were to use that feature. – Jerry Dodge Jul 28 '14 at 17:02
  • @Jerry, what specific feature are you talking about ? – TLama Jul 28 '14 at 18:09
  • @TLama It's called "Snap To" in the control panel for the Mouse - Pointer Options tab. It makes the pointer jump to the default control every time the focus changes between windows. – Jerry Dodge Jul 28 '14 at 18:10
  • Whilst the issue of screen vs client coords is valid, the question states that the cursor does not move at all – David Heffernan Jul 28 '14 at 18:17
  • @DavidHeffernan Thanks, that's more the issue. The cursor does not move. Using D5-Ent, CursorPos:= returns "Unknown Identifier" so I have to preface it with Mouse. As to all the "Conclusion-leapers," it is an app that ONLY I use and about 50-times a day at that. And, then 48 of those times, I want the same mouse position. Guys, please just try to answer the questions rather than opine on how much smarter you think you may be than the poster. I have often tried to explain the background to my questions, but invariably it got edited out so I stopped doing that. :) –  Jul 29 '14 at 20:11