Welcome

首页 / 软件开发 / Delphi / Delphi调用WINApi: GetCursorPos与转换

Delphi调用WINApi: GetCursorPos与转换2012-04-02 cnblogs 万一//获取鼠标在窗体中的当前位置

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
str: string;
begin
str := Format("%d,%d",[X,Y]);
ShowMessage(str);
end;

//用 GetCursorPos 获取的是鼠标相对与屏幕的位置

var
ps: TPoint;
str: string;
begin
GetCursorPos(ps);
str := Format("%d,%d",[ps.X,ps.Y]);
ShowMessage(str);
end;

//但通过 ScreenToClient 方法可以转换过来

var
ps: TPoint;
str: string;
begin
GetCursorPos(ps);
ps := ScreenToClient(ps);
str := Format("%d,%d",[ps.X,ps.Y]);
ShowMessage(str);
end;

// ClientToScreen 函数

procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
str: string;
ps: TPoint;
begin
{显示当前鼠标位置, 这是相对于窗体的}
str := Format("%d,%d",[X,Y]);
ShowMessage(str);
{通过 ClientToScreen 函数可以得到当前鼠标相对于屏幕的位置}
ps := point(X,Y);
ps := ClientToScreen(ps);
str := Format("%d,%d",[ps.X, ps.Y]);
ShowMessage(str);
end;