Delphi - 在第二台显示器上打开表格

时间:2016-12-20 10:09:33

标签: forms delphi multiple-monitors

我需要这个解决方案的帮助。 有几种形式的应用程序。需要在选定的监视器上打开其中一个表单。例如: 解决方案1.如果使用多个监视器,则OnCreate表单,并在最后一个监视器上打开。我试试这段代码,但没有运气:

"GPA.1234-5678-9012-34567" (base order number)
"GPA.1234-5678-9012-34567..0" (first recurrence orderID)
"GPA.1234-5678-9012-34567..1" (second recurrence orderID)
"GPA.1234-5678-9012-34567..2" (third recurrence orderID)

解决方案2.将表单拖动到选定的监视器,并将OnDestroy事件的Top和Left位置写入INI文件。下次表格在同一台显示器和相同位置打开。我试试这段代码,但也没有运气:

  Application.CreateForm(TfrmDashboard, frmDashboard);
  for I := 0 to Screen.MonitorCount -1 do
  begin
    // Checking Screen Position
    ShowMessageFmt('%d, %d, %d, %d',
              [Screen.Monitors[i].BoundsRect.Left,
              Screen.Monitors[i].BoundsRect.Top,
              Screen.Monitors[i].BoundsRect.Right,
              Screen.Monitors[i].BoundsRect.Bottom]);
  end;

  if Screen.MonitorCount > 1 then
  begin
      frmDashboard.Top:= Screen.Monitors[i].BoundsRect.Top;
      frmDashboard.Top:= Screen.Monitors[i].BoundsRect.Left;
  end;

1 个答案:

答案 0 :(得分:5)

frmDashboard.Top:= ...
frmDashboard.Top:= ...

这似乎是一个简单的复制粘贴错误。您设置Top两次。大概你的意思是:

frmDashboard.Top:= ...
frmDashboard.Left:= ...

这段代码犯了同样的错误:

if Screen.MonitorCount > 1 then
begin
  frmDashboard.Top:= Screen.Monitors[i].BoundsRect.Top;
  frmDashboard.Top:= Screen.Monitors[i].BoundsRect.Left;
end;

此外,当i定义不明确时,它会引用OnCreate。编译器会警告这一点。我希望你启用编译器警告和提示,并注意它们。

如果INI文件包含无效数据,则您的StrToInt事件处理程序将引发异常。例如,如果用户将位置值编辑为非数字,则OnCreate将引发异常。你的程序应该具有弹性。

OnDestroyStrToInt事件处理程序都无法正确管理INI文件对象的生命周期。如果INI文件访问失败,或者对obj := TMyObject.Create; try // do things with obj finally obj.Free; end; 的调用失败(参见上文),那么您将泄漏该对象。这是要遵循的模式:

newIncome
相关问题