folgende Prozedure (
AutoSizeStatusbarPanel) passt die Breite eines Panels so an, dass der Text das Panel voll ausfüllt.
Delphi-Quellcode:
procedure AutoSizeStatusbarPanel(sb: TStatusBar; idx:Integer);
var
s : string;
borders : array[0..2] of Integer;
begin
// don't deal with simple panels
if sb.SimplePanel
// don't resize the last panel
or (idx >= sb.Panels.Count-1) then
Exit;
// get the borders of the statusbar
// border[0] = width of the horizontal border
// border[1] = width of the vertical border
// border[2] = width of the border between rectangles
SendMessage(sb.Handle, SB_GETBORDERS, 0, Integer(@borders));
s := sb.Panels[idx].Text;
// calculate the width of the Panel
sb.Panels[idx].Width := TrueFontWidth(sb.Font, s) +
borders[2]*2 +2; // vertical border * 2 + 2 extra Pixels
end;
Dazu benötigt man noch die Funktion
TrueFontWidth:
Delphi-Quellcode:
function TrueFontWidth(fnt: TFont;
const text:
string): Integer;
var
dc: hdc;
tsize : Windows.TSize;
begin
dc := GetDC(0);
SelectObject(
DC, fnt.Handle);
GetTextExtentPoint32(
dc, PChar(text), Length(text), tsize);
ReleaseDC(0,
DC);
Result := tsize.cx;
end;