【问题标题】:Set Delphi 5 Form on monitor where previous Form was present in dual monitor system在双监视器系统中存在先前表单的监视器上设置 Delphi 5 Form
【发布时间】:2017-05-08 12:31:12
【问题描述】:

Delphi 5 没有内置功能来在双显示器的情况下在显示器上打开以前的表格时设置表格。为此,我导入了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 = $00000002;
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 } ;

【问题讨论】:

  • 您发布的代码有什么具体问题?翻译 MonitorFromPoint 不费力,那你怎么知道你做不到呢?
  • function MonitorFromPoint(ptScreenCoords: TPoint; dwFlags: DWORD): HMONITOR; stdcall; external user32 真的再简单不过了。你有没有尝试过任何东西?我已经告诉过你很多次了,如果标题翻译对你来说太难了,那么你可以使用 JEDI 库中的翻译。忽略错误处理也是一个糟糕的策略。不要忽略错误。
  • 我不能使用 JEDI 库,因为它不允许在我们的应用程序中使用 3 方库。
  • 哦,看在上帝的份上。阅读那里的代码,然后复制您需要的内容。但是好的,现在我看到你已经做到了。不过你做错了。它返回一个 HMONITOR,而不是一个 HWND。您缺乏错误检查正在杀死您。为什么不尝试学习和理解其中的一些代码。
  • public void Concatenate([MarshalAs(UnmanagedType.IUnknown)] Form f1) //LPWStr { var screen1 = Screen.FromPoint(Cursor.Position); f1.StartPosition = FormStartPosition.Manual; f1.Left = screen1.Bounds.Left + screen1.Bounds.Width / 2 - f1.Width / 2; f1.Top = screen1.Bounds.Top + screen1.Bounds.Height / 2 - f1.Height / 2; } 我在 c# 中尝试过,但无法在 delphi 5 中实现。请帮助@Ken White sir

标签: delphi delphi-5


【解决方案1】:

MonitorFromWindow() 返回 HMONITOR,而不是 HWND。而且您不能像您尝试做的那样将HMONITOR 传递给SetWindowPos()。它只接受HWNDs。要在特定监视器上定位HWND,只需将HWND 移动到virtual screen 的监视器矩形内的坐标。如果您有来自之前的HWNDHMONITOR,请使用GetMonitorInfo() 检索显示器的屏幕矩形,然后您可以使用SetWindowPos() 在该矩形内定位您的目标HWND

阅读 MSDN 了解更多详情:

About Multiple Display Monitors

Positioning Objects on Multiple Display Monitors

【讨论】:

  • 请提供 Monitorfrompoint 的示例代码。我试过但没有成功。 @雷米
  • @user7424581 如果您在做某事时遇到问题,请edit 提出您的问题,说明您已经尝试过什么,并说明您对它的期望以及为什么它不适合您。
  • 实际上,我不知道该怎么做才能在以前打开的表格的适当监视器上采取表格。我知道在哪里调用这些函数,但不知道如何编写它们。请帮助@Remy
【解决方案2】:
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);

【讨论】:

    猜你喜欢
    • 2015-11-09
    • 1970-01-01
    • 2010-11-03
    • 1970-01-01
    • 2022-06-10
    • 2021-11-09
    • 2013-09-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多