Winforms 如何用scale dpi修复delphi 7中的错误?

Winforms 如何用scale dpi修复delphi 7中的错误?,winforms,delphi-7,dpi,Winforms,Delphi 7,Dpi,请使用scale dpi帮助修复delphi 7中的错误。 在这个示例中,我使用带有锚点的TButton:=[akRight],您可以看到,如果窗口设置为125 dpi缩放模式,则按钮文本溢出。 我准备了一个演示示例: 1) 默认比例 2) 大规模(如您所见,按钮2已消失) 源代码: 单元1.pas unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Control

请使用scale dpi帮助修复delphi 7中的错误。 在这个示例中,我使用带有锚点的TButton:=[akRight],您可以看到,如果窗口设置为125 dpi缩放模式,则按钮文本溢出。

我准备了一个演示示例:

1) 默认比例

2) 大规模(如您所见,按钮2已消失)

源代码:

单元1.pas

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Panel2: TPanel;
    Button1: TButton;
    Button2: TButton;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

end.
单元1.dfm

object Form1: TForm1
  Left = 488
  Top = 196
  Width = 720
  Height = 511
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Panel1: TPanel
    Left = 0
    Top = 432
    Width = 704
    Height = 41
    Align = alBottom
    Caption = 'Panel1'
    TabOrder = 0
    DesignSize = (
      704
      41)
    object Button1: TButton
      Left = 12
      Top = 8
      Width = 75
      Height = 25
      Caption = 'Button1'
      TabOrder = 0
    end
    object Button2: TButton
      Left = 616
      Top = 8
      Width = 75
      Height = 25
      Anchors = [akTop, akRight] 
      Caption = 'Button2'
      TabOrder = 1
    end
  end
  object Panel2: TPanel
    Left = 0
    Top = 0
    Width = 704
    Height = 432
    Align = alClient
    Caption = 'Panel2'
    TabOrder = 1
  end
end

我找到了一个解决方案:检测所有TControl并重新定位它们

const
 DEFAULT_DPI = 97;

function TFMain.ScaleDPI(Value: Integer) : Integer;
begin
   Result:=Ceil(Value*Screen.PixelsPerInch/DEFAULT_DPI);
end;

var i, n       : integer;
    CompFix    : array of TAnchors;

with IsForm do begin
   SetLength(CompFix, ComponentCount);
   if ( Screen.PixelsPerInch > DEFAULT_DPI ) and Scaled then
      if ( (BorderStyle = bsSizeable) or (BorderStyle = bsSizeToolWin) ) then begin
         for i:=0 to ComponentCount-1 do
            if Components[i] is TControl then
               if ( akRight in (Components[i] as TControl).Anchors ) or
                  ( akBottom in (Components[i] as TControl).Anchors ) then begin
                     CompFix[i]:=(Components[i] as TControl).Anchors;
                     (Components[i] as TControl).Anchors:=(Components[i] as TControl).Anchors - [akRight] - [akBottom] + [akLeft] + [akTop];
                  end else CompFix[i]:=[];
         ClientWidth:=ScaleDPI(ClientWidth);
         ClientHeight:=ScaleDPI(ClientHeight);
         for i:=0 to ComponentCount-1 do
            if CompFix[i] <> [] then begin
               (Components[i] as TControl).Anchors:=CompFix[i];
               end;
         end;
   end;
const
默认值_DPI=97;
函数TFMain.ScaleDPI(值:整数):整数;
开始
结果:=Ceil(值*Screen.PixelsPerInch/DEFAULT\u DPI);
终止
变量i,n:整数;
CompFix:tanchor数组;
用IsForm开始
设置长度(CompFix、ComponentCount);
如果(Screen.PixelsPerInch>DEFAULT_DPI)并缩放,则
如果((BorderStyle=bsSizeToolWin)或(BorderStyle=bsSizeToolWin)),则开始
对于i:=0到ComponentCount-1 do
如果组件[i]为t控制,则
如果(在(组件[i]作为TControl.Anchors)中正确定位)或
(akBottom in(组件[i]作为TControl)。锚定)然后开始
CompFix[i]:=(组件[i]作为TControl)。锚定;
(组件[i]作为TControl)。锚定:=(组件[i]作为TControl)。锚定-[akRight]-[akBottom]+[akLeft]+[akTop];
end else CompFix[i]:=[];
ClientWidth:=ScaleDPI(ClientWidth);
ClientHeight:=ScaleDPI(ClientHeight);
对于i:=0到ComponentCount-1 do
如果CompFix[i][],则开始
(组件[i]作为控制)。锚定:=CompFix[i];
终止
终止
终止

Delphi 7不支持DPI。15年后,10.2版东京版仍然无法很好地处理DPI。即将发布的10.3将有更多的DPI补丁。这不是Delphi7中的错误。如果Delphi7发布时不存在某些东西,那么它不支持它也不是一个bug。如果你买了一辆1957年的雪佛兰,而且它没有GPS装置,或者你的手机不支持蓝牙,那就不是一个bug。您使用的是一个有15年以上历史的Delphi版本。不要惊讶,它不适用于新的操作系统功能。如果您希望更好地支持现代操作系统,请升级到更现代的Delphi版本。如果您不想这样做,您将需要忍受对这些功能的支持不足。@Ken嗯,在Delphi 7上支持新的Windows功能是可能的,您只需要做额外的工作。我知道我当时支持delphi 6的高DPI。但是它不是免费的。考虑到以上所有因素,我认为你所描述的内容不容易复制。你们能给我一个答案吗?好的,伙计们。这不是德尔福的错误。当向上兼容不起作用时,这就是生活的缺陷。我有一个非常好的项目(稳定工作),写了15年,迁移到新的delphi将太昂贵。为什么呢?仅用于显示按钮?谁将为这项艰苦的工作买单?哈哈