1. Computing

Display Custom Hints for Status Bar Panels

Implementing TStatusPanel.Hint for Each Panel on a Status Bar

By

TStatusBar.Panel.CustomHint

TStatusBar.Panel.CustomHint

The TStatusBar Delphi control displays a row of panels, usually aligned at the bottom of a form. The Panels property of the TStatusBar control is a collection of TStatusPanel objects.

Much like any other VCL control, the TStatusBar can be adopted and extended to offer greater functionality. You can display custom text and graphics on each of the panels on the status bar; or you can place a progress bar "inside" a status bar panel to provide display progress for some lengthy operation.

What's more you can also implement the OnClick event for a Panel on a TStatusBar.

A Hint for Each Panel?

What does not come out of the box is the possibility to have each panel on a status bar display its own hint (tooltip) when the mouse "stops" over a panel on a status bar.

Even though the TStatusBar provides the Hint and ShowHint properties to let you assign the text which appears when the user moves the mouse over the control - you cannot have each panel on a status bar have its own specific hint!

You cannot? You can! Here's how to implement TStatusPanel.Hint for each panel on a status bar...

Asking for some help from the Application.OnShowHint event

When the mouse pointer moves over a control or menu item that can display a hint, the OnHint event of the TApplicationEvents component gets executed.
The TApplicationEvents component can be used to intercept the events of the global Application object.

When ShowHint property is set to true for a status bar, and the mouse is over the status bar, the TApplicationEvents will fire the OnShowHint event - just before the hint is actually displayed. You can use this event to change the appearance and behavior of the hint.

To actually see how the OnShowHint works, follow the next steps:

  1. Drop a TStatusBar on a Delphi form, leave the default "StatusBar1" control name. Note that it will, by design, align itself to the bottom of the form.
  2. Add some panels to the status bar by operating on the Panels property.
  3. Drop a TApplicationEvents in the form, leave the default "ApplicationEvents1" name.
  4. Make sure ShowHint property for the status bar is set to "true".
  5. Handle the OnShowHint as:
 //Handles ApplicationEvents1.OnShowHint
 procedure TStatusForm.ApplicationEvents1ShowHint(
   var HintStr: string;
   var CanShow: Boolean;
   var HintInfo: THintInfo) ;
 var
   r: TRect;
   idx: integer;
 begin
   if (HintInfo.HintControl = StatusBar1) AND (NOT StatusBar1.SimplePanel) then
   begin
     r := StatusBar1.ClientRect;
     r.Right := StatusBar1.Panels[0].Width;
 
     //locate over what panel the mouse is
     for idx := 0 to -1 + Statusbar1.Panels.Count do
     begin
       if r.Right > HintInfo.CursorPos.X then
       begin
         HintInfo.CursorRect := r;
         //provide custom hint for the panel under the mouse
         HintStr := 'Hint for ' + Statusbar1.Panels[idx].Text;
         Exit;
       end;
       OffsetRect(r, Statusbar1.Panels[idx].Width, 0) ;
     end;
   end;
 end;
 
That's it, now each panel on a status bar displays its own hint!

©2014 About.com. All rights reserved.