Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   GUI-Design mit VCL / FireMonkey / Common Controls (https://www.delphipraxis.net/18-gui-design-mit-vcl-firemonkey-common-controls/)
-   -   Delphi [TListBox] Aktuelle Scrollzeile (https://www.delphipraxis.net/86283-%5Btlistbox%5D-aktuelle-scrollzeile.html)

Nils_13 12. Feb 2007 09:33


[TListBox] Aktuelle Scrollzeile
 
Liste der Anhänge anzeigen (Anzahl: 1)
Hi,

wenn man in einer Listbox das Mouserad abfängt, dann möchte ich, dass eine TrackBar die Position erhält, an welcher gerade die ListBox steht. Ich meine NICHT den ItemIndex. Es ist schwer zu erklären, deshalb ist im Anhang ein kleines Beispiel, welches dieses Problem demonstrieren soll: Benutzt einfach den Slider und bewegt ihn ein paar Mal hoch und runter. Ich möchte genau dies mit dem MouseRad ermöglichen. Es ist wirklich nicht über ItemIndex zu lösen, da man beim scrollen nicht den ItemIndex beinflusst und die künstliche ScrollBar somit zum stehen kommt.
Zum Scrollen (reagiert auf Vertikal und Horizontal, nur Vertikal ist nötig) hier den zur Verfügung stehenden Code:
Delphi-Quellcode:
procedure TfrmMain.lbListScroll(Sender: TObject; ScrollCode: TScrollCode;
  var ScrollPos: Integer);
begin
  //
end;

bitsetter 12. Feb 2007 10:17

Re: [TListBox] Aktuelle Scrollzeile
 
Ich weis zwar nicht, ab du sowas meinst, aber kannst ja mal versuchen.
Delphi-Quellcode:
var
  Rect: TRect;
begin
  if listbox1.Count> 0 then
  begin
    Rect:= listbox1.ItemRect(0) ;
    caption:= inttostr(Rect.Top div listbox1.ItemHeight* -1);//Beispiel
    Trackbar1.Position:= (Rect.Top div listbox1.ItemHeight* -1)
  end;
end;
Der Code funktioniert nur beim runterscrollen.

Nils_13 13. Feb 2007 16:55

Re: [TListBox] Aktuelle Scrollzeile
 
Thx, das funktioniert perfekt. Wie sieht das dann beim Hochscrollen aus ? Steh gerade echt auf dem Schlauch.

bitsetter 13. Feb 2007 20:07

Re: [TListBox] Aktuelle Scrollzeile
 
Ich hoffe mal, dass dieser Code beim Hochscrollen funktioniert.
Delphi-Quellcode:
procedure TForm1.Button2Click(Sender: TObject);
  var
  Rect: TRect;
  WinInfo: TWindowInfo;
  iHeight: integer;
begin
  Caption:= '0';
  if listbox1.Count> 0 then
  begin
    ZeroMemory(@WinInfo, sizeOf(WinInfo));
    WinInfo.cbSize:= SizeOf(WinInfo);
    GetWindowInfo(listbox1.Handle, WinInfo);
    //iHeight= sichtbare Bereich in der Listbox
    iHeight:= WinInfo.rcClient.Bottom- WinInfo.rcClient.Top;
    Rect:= Listbox1.ItemRect(Listbox1.Count) ;
    if Rect.Bottom> iHeight then
    begin
      caption:= inttostr((Rect.Bottom- iHeight)div listbox1.ItemHeight);//* -1);
      //Trackbar1.Position:= ((Rect.Bottom- iHeight) div listbox1.ItemHeight);
    end;
  end;
end;

Nils_13 13. Feb 2007 20:12

Re: [TListBox] Aktuelle Scrollzeile
 
Leider nicht.
Delphi-Quellcode:
procedure TfrmMain.lbListScroll(Sender: TObject; ScrollCode: TScrollCode;
  var ScrollPos: Integer);
var Rect   : TRect;
    WinInfo : TWindowInfo;
    iHeight : integer;
begin
  if ScrollCode = scLineDown then
  begin
    Rect := lbList.ItemRect(0);
    xiScrollV.Position := (Rect.Top div lbList.ItemHeight * -1);
  end else
  if ScrollCode = scLineUp then
  begin
    ZeroMemory(@WinInfo, sizeOf(WinInfo));
    WinInfo.cbSize:= SizeOf(WinInfo);
    GetWindowInfo(lbList.Handle, WinInfo);
    //iHeight= sichtbare Bereich in der Listbox
    iHeight:= WinInfo.rcClient.Bottom- WinInfo.rcClient.Top;

    Rect := lbList.ItemRect(lbList.Count);
    if Rect.Bottom > iHeight then
      xiScrollV.Position := ((Rect.Bottom- iHeight) div lbList.ItemHeight);
  end;
end;
Er scrollt hoch und danach wieder runter.

bitsetter 13. Feb 2007 20:22

Re: [TListBox] Aktuelle Scrollzeile
 
Welchen Wert hat bei dir
Delphi-Quellcode:
Trackbar1.Max
?
Die Procedure erechnet praktisch, wieviel Items nicht mehr sichtbar sind, also unterhalb der Listbox sind.

Nils_13 13. Feb 2007 20:25

Re: [TListBox] Aktuelle Scrollzeile
 
Ah, daran könnte es liegen. Er hat lbList.Count (Playlistcount bzw. Listboxcount). Wie muss man denn hierfür den Max-Wert der Trackbar setzen ?

bitsetter 13. Feb 2007 20:50

Re: [TListBox] Aktuelle Scrollzeile
 
Versuche erst einmal
Delphi-Quellcode:
Trackbar1.Position:= lbList.Count- //Items unterhalb der Listbox(also die nicht mehr sichtbar sind)
Ansonsten so:
Delphi-Quellcode:
var
  Rect: TRect;
  WinInfo: TWindowInfo;
  iHeight: integer;
begin
  if listbox1.Count> 0 then
  begin
    ZeroMemory(@WinInfo, sizeOf(WinInfo));
    WinInfo.cbSize:= SizeOf(WinInfo);
    GetWindowInfo(listbox1.Handle, WinInfo);
    iHeight:= WinInfo.rcClient.Bottom- WinInfo.rcClient.Top;
    Trackbar1.Max:= ((Listbox1.Count* Listbox1.ItemHeight)-  iHeight) div Listbox1.ItemHeight+ 1;
    Rect:= listbox1.ItemRect(0) ;
    caption:= inttostr(Rect.Top div listbox1.ItemHeight* -1);//Beispiel
    Trackbar1.Position:= (Rect.Top div listbox1.ItemHeight* -1)
  end;
end;
Das würde bei dir aber komisch aussehen, da du eine Trackbar aber keine Scrollbar hast.

EDIT: Code nochmal geändert, funktioniert im prinzip aber genauso.

Nils_13 13. Feb 2007 21:14

Re: [TListBox] Aktuelle Scrollzeile
 
Was heißt "Items unterhalb der Listbox" ?

Daniel G 13. Feb 2007 21:20

Re: [TListBox] Aktuelle Scrollzeile
 
Zitat:

Zitat von Nils_13
Was heißt "Items unterhalb der Listbox" ?

Die Items, die ausgeblendet sind, sprich' "Unter'm unterstem Rand der Listbox" :wink:

bitsetter 13. Feb 2007 21:35

Re: [TListBox] Aktuelle Scrollzeile
 
Ja genau das meinte ich, das ist was die procedure ausgerechnet hat.

Nils_13 14. Feb 2007 14:31

Re: [TListBox] Aktuelle Scrollzeile
 
Deine neue Prozedur zum Runterscrollen macht jetzt alles perfekt. :) Jetzt ist halt noch die Frage, was man wegen dem Hochscrollen tun könnte.

bitsetter 14. Feb 2007 22:33

Re: [TListBox] Aktuelle Scrollzeile
 
Stell doch mal deine neue Demo zum Download bereit, will mal sehen wie es jetzt aussieht.

Nils_13 15. Feb 2007 17:56

Re: [TListBox] Aktuelle Scrollzeile
 
Liste der Anhänge anzeigen (Anzahl: 1)
Hier ist sie:

bitsetter 15. Feb 2007 19:46

Re: [TListBox] Aktuelle Scrollzeile
 
Liste der Anhänge anzeigen (Anzahl: 1)
Du benutzt für das Hoch- und Runterscrollen anscheinend 2 verschiedene Proceduren. Wenn man bei dir erst runterscrollt, und dann wieder hochscrollt, dann macht deine Trackbar einen großen Sprung.
Hast du schon einmal versucht, für das Hoch- und Runterscrollen, die gleiche Procedur zu nehmen?
Ich habe noch ein primitives Beispiel geschrieben, da ich nicht deine Listbox habe, musste ich mich mit einem Timer behelfen. Der Timer soll die Scrollmessage ersetzen.
Die Trackbar hatte jedenfalls beim scrollen immer die richtige Position.

Nils_13 15. Feb 2007 20:12

Re: [TListBox] Aktuelle Scrollzeile
 
:oops:
Ich hatte noch zwei If-Abfragen und noch einen etwas anderen Code:
Delphi-Quellcode:
if ScrollCode = scLineDown then
begin
  ZeroMemory(@WinInfo, sizeOf(WinInfo));
  WinInfo.cbSize    := SizeOf(WinInfo);
  GetWindowInfo(lbList.Handle, WinInfo);
  iHeight           := WinInfo.rcClient.Bottom - WinInfo.rcClient.Top;
  xiScrollV.Max     := (lbList.Count * lbList.ItemHeight - iHeight) div lbList.ItemHeight + 1;
  Rect              := lbList.ItemRect(0);
  xiScrollV.Position := (Rect.Top div lbList.ItemHeight* -1)
end else
if if ScrollCode = scLineUp then
begin
  ZeroMemory(@WinInfo, sizeOf(WinInfo));
  WinInfo.cbSize    := SizeOf(WinInfo);
  GetWindowInfo(lbList.Handle, WinInfo);
  iHeight:= WinInfo.rcClient.Top - WinInfo.rcClient.Bottom;
  xiScrollV.Max     := (lbList.Count * lbList.ItemHeight - iHeight) div lbList.ItemHeight + 1;
  Rect              := lbList.ItemRect(0) ;
  xiScrollV.Position := (Rect.Bottom div lbList.ItemHeight * -1);
end;
Danke, jetzt funktioniert alles!


Alle Zeitangaben in WEZ +1. Es ist jetzt 05:37 Uhr.

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