Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Library: VCL / WinForms / Controls (https://www.delphipraxis.net/24-library-vcl-winforms-controls/)
-   -   Delphi ListBox mit Scrollbalken (Horizontal) (https://www.delphipraxis.net/50982-listbox-mit-scrollbalken-horizontal.html)

Sharky 3. Aug 2005 19:39


ListBox mit Scrollbalken (Horizontal)
 
Hai,

dieser kleine Code setzt bei einer TListBox einen horizontalen Scrollbalken auf Breite des breitesten Eintrages:

Delphi-Quellcode:
procedure SetLBHScrollbar(aValue: TListBox);
var
  ndx: integer;
  curentWidth: integer;
  MaxWidth: integer;
begin
  MaxWidth := 0;
  for ndx := 0 to aValue.Items.Count - 1 do
  begin
    curentWidth := aValue.Canvas.TextWidth(aValue.Items.Strings[ndx] + 'x');
    if MaxWidth < curentWidth then
    begin
      MaxWidth := curentWidth
    end;
  end;
  SendMessage(aValue.Handle, LB_SETHORIZONTALEXTENT, MaxWidth, 0);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  SetLBHScrollbar(ListBox1);
end;
Das " + 'x' " ist nicht notwendig. Aber dadurch "klebt" der Eintrag nicht so eng am rechten Rand ;-)

The-X 4. Aug 2005 00:27

Re: ListBox mit Scrollbalken (Horizontal)
 
Hat die ListBox nicht sogar ne Property Namens ScrollWidth?
Delphi-Quellcode:
procedure SetScrollWidth(ListBox: TListBox);
var
  DC: HDC;
  Size: TSize;
  w, i, offset: Integer;
begin
  offset := 2 * GetSystemMetrics(SM_CXFRAME) + GetSystemMetrics(SM_CXVSCROLL);
  with ListBox do
  begin
    DC := GetDC(Handle);
    try
      SelectObject(DC, Font.Handle);
      w := 0;
      for i := 0 to Items.Count - 1 do
      begin
        if GetTextExtentPoint32(DC, PChar(Items[i]),
          StrLen(PChar(Items[i])), Size) then
          if w < Size.cx + offset then
            w := Size.cx + offset;
      end;
      ScrollWidth := w;
    finally
      ReleaseDC(Handle, DC);
    end;
  end;
end;
mal irgendwo durch Zufall gefunden (wenn nicht sogar hier)

SirThornberry 3. Sep 2005 18:31

Re: ListBox mit Scrollbalken (Horizontal)
 
das ganze kann man auch noch bischen kürzen und anstelle von "+ x" kann man auch einfach an des die maximale breite später eine feste zahl hängen
Delphi-Quellcode:
uses
  math;
[...]
procedure SetLBHScrollbar(aValue: TListBox);
var
  ndx: integer;
  MaxWidth: integer;
begin
  MaxWidth := 0;
  for ndx := 0 to aValue.Items.Count - 1 do
    MaxWidth := Max(MaxWidth, aValue.Canvas.TextWidth(aValue.Items.Strings[ndx]);
  SendMessage(aValue.Handle, LB_SETHORIZONTALEXTENT, MaxWidth + 10, 0);
end;


Alle Zeitangaben in WEZ +1. Es ist jetzt 14:31 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz