delphi Api 函数: GetCursorPos 与转换  
官方Delphi 学习QQ群: 682628230(三千人)
频道

delphi Api 函数: GetCursorPos 与转换


//获取鼠标在窗体中的当前位置
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;
推荐分享
图文皆来源于网络,内容仅做公益性分享,版权归原作者所有,如有侵权请告知删除!
 

Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号

执行时间: 0.052847146987915 seconds