delphi實現窗體組建隨窗體大小改變而改變


在網上查了許多資料去論述如何在dephi中去實現組件隨窗口大小的變化而變化,然都不盡如人意。有人說用組件的align + anchors 這兩個屬性去控制,但是我用了之后,讓我大驚失色。把anchors屬性下面的四個值都變為true.如果用戶用鼠標去拖動窗口,那么窗口上的組件像是中了奪命書生的面目全非腳一樣,基本上就沒有所謂的五官之態。

下面就介紹我的方法,同樣本人的方法也是看了網友們的資料才得以完成的。

(1) 下載TFormResizer組件

我用的是第三方插件TFormResizer,首先下載一個Easysize.rar,(百度一下,應該不難找到) 解壓之后,其文件中內容如下:

Dephi如何實現組件隨窗口大小的改變而改變 - i_love_you_chai - i_love_you_chai的博客

本人建議應該把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 ,出現如下界面:

Dephi如何實現組件隨窗口大小的改變而改變 - i_love_you_chai - i_love_you_chai的博客
在Unit file name 那一行上點擊 Browse按鈕   加載你剛才你解壓的Easysize源文件,完成之后點擊OK按鈕,彈出如下界面:
Dephi如何實現組件隨窗口大小的改變而改變 - i_love_you_chai - i_love_you_chai的博客
點擊Comile按鈕,這樣就在Tag標簽頁上出現www 上出現了FormResizer組件。
4.在form窗口中加如下代碼:
      (1) procedure TForm2.FormCreate(Sender: TObject);
                begin
                            formresizer1.ResizeFonts := true;   //設置字體是否隨窗口大小的改變而改變。
                             FormResizer1.InitializeForm;
                end;
      (2)在form的OnResize事件上增加如下代碼:
               procedure TForm2.FormResize(Sender: TObject);
               begin
                      FormResizer1.ResizeAll;    
               end;
       
自此,窗口中的組件就可以隨窗口大小的改變而改變了。
 
http://blog.163.com/i_love_you_chai/blog/static/1708911082010112105057644/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM