Solved

delphi global cursor position

Posted on 2010-11-17
4
1,013 Views
Last Modified: 2012-05-10
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
Comment
Question by:chrislock
  • 2
4 Comments
 
LVL 24

Accepted Solution

by:
jimyX earned 500 total points
ID: 34157527
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
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 34157610
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
 

Author Closing Comment

by:chrislock
ID: 34157640
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
 
LVL 24

Expert Comment

by:jimyX
ID: 34157641
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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question