设置 Delphi 5 Form on monitor where previous Form was present in dual monitor system

Set Delphi 5 Form on monitor where previous Form was present in dual monitor system

Delphi 5 没有内置功能,在双显示器的情况下,在打开前一个Form的显示器上设置一个Form。为此,我导入了 windows dll。我搜索了这个并找到了 MonitorFromWindow()MonitorFromPoint().

我实施了 MonitorFromWindow() 但无法实施 MonitorFromPoint()

如何获得显示器并在其上设置我的表单?

function MonitorFromWindow(hwnd: HWND; dwFlags: DWORD): HWND; stdcall; external 'User32.dll';

procedure TSmForm.AfterCreateForm(Session: ISmSession; SmHelpContext: TDM_Int32; IsDLL: Boolean);
type
  HMONITOR = type THandle;
var
  MBMonitor: HMONITOR;
const
  MONITOR_DEFAULTTONEAREST = [=11=]000002;
begin
  //If you decide to remove the next two lines, make sure no one use this function and assume init of SmSession is here (like ScriptMaintanance etc.).
  if SmSession<>Session then
    SmSession := Session;
  if SmHelpContext > 0 then
    HelpContext := SmHelpContext;
  //Following lines ensure that if the form resides in a dll, its icon is the same as the host application's
  if (IsDLL) then
  begin
    if (Icon.Empty) and (ParentHWND <> 0) then
      SendMessage(Handle, WM_SETICON, 1, SendMessage(ParentHWND, WM_GETICON, 1, 0));
  end;

  MBMonitor := MonitorFromWindow(Self.Handle, MONITOR_DEFAULTTONEAREST);

  //HWND_NOTOPMOST
  SetWindowPos(Self.Handle, MBMonitor, 0, 0, 0, 0, SWP_NOMOVE or SWP_NOSIZE);
end { of TSmForm.AfterCreateForm } ;

MonitorFromWindow() returns 一个 HMONITOR,而不是一个 HWND。而且您不能像您尝试的那样将 HMONITOR 传递给 SetWindowPos()。它只接受 HWNDs。要在特定监视器上定位 HWND,只需将 HWND 移动到 virtual screen. If you have an HMONITOR from a previous HWND, use GetMonitorInfo() 的监视器矩形内的坐标以检索监视器的屏幕矩形,然后您可以定位目标HWND 在该矩形内使用 SetWindowPos().

阅读 MSDN 了解更多详情:

About Multiple Display Monitors

Positioning Objects on Multiple Display Monitors

Monitor:=   screen.Forms[Screen.FormCount-1].Monitor;
Self.Left := Monitor.Left + ((Monitor.Width - Self.Width) div 2);
Self.Top :=  Monitor.Top + ((Monitor.Height - Self.Height) div 2);