You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Delphi 11应用双屏切换时最大化窗口显示异常求助

解决Delphi VCL窗体双屏最大化显示异常问题

问题概述

开发Delphi 11双屏应用时,窗体设为wsMaximized状态,点击按钮切换到第二显示器时,仅显示标题栏下部,其余无内容;wsNormal状态下功能正常。当前使用TForm.MakeFullyVisible方法切换显示器。

问题原因

当窗体处于最大化状态时,Windows系统会接管窗体的位置和尺寸管理,直接调用MakeFullyVisible无法正确触发系统对目标显示器最大化状态的适配,导致显示异常。

解决方案

切换显示器时,先将窗体恢复为正常状态,设置其位置到目标显示器的工作区域,再重新设置为最大化状态。具体修改如下:

修改后的代码(.pas)

unit Unit17;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, 
  Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm17 = class(TForm)
    Button1: TButton;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form17: TForm17;

implementation

{$R *.dfm}

var monitorNo :integer = 0;

procedure TForm17.Button1Click(Sender: TObject);
var
  TargetMonitor: TMonitor;
begin
  monitorNo := (monitorNo + 1) mod Screen.MonitorCount;
  Label1.Caption := IntToStr(monitorNo);
  TargetMonitor := Screen.Monitors[monitorNo];

  // 先恢复正常状态,解除系统对最大化窗体的位置锁定
  WindowState := wsNormal;
  // 将窗体定位到目标显示器的工作区域
  BoundsRect := TargetMonitor.WorkareaRect;
  // 重新最大化
  WindowState := wsMaximized;
end;

end.

关键修改说明

  • 先将WindowState设为wsNormal,解除系统对最大化窗体的位置锁定
  • 使用目标显示器的WorkareaRect设置窗体边界,确保窗体处于目标显示器范围内
  • 最后重新设置WindowState为wsMaximized,让系统在目标显示器上正确渲染最大化窗体

补充说明

如果需要保留窗体在正常状态下的原始尺寸,可以在切换前记录BoundsRect,后续若切换回正常状态时再恢复。但针对当前最大化切换的需求,上述代码已足够解决问题。

内容的提问来源于stack exchange,提问作者user3140961

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 11:52:25