Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1162
  • Last Modified:

delphi global cursor position

Hi,
I'm trying to understand the code below (which I found on the web).

Its quite simple - a small stayontop form with a Tapplicationevents component and one label.
It works OK but the global  mouse position is constantly being refreshed. I've tried to select only mouse messages to reduce the flickering caused by the the vast number of messages/sec, but the app stops working. If the mouse is stationary, the position is still refreshed. Is there anything I can do about this.
Thanks


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, AppEvnts, ExtCtrls;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    ApplicationEvents1: TApplicationEvents;
    procedure ApplicationEvents1Idle(Sender: TObject; var Done: Boolean);
    procedure ApplicationEvents1Message(var Msg: tagMSG;
      var Handled: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
   
implementation

{$R *.dfm}


procedure TForm1.ApplicationEvents1Idle(Sender: TObject;
  var Done: Boolean);

var
Msg: tagMSG;
Begin
label1.Caption := ' X-' + inttostr(Msg.pt.X) + ' Y-'+ inttostr(Msg.pt.Y);
end;


procedure TForm1.ApplicationEvents1Message(var Msg: tagMSG; var Handled: Boolean);
begin
label1.Caption := ' X-' + inttostr(Msg.pt.X) + ' Y-'+ inttostr(Msg.pt.Y);
end;
end.
0
chrislock
Asked:
chrislock
  • 2
1 Solution
 
jimyXCommented:
If you are trying to get the global mouse position then use this instead:


var
pt:tpoint;
begin
getcursorpos(pt);
label1.Caption := 'The cursor pos. is: x='+inttostr(pt.x)+' y='+inttostr(pt.y);
end;

Open in new window

0
 
cyberkiwiCommented:
You may be getting too many messages because of the Idle event. Remove it and see what happens.
If the mouse is not moving (as in idle), there is no need to change/report it.
0
 
chrislockAuthor Commented:
I've been stupid -  I thought the getcursorpos proc was limited to the Delphi form!
What a lot of time I've wasted!

I've placed this code in a timer event and it works fine - thanks!
0
 
jimyXCommented:
You can place that code in a timer with 50 ms interval.


That code is from:
http://home.x-pack.org/delphi-extact-the-cursor-position-and-how-to-set-the-cursorpos/
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now