Delphi Tips 
-----------------------------

0179  D1   D2   D3   D4   D5   D6   D7   3.1   95   98    作成: 1999/05/01 おばQ rev 1.1
   B1   B3   B4   B5   B6   B7   NT3   NT4   2K   XP  更新: 1999/05/01 おばQ 編集
右の項目の幅が固定されるTStatusBar

Delphiで普通にTStatusBarを使用してスターテスバーを作り
複数のTStatusPanelを設定すると
Formをリサイズする時、右の項目の幅が変化します。

IEエクスプローラやワードパッドなどでは
WindowをResizeした時に
StatusBarの項目の右の方のものが幅が固定されていて
左の項目の幅が変化するものがあります。

推定ですが、VC++アプリケーションウィザードを普通に
用いるとこのようなものが自動で生成されるようです。

これをDelphiで実現します。
StatusBarのResizeイベントに以下のようなコードを書きます。

procedure TForm1.StatusBarResize(Sender: TObject);
const
  ResizePanelNumber=0;    //リサイズするStatusbarのパネル番号を指定
  MinSize=0;              //リサイズされても維持したい最小のWidthを指定
var
  BarWidth,i: Integer;
begin
  with StatusBar do
  begin
    BarWidth := 0;
    for i:=0 to Panels.Count-1 do
    begin
      if not(i=ResizePanelNumber) then
        BarWidth := BarWidth + Panels[i].Width;
    end;

    if (Width-BarWidth)<=MinSize then
      Panels[ResizePanelNumber].Width := MinSize
    else
      Panels[ResizePanelNumber].Width := Width - BarWidth;
  end;
end;

これで項目のうちResizePanelNumberで指定したパネルのWidthが
StatusBarのResize、つまりFormのリサイズに合わせてリサイズします。

また、
すべての項目において右の項目の幅が優先されて維持される
IEブラウザのような動作をさせるには工夫が必要です。

設計時のPanelのWidthを保持して
Panels.Widthを変化させます。

  TForm1 = class(TForm)
    …
  private
    FBarWidths: array of Integer;
  end;

procedure TForm1.FormCreate(Sender: TObject);
    procedure SetStatusBarWidth;  
    var  //右項目固定StatusBarの為に設計時のpanelのWidthを保存
      i: Integer;
    begin
      with StatusBar do
      begin
        SetLength(FBarWidths,Panels.Count);
        for i:=0 to Panels.Count-1 do
          FBarWidths[i]:=  Panels[i].Width;
      end;
    end;

begin
  SetStatusBarWidth;
end;


procedure TForm1.StatusBarResize(Sender: TObject);
var
  BarWidth,i,PointPanel,k: Integer;
begin
  BarWidth:=0;
  with StatusBar do
  begin
    PointPanel := Panels.Count;
    repeat
      dec(PointPanel);    //何番のパネルをリサイズするべきか決定する
      BarWidth := BarWidth + FBarWidths[PointPanel];
    until (Width < BarWidth)or(PointPanel=0) ;

    for i:=0 to PointPanel-1 do  //消えるべきパネルをWidth:=0にする
      Panels[i].Width := 0;

    BarWidth := 0;
    for i:=PointPanel+1 to Panels.Count-1 do
    begin
      Panels[i].Width := FBarWidths[i];
      BarWidth := BarWidth + FBarWidths[i];
    end;
    Panels[PointPanel].Width := StatusBar.Width - BarWidth;
  end;
end;


分かりにくい説明ですが、プログラムを実行すると分かります。
VCL化してみるのも面白いでしょう。
参照: <Win95> <コンポーネント >

[新規作成] [最新の情報に更新]

How To
Lounge
KeyWords


Tips
Delphi
Home
Osamu Takeuchi osamu@big.or.jp