Implementing Custom Hint For Each Panel on a Status Bar

TStatusBar.Panel.HintAs the Help states, The TStatusBar Delphi control displays a row of panels, usually aligned at the bottom of a form. Each panel is represented by a TStatusPanel object listed in the Panels property.

Much like any other VCL control, the TStatusBar can be adopted and extended to offer greater functionality. A simple case would be to use the status bar to display long hints from other controls.

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.

Even though the TStatusBar provides the Hint and ShowHint properties to let you assign the tooltip 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…

Application.OnShowHint for TStatusPanel.Hint

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 on the form, leave the default “ApplicationEvents1” name.
  4. Make sure ShowHint property for the status bar is set to “true”.

Now, handle the OnShowHint as:

procedure TTestForm.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!

3 thoughts on “Implementing Custom Hint For Each Panel on a Status Bar

  1. Andrew Sachs

    Just wanted to express appreciation and say: Really cool. I always learn something new from your postings.

    Unfortunately, when your material was transferred to Thought Co. they really messed up the links between your sequential mini-tutorials.

    Reply
    1. zarkogajic Post author

      HI Andrew, Thanks. Yes, my old articles are hard to locate on Thought Co. I’ll try (re-) posting some timely ones what I can here …

      Reply
  2. Oleg

    For panels of different widths, I added the following lines:

    OffsetRect(r, sbMain.Panels[idx].Width, 0) ;
    =====================================
    if idx<sbMain.Panels.Count-1 then
    r.Right:=r.Left+sbMain.Panels[idx+1].Width;

    Reply

Leave a Reply to Oleg Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.