8

我的 Delphi-7 应用程序显示:

Screen.DesktopWidth  
Screen.DesktopHeight  
Screen.Monitors[0].Width  
Screen.Monitors[0].Height  

并且,如果选择了第二台显示器,还:

Screen.Monitors[1].Width  
Screen.Monitors[1].Height  

随着应用程序在我的 WinXP-Pro PC 上运行,我转到控制面板/显示/设置,然后更改第二台显示器的设置(添加或删除)。

然后我单击刷新按钮以显示 4 个(或 6 个)参数的新值,并且发生了一些意想不到的事情:Screen.DesktopWidth 和 Screen.DesktopHeight 显示正确的新值,但其他 2 个(或 4 个)的值参数很不对。

像 Screen.Monitors[0].Width = 5586935 一样,它应该是 1680 。

在 Delphi 7 中使用 TScreen 是否有一些特殊规则?

4

3 回答 3

4

由于连接或断开显示器或 USB 显示设备时 TScreen 的刷新问题(错误)而来到这里。@Dave82 的答案对我不起作用。MonitorFromWindow 函数的结果必须返回另一个值(未知/无效值)以强制更新 TScreen 对象。

下面的这个作弊可以解决问题:

确保 multimon 在uses子句中:

uses
 multimon;

将此添加到界面部分(表单)

protected
procedure WMDeviceChange(var Msg: TMessage); message WM_DEVICECHANGE;

将此添加到实现部分(形式)

    function cheatMonitorFromWindow(hWnd: HWND; dwFlags: DWORD): HMONITOR; stdcall;
    begin
      // Does nothing, returns zero to force invalidate
     Result:=0;
    end;

    procedure TForm1.WMDeviceChange(var Msg: TMessage);
    var
     iCurrDisplayCount    : LongInt;
     iNewDisplayCount     : LongInt;
     pMonitorFromWinProc  : TMonitorFromWindow;

    begin
     iCurrDisplayCount:=Screen.MonitorCount;
     // Force monitor update, fix bug in customform, won't update at display change.
     // This a hack/cheat to multimon MonitorFromWindow func, it's fakes the result.
     // This is required to tell customform.getMonitor() to update the TScreen object.
     pMonitorFromWinProc:=MonitorFromWindow;      // Backup pointer to dynamic assigned DLL func  
     MonitorFromWindow:=cheatMonitorFromWindow;   // Assign cheat func 
     monitor;                                     // call the monitor property that calls customform.getMonitor and cheatfunc
     MonitorFromWindow:=pMonitorFromWinProc;      // restore the original func
     // ==========
     iNewDisplayCount:=Screen.MonitorCount;
     if( iCurrDisplayCount <> iNewDisplayCount ) then
     begin
       // Display count change!
     end;  
end;

customform 内部发生了什么(Forms.pas 中的代码)?

function TCustomForm.GetMonitor: TMonitor;
var
  HM: HMonitor;
  I: Integer;
begin
  Result := nil;
  HM := MonitorFromWindow(Handle, MONITOR_DEFAULTTONEAREST);
  for I := 0 to Screen.MonitorCount - 1 do
    if Screen.Monitors[I].Handle = HM then
    begin
      Result := Screen.Monitors[I];
      Exit;
    end;

  //if we get here, the Monitors array has changed, so we need to clear and reinitialize it
  for i := 0 to Screen.MonitorCount-1 do
    TMonitor(Screen.FMonitors[i]).Free;
  Screen.FMonitors.Clear;
  EnumDisplayMonitors(0, nil, @EnumMonitorsProc, LongInt(Screen.FMonitors));
  for I := 0 to Screen.MonitorCount - 1 do
    if Screen.Monitors[I].Handle = HM then
    begin
      Result := Screen.Monitors[I];
      Exit;
    end;    
end;

希望当有人在寻找这个时它会有所帮助。当您想要检测显示设备设置更改(分辨率和方向)时,请改为捕获 WM_DISPLAYCHANGE 事件。

于 2015-09-14T03:23:12.160 回答
1

如果您在程序运行时切换用户,则 Screen.Monitors 数组包含无效值。我们使用这行代码来强制 Screen 对象更新列表:

Screen.MonitorFromWindow(0, mdNull);
于 2014-08-08T12:35:38.507 回答
0

感谢 TLama,我在 Delphi 7 中找到了 TScreen 问题的解决方法。

“导致”问题的原始代码:

LabMon1.Caption := ' Mon 1: ' + IntToStr (Screen.Monitors[0].Width) +
                   ' x ' + IntToStr (Screen.Monitors[0].Height);

if (Screen.MonitorCount = 1)
then LabMon2.Caption := ' Mon 2: -'
else LabMon2.Caption := ' Mon 2: ' + IntToStr (Screen.Monitors[1].Width) +
                        ' x ' + IntToStr (Screen.Monitors[1].Height);

我只需要添加 1 行代码来解决它:

LabMon1.Caption := ' Mon 1: ' + IntToStr (Monitor.Width) +
                   ' x ' + IntToStr (Monitor.Height) ;

LabMon1.Caption := ' Mon 1: ' + IntToStr (Screen.Monitors[0].Width) +
                   ' x ' + IntToStr (Screen.Monitors[0].Height);

if (Screen.MonitorCount = 1)
then LabMon2.Caption := ' Mon 2: -'
else LabMon2.Caption := ' Mon 2: ' + IntToStr (Screen.Monitors[1].Width) +
                        ' x ' + IntToStr (Screen.Monitors[1].Height);

因此,再次感谢 TLama,感谢您对这个问题线程的巨大贡献!

于 2012-06-24T12:50:15.483 回答