2009-02-25 5 views
4

フォーム上のすべてのアンカーコントロールを一時的に移動またはサイズ変更できないようにする方法はありますか?すなわち:Delphi 5:アンカーレイアウトを中断する方法

procedure ScaleFormBy(AForm: TForm; n, d: Integer); 
begin 
    AForm.SuspendAnchors(); 
    try 
     AForm.ScaleBy(n, d); 
    finally 
     AForm.ResumeAnchors(); 
    end; 
end; 

私はきちんとアンカーのコントロールを処理しない

AForm.ScaleBy(m, d); 

を呼んでいるので、私はこれを行う必要があります。 (それは、フォームのエッジオフ制御をアンカー右または上+下+左プッシュ

注:私はアンカーを無効にしたい、ではないアライメント「

答えて

4

ガイは良いアイデアを持っていましたが、子供のコントロールを処理しませんでした。 TPanel、TPageControlなど)

ここには再帰を使用するバリアントがあります。また、私は実際にアンカーを無効にしないことに注意してください - ScaleByはアンカーなしでは動作しません。

だから今、あなたが使用してフォームを拡張することができます

サポートライブラリ関数で
procedure ScaleFormBy(AForm: TForm; M, D: Integer); 
var 
    StoredAnchors: TAnchorsArray; 
begin 
    StoredAnchors := DisableAnchors(AForm); 
    try 
     AForm.ScaleBy(M, D); 
    finally 
     EnableAnchors(AForm, StoredAnchors); 
    end; 
end; 

TAnchorsArray = array of TAnchors; 

function DisableAnchors(ParentControl: TWinControl): TAnchorsArray; 
var 
    StartingIndex: Integer; 
begin 
    StartingIndex := 0; 
    DisableAnchors_Core(ParentControl, Result, StartingIndex); 
end; 

procedure EnableAnchors(ParentControl: TWinControl; aAnchorStorage: TAnchorsArray); 
var 
    StartingIndex: Integer; 
begin 
    StartingIndex := 0; 
    EnableAnchors_Core(ParentControl, aAnchorStorage, StartingIndex); 
end; 

procedure DisableAnchors_Core(ParentControl: TWinControl; var aAnchorStorage: TAnchorsArray; var StartingIndex: Integer); 
var 
    iCounter: integer; 
    ChildControl: TControl; 
begin 
    if (StartingIndex+ParentControl.ControlCount+1) > (Length(aAnchorStorage)) then 
     SetLength(aAnchorStorage, StartingIndex+ParentControl.ControlCount+1); 

    for iCounter := 0 to ParentControl.ControlCount - 1 do 
    begin 
     ChildControl := ParentControl.Controls[iCounter]; 
     aAnchorStorage[StartingIndex] := ChildControl.Anchors; 

     if ([akLeft, akRight ] * ChildControl.Anchors) = [akLeft, akRight] then 
     ChildControl.Anchors := ChildControl.Anchors - [akRight]; 

     if ([akTop, akBottom] * ChildControl.Anchors) = [akTop, akBottom] then 
     ChildControl.Anchors := ChildControl.Anchors - [akBottom]; 

     Inc(StartingIndex); 
    end; 

    //Add children 
    for iCounter := 0 to ParentControl.ControlCount - 1 do 
    begin 
     ChildControl := ParentControl.Controls[iCounter]; 
     if ChildControl is TWinControl then 
     DisableAnchors_Core(TWinControl(ChildControl), aAnchorStorage, StartingIndex); 
    end; 
end; 

procedure EnableAnchors_Core(ParentControl: TWinControl; aAnchorStorage: TAnchorsArray; var StartingIndex: Integer); 
var 
    iCounter: integer; 
    ChildControl: TControl; 
begin 
    for iCounter := 0 to ParentControl.ControlCount - 1 do 
    begin 
     ChildControl := ParentControl.Controls[iCounter]; 
     ChildControl.Anchors := aAnchorStorage[StartingIndex]; 

     Inc(StartingIndex); 
    end; 

    //Restore children 
    for iCounter := 0 to ParentControl.ControlCount - 1 do 
    begin 
     ChildControl := ParentControl.Controls[iCounter]; 
     if ChildControl is TWinControl then 
     EnableAnchors_Core(TWinControl(ChildControl), aAnchorStorage, StartingIndex); 
    end; 
end; 


end; 
+0

Borlandが抱えていた問題を解決するために、 –

6

SuspendAnchorsが基本法のように聞こえるが、私はドンを。トン、それはベースDelphi言語の一部だと思う:)ここ は、トリックを行いますいくつかのコードです:

をお楽しみ


var aAnchorStorage: Array of TAnchors; 
procedure AnchorsDisable(AForm: TForm); 
var 
    iCounter: integer; 
begin 
    SetLength(aAnchorStorage, AForm.ControlCount); 
    for iCounter := 0 to AForm.ControlCount - 1 do begin 
    aAnchorStorage[iCounter] := AForm.Controls[iCounter].Anchors; 
    AForm.Controls[iCounter].Anchors := []; 
    end; 
end; 

procedure AnchorsEnable(AForm: TForm); 
var 
    iCounter: integer; 
begin 
    SetLength(aAnchorStorage, AForm.ControlCount); 
    for iCounter := 0 to AForm.ControlCount - 1 do 
    AForm.Controls[iCounter].Anchors := aAnchorStorage[iCounter]; 
end; 

procedure TForm1.btnAnchorsDisableClick(Sender: TObject); 
begin 
    AnchorsDisable(Self); 
end; 

procedure TForm1.btnAnchorsEnableClick(Sender: TObject); 
begin 
    AnchorsEnable(Self); 
end; 

+0

私が手続きに追加のパラメータによって、グローバル変数を置き換える、またはのいくつかの種類になるだろうTForm参照と保存されたアンカーとの間のマップですが、それ以外は+1です。 – mghie

+0

はい - このコードは本番用に調整する必要があります。私はシンプルなフォームでコードをテストし、スケーリング、サイズと幅の調整に関しては問題なく動作します。 –

+0

ネストされたコントロールは処理されません。つまり、TPanel、TTabSheet –

関連する問題