delphi2010获取鼠标指向窗口的位置及鼠标在窗口内的相对位置坐标

unit Unit2;

interface

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

type
  TForm2 = class(TForm)
    Timer1: TTimer;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    procedure Timer1Timer(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.dfm}

procedure TForm2.Timer1Timer(Sender: TObject);

var
  p: TPoint;
  h: HWND;
  r:trect;
begin
  GetCursorPos(p);
  h := WindowFromPoint(p);
  GetWindowRect(h,r);//得到窗口的左上角坐标
  label4.Caption:=format('x: %d, y: %d',[r.left,r.top]);
  Windows.ScreenToClient(h, p);
  label1.Caption:=format('x: %d, y: %d',[p.X,p.y]);//鼠标在窗口里的相对位置坐标

end;

end.

http://cy03wqh.blog.163.com/blog/static/367803022010855525783/

你可能感兴趣的:(Delphi)