này, thực sự là giống như phiên bản TLama đầu tiên của câu trả lời đã xóa của ông, mà tôi thích tốt hơn:
type
TForm1 = class(TForm)
StatusBar1: TStatusBar;
procedure FormResize(Sender: TObject);
private
procedure SetLeftPanelWidth;
..
uses
filectrl, commctrl;
...
procedure TForm1.SetLeftPanelWidth;
var
Borders: array[0..2] of Integer;
PanelWidth, MaxWidth: Integer;
begin
// calculate a little indent on both sides of the text (credit @TLama)
SendMessage(StatusBar1.Handle, SB_GETBORDERS, 0, LPARAM(@Borders));
StatusBar1.Canvas.Font := StatusBar1.Font;
PanelWidth := StatusBar1.Canvas.TextWidth(StatusBar1.Panels[0].Text)
+ 2 * Borders[1] + 2;
// Per Ken's comment, specify a maximum width, otherwise the panel can overgrow
MaxWidth := StatusBar1.Width div 4 * 3; // arbitrary requirement
if PanelWidth > MaxWidth then begin
StatusBar1.Panels[0].Text := MinimizeName(TFileName(StatusBar1.Panels[0].Text),
StatusBar1.Canvas, MaxWidth);
// recalculate
PanelWidth := StatusBar1.Canvas.TextWidth(StatusBar1.Panels[0].Text) +
2 * Borders[1] + 2;
end;
StatusBar1.Panels[0].Width := PanelWidth;
end;
procedure TForm1.FormResize(Sender: TObject);
begin
// have to set the text again since original filename might have been minimized
StatusBar1.Panels[0].Text := ...;
SetLeftPanelWidth;
end;
Trên đây rút ngắn đường dẫn nếu nó không phù hợp với chiều rộng tối đa, nhưng bản gốc tên tệp không hiển thị cho người dùng theo bất kỳ cách nào. Để có thể sử dụng hỗ trợ gợi ý gốc cho bảng thanh trạng thái, chiều rộng của bảng điều khiển phải ngắn hơn văn bản có thể vừa.
Vì vậy, như một sự thay thế, dưới đây truncates phần dấu của tên tập tin khi nó dài hơn chiều rộng tối đa và cho thấy một tooltip khi được quét bằng chuột:
type
TStatusBar = class(comctrls.TStatusBar)
protected
procedure CreateParams(var Params: TCreateParams); override;
end;
TForm1 = class(TForm)
StatusBar1: TStatusBar;
procedure FormResize(Sender: TObject);
private
procedure SetLeftPanelWidth;
..
procedure TStatusBar.CreateParams(var Params: TCreateParams);
begin
inherited;
Params.Style := Params.Style or SBT_TOOLTIPS;
end;
procedure TForm1.SetLeftPanelWidth;
var
Borders: array[0..2] of Integer;
PanelWidth, MaxWidth: Integer;
begin
SendMessage(StatusBar1.Handle, SB_GETBORDERS, 0, LPARAM(@Borders));
StatusBar1.Canvas.Font := StatusBar1.Font;
PanelWidth := StatusBar1.Canvas.TextWidth(StatusBar1.Panels[0].Text)
+ 2 * Borders[1] + 2;
MaxWidth := StatusBar1.Width div 4 * 3; // arbitrary requirement
if PanelWidth > MaxWidth then begin
SendMessage(StatusBar1.Handle, SB_SETTIPTEXT, 0,
NativeInt(PChar(StatusBar1.Panels[0].Text)));
PanelWidth := MaxWidth;
end else
SendMessage(StatusBar1.Handle, SB_SETTIPTEXT, 0, 0);
StatusBar1.Panels[0].Width := PanelWidth;
end;
procedure TForm1.FormResize(Sender: TObject);
begin
SetLeftPanelWidth;
end;
Nguồn
2012-08-25 03:06:22
Bạn cần phải suy nghĩ lại về câu hỏi của bạn. :-) Với ví dụ thứ hai, bảng điều khiển bên trái của bạn rất có thể sẽ vắt ra ngoài cùng bên phải - bạn sẽ làm gì trong trường hợp đó? –
+1 @Ken Vâng, tôi đã nghĩ về điều đó. Tôi làm gì? Tương tự như trong trường hợp kích thước cố định. Nếu tổng số thông tin quá nhiều, tôi sẽ mất một số thứ. Prolly tooltip, tôi đoán – Mawg
là vị trí ngoài cùng bên trái là phải không? Nếu không, làm thế nào về việc sử dụng bảng điều khiển ngoài cùng bên phải? Nó đơn giản hơn, vì bạn không cần phải thay đổi kích thước nó. – Hendra