![](/img/trans.png)
[英]How to adjust visual components that don't scale correctly in a high DPI aware Windows application made in Delphi?
[英]How can I get visual inheritance working at high dpi values?
我有以下帶有兩個按鈕( BitBtn
)的TForm1
表單示例:
另外,我還有第二種形式TForm2
,它是從第一種形式衍生而來的。 第二個按鈕移到左側,並添加另一個按鈕:
在運行時(Windows 7),第二種形式如下所示:
如果將字體縮放比例更改為125%,則我的表單如下所示:
不知何故,新按鈕的位置和大小都不正確。 我該怎么辦?
我使用了以下.dfm
文件(已縮短):
object Form1: TForm1
Left = 0
Top = 0
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object BitBtn1: TBitBtn
Left = 8
Top = 8
Width = 105
Height = 105
Caption = 'BitBtn1'
end
object BitBtn2: TBitBtn
Left = 359
Top = 8
Width = 105
Height = 105
Caption = 'BitBtn2'
end
end
inherited Form2: TForm2
Caption = 'Form2'
PixelsPerInch = 96
TextHeight = 13
inherited BitBtn2: TBitBtn
Left = 191
Top = 7
ExplicitLeft = 191
ExplicitTop = 7
end
object BitBtn3: TBitBtn
Left = 359
Top = 8
Width = 105
Height = 104
Caption = 'BitBtn3'
end
end
在設計器生成類文件時,它們保持不變。 它們不包含任何相關代碼。
我自己找到了一種可能的解決方案:必須用以下代碼覆蓋ReadState
:
procedure TForm2.ReadState(Reader: TReader);
begin
IntPtr(FReserved) := 0;
inherited;
end;
這看起來很奇怪。 它是如何工作的?
我查看了VCL
源代碼,發現在TForm.ReadState(...)
完成表單加載后的縮放。 Apparrently,被保護的字段FReserved
的TControl
用於存儲當前應用的DPI
值:
dfm
所有組件 DPI
值存儲在FReserved
dfm
文件再次調用ReadState
FReserved
中的值,縮放不應用於導出形式的組件 因此,一種想法是在讀取組件之前重置緩存的DPI
值。 它對Form1
的組件進行兩次縮放,但這並沒有什么壞處,因為原始大小和位置被記住為縮放的基本值,並且結果完全相同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.