坐标转换 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;

GetCursorPos:得到鼠标当前位置
ScreenToClient:将屏幕坐标转换为用户区域坐标
ClientToScreen:将用户区域坐标转化为屏幕坐标

posted @ 2014-06-20 01:05  Wishmeluck  阅读(1052)  评论(0编辑  收藏  举报