Delphi

通过WinAPI GetCursorPos实现鼠标位置的实时显示

有时候我们需要将鼠标的位置实时抓取出来,可以通过如下方式实现。

添加一个Timer控件,执行间隔改为100ms,双击控件输入如下代码:

1 var
2   P: TPoint;
3 begin
4    GetCursorPos(P);
5    RzLabel_Point.Caption := Format('(%d,%d)', [P.X,P.Y]);
6 end;

在窗体Show事件中输入如下代码,调整下鼠标显示样式:

1 Screen.Cursor := crHandPoint;

最终效果:

单元代码如下:

 1 unit U_Operation;
 2 
 3 interface
 4 
 5 uses
 6   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 7   Dialogs, ExtCtrls, RzPanel, StdCtrls, RzLabel;
 8 
 9 type
10   TFrm_Operation = class(TForm)
11     RzGroupBox1: TRzGroupBox;
12     RzGroupBox2: TRzGroupBox;
13     Timer_Display: TTimer;
14     RzLabel1: TRzLabel;
15     RzLabel_Point: TRzLabel;
16     procedure Timer_DisplayTimer(Sender: TObject);
17     procedure FormCreate(Sender: TObject);
18   private
19     { Private declarations }
20   public
21     { Public declarations }
22   end;
23 
24 var
25   Frm_Operation: TFrm_Operation;
26 
27 implementation
28 
29 {$R *.dfm}
30 
31 procedure TFrm_Operation.Timer_DisplayTimer(Sender: TObject);
32 var
33   P: TPoint;
34 begin
35    GetCursorPos(P);
36    RzLabel_Point.Caption := Format('(%d,%d)', [P.X,P.Y]);
37 end;
38 
39 procedure TFrm_Operation.FormCreate(Sender: TObject);
40 begin
41    Screen.Cursor := crHandPoint;
42 end;
43 
44 end.

  作者:Jeremy.Wu
  出处:https://www.cnblogs.com/jeremywucnblog/
  本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

原文地址:https://www.cnblogs.com/jeremywucnblog/p/11433069.html