Board index » delphi » Help about Listbox and Horizontal ScrollBar yet

Help about Listbox and Horizontal ScrollBar yet

Yorai Aminov send me:

Quote
>You create a TListBox descendant to do this:
>type
>  TMyListBox = class(TListBox)
>  private
>    procedure WMHScroll(var Message: TMessage); message WM_HSCROLL;
>  end;
>...
>procedure TMyListBox.WMHScroll(var Message: TMessage);
>begin
>  inherited;
>  Do_Whatever_You_Want; { What I must make here }
>end;

and Peter Below said me:

Quote

>  Var
>    scrollinfo: TScrollInfo;

>  SendMessage(Listbox1.Handle, LB_SETHORIZONTALEXTENT, 700, Longint(0));
>  FillChar( scrollinfo, sizeof(scrollinfo),0);
>  With scrollinfo Do begin
>    cbSize := Sizeof( scrollinfo );
>    fMask := SIF_PAGE;
>   nPage := 200;
> End;
>  SetscrollInfo( listbox1.handle, SB_HORZ, scrollinfo, true )

Well, which I want it is really to define the amount that should be rolled
in the horizontal bar of a Listbox. How do I make this? How i can use the
Peter's code?

Do u could help me?

Thanks, Marcos

 

Re:Help about Listbox and Horizontal ScrollBar yet


Marcos,

Here is code that we implemented in the normal units of our programs to
force a horizontal scrollbar into a listbox.  To make it work, you must
call the ??? routine directly in code when you want the scroll bar.

You can create a custom component as has been suggested in the previous
posts you cited and encorporate this code within.  Note that the ???
routine is the one that determines the width of the scrollbar, per your
question.

The Delphi Super Page at http://ftp.cdrom.com/pub/delphi/index.htm
contains an extended custom listbox component which implements this
behavior.  I've not tested it so I don't know its performance.  Click on
"Delphi 3" icon on the left side.  Scroll the upper panel until
"Inproved Components" comes into view and click it.  Scroll the lower
panel until "hslistbx.zip" comes into view.  It is provided as a source
file only which you will have to install into your component pallet
manually.

Also note that the custom component available from the Delphi Super Page
uses a different technique for determining when and how wide the
scrollbar should be.  It is probably an improvement over the code I've
provided below.

{ ============================================================== }
{ === Set Horizontal Scroll Bars On Listbox Objects === }
{ ============================================================== }

{ not sure what units need to be listed in the 'uses' clause
  other than WinProcs and WinTypes for Delphi 3 }

{ ============================================================== }
function GetTextWidth(CanvasOwner: TListBox; Text : String; TextFont :
TFont): Integer;
var
  OldFont : TFont;
begin

  { -----
    This procedure supports the HorizontalScrollBar routine.
    ----- }

  OldFont := TFont.Create;
  try
    OldFont.Assign( CanvasOWner.Font );
    CanvasOWner.Font.Assign( TextFont );
    Result := CanvasOwner.Canvas.TextWidth(Text);
    CanvasOWner.Font.Assign( OldFont );
  finally
    OldFont.Free;
  end;

end;

{ ============================================================== }
Procedure HorizontalScrollBar(ListBox: TListBox);
var
  MaxWidth : word;
  TextWidth : word;
  i : integer;
begin

  { -----
    This procedure is called to create a horizontal scroll bar for
    ListBoxes which Delphi didn't implement.
    ----- }

  MaxWidth := 0;
  for i := 0 to ListBox.Items.Count -1
  do
  begin
    TextWidth := GetTextWidth(ListBox, ListBox.Items[i],
                              ListBox.Font);
    if TextWidth > MaxWidth
    then
      MaxWidth := TextWidth;
  end;
  { the 'XXXXXXXX' is for aesthetic purposes only, not required
    and may need to be adjusted when incorporated into a custom
    component }
  MaxWidth := MaxWidth - GetTextWidth(ListBox, 'XXXXXXXX',
              ListBox.Font);
  SendMessage(Listbox.Handle, LB_SetHorizontalExtent, MaxWidth,
              Longint(0));

end;

--
Mark Richter
eMCee Software

Other Threads