在網上查了許多資料去論述如何在dephi中去實現組件隨窗口大小的變化而變化,然都不盡如人意。有人說用組件的align + anchors 這兩個屬性去控制,但是我用了之后,讓我大驚失色。把anchors屬性下面的四個值都變為true.如果用戶用鼠標去拖動窗口,那么窗口上的組件像是中了奪命書生的面目全非腳一樣,基本上就沒有所謂的五官之態。
下面就介紹我的方法,同樣本人的方法也是看了網友們的資料才得以完成的。
(1) 下載TFormResizer組件
我用的是第三方插件TFormResizer,首先下載一個Easysize.rar,(百度一下,應該不難找到) 解壓之后,其文件中內容如下:
本人建議應該把Easysite源文件,還有Easysize.d32, Easysize.dcu這三個文件放到你的dephi安裝目錄中的lib目錄下,因為這里面基本上都是dephi的組件。這個組件默認的是標簽不隨窗口大小的改變而改變,其他的組件隨窗口大小的改變而改變。如果想選擇默認的方式,那么就不要修改這個源文件,直接進行安裝這個組件即可。(在這里注意:不管你以前是否設置Label標簽的AutoSize屬性,他都會變成true)。
2.修改Easysize源文件
(1).在TSingleResizer的構造方法里,即在 constructor TSingleResizer.Create(LinkedControl, ParentForm : TControl;IncFont : Boolean;MinFS, MaxFS : Integer)方法里注釋掉最后幾句:
{ But do not adjust width of labels (to avoid
interaction of label auto-sizing with re-sizing) }
if FComponent is TCustomLabel then
FFlexWidth := false;
把上面的這幾句話注釋掉
(2).在procedure TSingleResizer.Resize(OwnerHeight, OwnerWidth; HeightRatio, WidthRatio : Double)方法里在最后幾行里找到:
{ If component is a label, save original AutoSize value,
and set value temporarily to False }
if FComponent is TCustomLabel then
begin
OldAutoSize := GetAutoSizeProperty(FComponent);
SetAutoSizeProperty(FComponent, False);
end;
{ end W.K. insert }
和
{ begin W.K. insert }
{ If component is a label, set AutoSize temporarily to
True, call Refresh, and finally restore original value. }
if FComponent is TCustomLabel then
begin
SetAutoSizeProperty(Fcomponent, True);
TCustomLabel(Fcomponent).Refresh;
SetAutoSizeProperty(FComponent, OldAutoSize);
end;
{ end W.K. insert }
把上面的話用大括號注釋掉。
(3)至此,源文件修改完畢
3.安裝FormResizer這個組:
打開dephi軟件,本人用的是dephi7, 點擊 component ->Install Component ,出現如下界面:


FormResizer1.InitializeForm;
begin
FormResizer1.ResizeAll;
end;