程序窗体及控件自适应分辨率

本文介绍了一个简单的Delphi程序,用于实现窗体及其控件在不同屏幕分辨率下的自适应布局。通过设置字体大小和位置比例,确保了应用界面的一致性和可用性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
  1. 代码如下:
  2. unit untFixForm;
  3. interface
  4. uses
  5. Classes, SysUtils, Controls, Forms;
  6. type
  7. TFontedControl = class(TControl)
  8. public
  9.     property Font;
  10. end;
  11. TFontMapping = record
  12.     SWidth : Integer;
  13.     SHeight: Integer;
  14.     FName: string;
  15.     FSize: Integer;
  16. end;
  17. procedure FixForm(AForm: TForm);
  18. procedure SetFontMapping;
  19. var
  20. FontMapping : array of TFontMapping;
  21. implementation
  22. procedure SetFontMapping;
  23. begin
  24. SetLength(FontMapping, 3);
  25. // 800 x 600
  26. FontMapping[0].SWidth := 800;
  27. FontMapping[0].SHeight := 600;
  28. FontMapping[0].FName := '宋体';
  29. FontMapping[0].FSize := 7;
  30. // 1024 x 768
  31. FontMapping[1].SWidth := 1024;
  32. FontMapping[1].SHeight := 768;
  33. FontMapping[1].FName := '宋体';
  34. FontMapping[1].FSize := 9;
  35. // 1280 x 1024
  36. FontMapping[2].SWidth := 1280;
  37. FontMapping[2].SHeight := 1024;
  38. FontMapping[2].FName := '宋体';
  39. FontMapping[2].FSize := 11;
  40. end;
  41. procedure FixForm(AForm: TForm);
  42. var
  43. i, j                 : integer;
  44. t                 : TControl;
  45. begin
  46. with AForm do
  47. begin
  48.     for i := 0 to ComponentCount - 1 do
  49.     begin
  50.       try
  51.         t := TControl(Components[i]);
  52.         t.left := Trunc(t.left * (Screen.width / 1024));
  53.         t.top := Trunc(t.Top * (Screen.Height / 768));
  54.         t.Width := Trunc(t.Width * (Screen.Width / 1024));
  55.         t.Height := Trunc(t.Height * (Screen.Height / 768));
  56.       except
  57.       end{ try }
  58.     end{ for i }
  59.     for i:= 0 to Length(FontMapping) - 1 do
  60.     begin
  61.       if (Screen.Width = FontMapping[i].SWidth) and (Screen.Height = FontMapping[i].SHeight) then
  62.       begin
  63.         for j := 0 to ComponentCount - 1 do
  64.         begin
  65.           try
  66.             TFontedControl(Components[j]).Font.Name := FontMapping[i].FName;
  67.             TFontedControl(Components[j]).FONT.Size := FontMapping[i].FSize;
  68.           except
  69.           end{ try }
  70.         end{ for j }
  71.       end{ if }
  72.     end{ for i }
  73. end{ with }
  74. end;
  75. initialization
  76. SetFontMapping;
  77. end.
  78. SetFontMapping 方法可以自行修改,以适应更多的分辨率。
  79. 调用也非常简单,如下所示:
  80. procedure TForm1.FormShow(Sender: TObject);
  81. begin
  82. if MessageBox(Handle,'要使用屏幕自适应吗?','提示',MB_YESNO or MB_ICONINFORMATION) = idno then Exit;
  83. untFixForm.FixForm(Self);
  84. end;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值