[返回]
中国计算机报1999年第41期

Delphi中状态条构件的妙用

胡晓鹰

  TStatusBar构件的主要属性


  实际使用过程中,我们常用的只有一个:Panels属性。这个属性本身是一个TStatusPanels对象,而在一个状态条可以有很多个TStatusPanels对象。在这个TStatusPanels对象中,我们经常使用的是其中的Text和Style属性。比如:

  StatusBar1.Panels[0].Text := ′这是显示在状态条中的Hint′;

  表示显示在StatusBar1中的Panels[0]中的字符串

  要创建状态条构件的TStatusPanels对象相当简单:

  (1)双击状态条构件,Delphi4会自动弹出Editing StatusBar1 Panels对话框。

  (2)在这个对话框中用鼠标单击左上角的Add New按钮或者使用Insert键就可以向StatusBar中增加TStatusPanels对象了。

  (3)任意选择一个TStatusPanels对象,在属性观察器(Object Inspector)中可以设置每个TStatusPanels对象的属性。


  显示当前选择的选单项的有关信息


  我们希望用户把鼠标移动到某个选单项的时候,自动在状态条中显示关于这个选单的功能、使用注意事项等。

 (1)在Form上放置一个TStatusBar构件,设置其Name为StatusBar1。

 (2)在Form上放置一个TMainMenu构件,设置其Items属性,并且设置选单中的每个TMenuItem对象的Hint属性。

   (3)增加以下代码:

   procedure TForm1.FormCreate(Sender: TObject);

   begin

   Application.ShowHint := True;

   Application.OnHint := DoShowHint; //指定显示系统Hint的过程

   end;

   procedure TForm1.DoShowHint(Sender : TObject);

   begin

     StatusBar1.Panels[0].Text := Application.Hint; //显示应用程序的提示

   end;


  显示锁定键的状态


   为了实时显示锁定键盘的当前状态,我们需要一个TTimer构件,每隔一定的时间检测锁定键的当前状态,并把检测出来的结果显示在状态条中。 

   procedure TForm1.FormCreate(Sender: TObject);

   begin

   Timer1.Enabled := True;

   //注意:在本例子中Timer1的Interval属性不能设置的过大,否则容易造成检测锁定键当前状态的延迟

   Timer1.Interval := 10;

   end;

   procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;Shift: TShiftState);

   begin

      CheckKeyBoard;

   end;

   procedure TForm1.CheckKeyBoard; //显示锁定键盘的当前状态

   begin

     If Odd(GetKeyState(VK—INSERT)) then

    StatusBar1.Panels[0].Text := ′改写′

  else

    StatusBar1.Panels[0].Text := ′插入′;

  If Odd(GetKeyState(VK—CAPITAL)) then

    StatusBar1.Panels[1].Text := ′大写′

  else

    StatusBar1.Panels[1].Text := ′小写′

   end;

   procedure TForm1.Timer1Timer(Sender: TObject);

   begin

      CheckKeyBoard;

   end;


  特殊显示效果的实现


   除了以上两种常见的效果以外,我们还可以利用TStatusBar构件的OnDrawPanel事件在状态条中实现特殊效果的显示。OnDrawPanel事件实际上叫做“状态条的自画”事件。很多Delphi的构件都有类似的自画事件。几乎所有使用通常的属性、方法、事件不能达到的效果,我们都可以通过“自画”事件来实现。

   例子1:在状态条中显示一个TProgressBar(进度条构件)。实现的步骤如下:

   (1)在Form上放置TStatusBar、TProgressBar、TTimer构件,设置它们的Name属性为StatusBar1、ProgressBar1、Timer1。

   (2)为StatusBar1创建三个TStatusPanel对象。

   (3)设置第2个TStatusPanel对象的Style := psOwnerDraw。

   (4)设置ProgressBar1的Max、Min、Position属性分别为100、0、0。

   (5)在FormCreate中增加以下代码:

   procedure TForm1.FormCreate(Sender: TObject); //确定进度条的初始位置

   begin

   StatusBar1.Panels.Items[1].Width := Progressbar1.Width ;

   StatusBar1.Panels.Items[0].Width := (ClientWidth - StatusBar1.Panels.Items[1].Width) - 10 ;

   ProgressBar1.Parent:=StatusBar1 ;

   end;

   (6)在StatusBar1的OnDrawPanel事件中增加以下的代码:

   procedure TForm1.StatusBar1DrawPanel(StatusBar: TStatusBar;Panel: TStatusPanel; const Rect: TRect);

   begin

   ProgressBar1.BoundsRect := Rect ;

   end;

   (7)处理FormResize事件:

   procedure TForm1.FormResize(Sender: TObject);

   begin //当Form的大小发生变化的时候,改变状态条的宽度

   StatusBar1.Panels.Items[0].Width := (ClientWidth -StatusBar1.Panels.Items[1].Width) - 10 ;

   end;

   (8)处理Timer1的OnTimer事件(在状态条构件中动态演示进度条的变化)

   procedure TForm1.Timer1Timer(Sender: TObject);

   begin

   ProgressBar1.Position := ProgressBar1.Position + 1;

   if ProgressBar1.Position >= 100 then

   ProgressBar1.Position := 0;

   end;

   当然,本效果只是为了演示,你如果有兴趣的话,可以把这个效果用在诸如复制文件的过程中,一定比较好看!

   例子2:在状态条中显示带有颜色的字符串。当我们要把一些比较重要的信息显示在状态条的时候,通常总是希望此时的提示信息用其它的颜色来表示,来引起用户的注意。可是状态条构件中并没有可以设置显示在状态条中的文本颜色的属性。但这个效果可以通过“自画”事件来实现。具体步骤如下:

   (1)在Form上放置一个TStatusBar,设置其Name为StatusBar1。

   (2)为StatusBar1中创建三个TStatusPanel对象。

   (3)在对话框中增加三个TStatusPanel对象,分别设置它们的属性为:StatusPanel对象、Style属性、Text属性。第一个TStatusPanel对象的psOwnerDraw通过TStausBar的自画实现显示红色;第二个TStatusPanel对象的psOwnerDraw实现显示绿色;第三个TStatusPanel对象为psText,显示缺省颜色。

   (4)在StatusBar1的OnDrawPanel事件中加入以下的代码:

   procedure TForm1.StatusBar1DrawPanel(StatusBar: TStatusBar;Panel: TStatusPanel; const Rect: TRect);

   begin

   with StatusBar1 do

   begin

   if Panel = Panels[0] then

   begin

   Canvas.Font.Color := clRed; //第1个提示红色显示

   Canvas.TextOut(Rect.Left, Rect.Top, Panels[0].Text)

   end

   else if Panel = Panels[1] then

   begin

   Canvas.Font.Color := clGreen; //第2个提示绿色显示

   Canvas.TextOut(Rect.Left, Rect.Top,Panels[1].Text);

   end;

   end;

   end;