Solved

delphi global cursor position

Posted on 2010-11-17
4
989 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now