Delphi's TStatusBar 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.
The TStatusBar exposes the OnClick event that gets fired when the user clicks the control.
Unfortunatelly, there's no "Panel Click" event to be fired when a particular Status Panel is clicked.
In this article you'll find the code to simulate the OnStatusPanelClick event.
Transforming StatusBar.OnClick into StatusBar.Panel.OnClick
The TStatusBar provides the OnClick event which occurs when the status bar is clicked.If you want to know the index of the Panel that was "clicked", you need to calculate where exactly the mouse click event occured, in terms of the StatusBar coordinates.
Drop a TStatusBar on a form, name it "StatusBar1". Also, drop a TMemo, named "Memo1".
Use the Object Inspector to add several Panels to the StatusBar.
Handle the OnClick of the StatusBar:
//StatusBar1 OnClick
procedure TStatusForm.StatusBar1Click(Sender: TObject) ;
var
mpt : TPoint;
x : integer;
j : integer;
panel : integer;
begin
//no StatusPanels defined
if (StatusBar1.SimplePanel) OR (StatusBar1.Panels.Count = 0) then
begin
Memo1.Lines.Add('Clicked on a StatusBar, no Panels') ;
Exit;
end;
//mouse position in screen coordinates
mpt := Mouse.CursorPos;
//mouse position in StatusBar coordinates
mpt := StatusBar1.ScreenToClient(mpt) ;
panel := -1;
x := 0;
for j := 0 to StatusBar1.Panels.Count - 1 do
begin
x := x + StatusBar1.Panels[j].Width;
if mpt.X < x then
begin
panel := j;
Break;
end;
end;
//clicked "after" the last panel -
//fake it as if the last one was clicked
if panel = -1 then
panel := -1 + StatusBar1.Panels.Count;
Memo1.Lines.Add(Format('Clicked on StatusPanel %d',[panel])) ;
end;
Each Panel in the StatusBar's Panels property has its Width. To get the index of the clicked Panel you need to transforms mouse coordinates, returned by Mouse.CursorPos into StatusBar coordinates using ScreenToClient RTL function.
If Panels are not assigned or if SimplePanel property is True, StatusBar does NOT display Panels.
Whatever the number of the panels is, the last Panel appears to take all the space up until the full width of the StatusBar, even if its Width property is set to a fixed value.
In the above code, we sum the Width of each Panel, until this sum exceeds the X coordinate of the mouse in the StatusBar coordinates.
That's it. Memo1 displays what panel was clicked.

