Delphi 窗体函数GetWindowRect 取窗口矩形坐标

GetWindowRect,用于取窗口矩形坐标。返回值类型:布尔型(LongBool)。执行成功返回真(True),否则返回假(False);参数1类型:整数型(HWND),目标窗口的窗口句柄;参数2类型:坐标结构(RECT),目标窗口的坐标结构地址。在Windows SDK中的函数原型:

BOOL GetWindowRect(

HWND hWnd, // handle of window 参数1:目标窗口句柄

LPRECT lpRect // address of structure for window coordinates 参数2:窗口的坐标结构地址

);

RECT定义:

typedef struct _RECT { // rc

LONG left; //桌面窗口到目标窗口的左边距

LONG top; //桌面窗口到目标窗口的顶边距

LONG right; //桌面窗口到目标窗口的右边距

LONG bottom; //桌面窗口到目标窗口的底边距

} RECT;

例如:

var
  R: TRect;
begin
  GetWindowRect(Form1的句柄, R);
  ShowMessageFmt('宽: %d, 高: %d, 横坐标: %d, 纵坐标: %d', [R.Right-R.Left,
    R.Bottom-R.Top,R.Left,R.Top]);
end;
var
  hwnd:HWND;
  R:TRect;
begin
  hwnd:=FindWindow('SciCalc','计算器');
  GetWindowRect(hwnd,R);
  lable1.Caption := IntToStr(R.Left)+','+ IntToStr(R.Top)+'/'+
  IntToStr(R.Right)+ ','+ IntToStr(R.Bottom );
end;