且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将工作空间坐标转换为屏幕坐标?

更新时间:2023-02-11 11:32:36

最简单,最干净的方法是使用与 GetWindowPlacement ,即 SetWindowPlacement 。这样,您无需在工作空间和屏幕坐标之间转换,因为您让系统为您完成工作。

The simplest and cleanest way is to use the API function that partners with GetWindowPlacement, namely SetWindowPlacement. That way you don't need to convert between workspace and screen coordinates because you let the system do the work for you.

var
  WindowPlacement: TWindowPlacement;
....
WindowPlacement.length := SizeOf(WindowPlacement);
Win32Check(GetWindowPlacement(Handle, WindowPlacement));
....
Win32Check(SetWindowPlacement(Handle, WindowPlacement));

在上面的代码中,假定为 Handle 成为表单的窗口句柄。

In the above code, Handle is assumed to be the window handle of the form.

如果您一直坚持左上方,则可以像这样恢复它们:

If you have persisted the left and top then you'd restore them like this:

var
  WindowPlacement: TWindowPlacement;
....
WindowPlacement.length := SizeOf(WindowPlacement);
Win32Check(GetWindowPlacement(Handle, WindowPlacement));
WindowPlacement.rcNormalPosition.Left := NewLeft;
WindowPlacement.rcNormalPosition.Top := NewTop;
Win32Check(SetWindowPlacement(Handle, WindowPlacement));