http://anony3721.blog.163.com/blog/static/511974201082235754423/

解决Delphi窗体缩放の疑难杂症

2010-09-22 15:57:54|  分类: 默认分类 |  标签:delphi  |举报|字号 订阅

 
 

No.1 Delphi中如何限制窗体大小?

Delphi中可以限制窗体或者元件的大小,只要指定它的Constraints属性即可(默认是0,就是不限制)。但是Delphi这个属性有个很大的缺陷,比如从上往下调整,达到最小后,窗体高度虽然不会缩小了,但是窗体会继续往下移动,这样看起来会有些不正常(Delphi自己的窗体没有这个现象)。要解决这个问题,还是只有截取windows的消息,自己进行处理。

const
  MinWidth = ;
  MinHeight = ;

procedure WMSIZING(var Msg: TMessage); message WM_SIZING;

procedure TfrmMain.WMSIZING(var Msg: TMessage);
begin
  Msg.Result := ;

  //限制最小高度
  if (PRect(Msg.LPARAM)^.Bottom - PRect(Msg.LPARAM)^.Top) < MinHeight then
  begin
    if PRect(Msg.LPARAM)^.Top = Top then
      PRect(Msg.LPARAM)^.Bottom := PRect(Msg.LPARAM)^.Top + MinHeight
    else
      PRect(Msg.LPARAM)^.Top := PRect(Msg.LPARAM)^.Bottom - MinHeight;
  end;

  //限制最小宽度
  if (PRect(Msg.LPARAM)^.Right - PRect(Msg.LPARAM)^.Left) < MinWidth then
  begin
    if PRect(Msg.LPARAM)^.Left = Left then
      PRect(Msg.LPARAM)^.Right := PRect(Msg.LPARAM)^.Left + MinWidth
    else
      PRect(Msg.LPARAM)^.Left := PRect(Msg.LPARAM)^.Right - MinWidth;
  end;
end;

 
No2. Delphi中可视控件随窗口的缩放自动改变大小的方法

找了很久,發現下面的寫法可以實現,在form改變大小時,畫面上的控件的相對位置不變,但控件的大小及控件上的文字大小隨之改變。

unit Unit1;   
interface     
uses 
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
Dialogs, StdCtrls, Buttons;

type 
TForm1 = class(TForm) 
Button1: TButton; 
SpeedButton1: TSpeedButton; 
procedure FormResize(Sender: TObject); 
procedure FormCreate(Sender: TObject); 
private 
{ Private declarations } 
FWidth: Integer; 
public 
{ Public declarations } 
end;

var 
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormResize(Sender: TObject); 
begin 
ScaleBy(Width, FWidth); 
FWidth := Width; 
end;

procedure TForm1.FormCreate(Sender: TObject); 
begin 
FWidth := Width; 
end;

end.

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

代码如下:
unit untFixForm;
interface
uses
Classes, SysUtils, Controls, Forms;
type
TFontedControl = class(TControl)
public
    property Font;
end;
TFontMapping = record
    SWidth : Integer;
    SHeight: Integer;
    FName: string;
    FSize: Integer;
end;
procedure FixForm(AForm: TForm);
procedure SetFontMapping;
var
FontMapping : array of TFontMapping;
implementation
procedure SetFontMapping;
begin
SetLength(FontMapping, 3);
// 800 x 600
FontMapping[0].SWidth := 800;
FontMapping[0].SHeight := 600;
FontMapping[0].FName := '宋体';
FontMapping[0].FSize := 7;
// 1024 x 768
FontMapping[1].SWidth := 1024;
FontMapping[1].SHeight := 768;
FontMapping[1].FName := '宋体';
FontMapping[1].FSize := 9;
// 1280 x 1024
FontMapping[2].SWidth := 1280;
FontMapping[2].SHeight := 1024;
FontMapping[2].FName := '宋体';
FontMapping[2].FSize := 11;
end;
procedure FixForm(AForm: TForm);
var
i, j                 : integer;
t                 : TControl;
begin
with AForm do
begin
    for i := 0 to ComponentCount - 1 do
    begin
      try
        t := TControl(Components[i]);
        t.left := Trunc(t.left * (Screen.width / 1024));
        t.top := Trunc(t.Top * (Screen.Height / 768));
        t.Width := Trunc(t.Width * (Screen.Width / 1024));
        t.Height := Trunc(t.Height * (Screen.Height / 768));
      except
      end; { try }
    end; { for i }
    for i:= 0 to Length(FontMapping) - 1 do
    begin
      if (Screen.Width = FontMapping[i].SWidth) and (Screen.Height = FontMapping[i].SHeight) then
      begin
        for j := 0 to ComponentCount - 1 do
        begin
          try
            TFontedControl(Components[j]).Font.Name := FontMapping[i].FName;
            TFontedControl(Components[j]).FONT.Size := FontMapping[i].FSize;
          except
          end; { try }
        end; { for j }
      end; { if }
    end; { for i }
end; { with }
end;
initialization
SetFontMapping;
end.
SetFontMapping 方法可以自行修改,以适应更多的分辨率。
调用也非常简单,如下所示:
procedure TForm1.FormShow(Sender: TObject);
begin
if MessageBox(Handle,'要使用屏幕自适应吗?','提示',MB_YESNO or MB_ICONINFORMATION) = idno then Exit;
untFixForm.FixForm(Self);
end;

--

Anony
专注的力量成就梦想

最新文章

  1. 《编写高质量代码:改善Java程序的151个建议》笔记
  2. 卸载CentOS 5.4自带的OpenJDK,配置新的Java环境
  3. Pl/sql 导入数据错位问题
  4. Ext入门学习系列(三)复杂自定义窗体
  5. jQuery1.6以上版本prop和attr的区别
  6. 一次oracle大量数据删除经历
  7. USB接口的SmartCard Class协议标准:ICCD and CCID
  8. zTree学习实例
  9. js中键盘按键对应的键值
  10. 挖一挖不常用到而又很实用的重载-Trim
  11. H5_ 表单及其他新增和改良元素
  12. 20145338 《网络对抗》逆向及Bof基础实验
  13. crontab定时任务第一个周期未完成下一个周期执行就来了
  14. es6Math对象新增的方法
  15. Asp.Net上传大文件带进度条swfupload
  16. nyoj 素数距离
  17. juery下拉刷新,ajax请求,div加载更多元素(一)
  18. VirtualBox只能生成32位虚拟机
  19. django2.0实现数据详情页展示的流程
  20. 【c++ primer, 5e】函数指针

热门文章

  1. webpack自定义loader和自定义插件
  2. KFK2060穿越者
  3. [USACO12DEC]第一!First!(字典树,拓扑排序)
  4. openGL坐标系
  5. centos 挂载windows 2003 smb
  6. Django【第22篇】:基于Ajax实现的登录
  7. SPOJ-GSS1-Can you answer these queries 1
  8. [洛谷P3243] 菜肴制作
  9. 关于softmax稳定性问题
  10. 【JavaScript】 命名空间污染解决