How to scroll a list by dragging an item up or down?

I am working with TListView , which has drag / drop functions to drag one or more items into one other item, like a merge form. When you drag an item to the top or bottom of a control, I need to automatically scroll up or down, but that’s not the case. The same applies to scrolling left or right in certain viewing styles. How can I automatically scroll it in the direction in which the user drags the item?

PS: I also have VCL themes

+4
source share
1 answer

Not much has been tested, but the attempt below includes a timer when the item is dragged outside the control over its parent (in the case of an example, a form), and the timer event checks the cursor position to find out if the scroll message should be sent to the list.

 procedure TForm1.FormCreate(Sender: TObject); begin Timer1.Enabled := False; Timer1.Interval := 500; end; procedure TForm1.FormDragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean); begin if Source = ListView1 then Timer1.Enabled := True else Timer1.Enabled := False; end; procedure TForm1.Timer1Timer(Sender: TObject); var Pt: TPoint; begin // Stop timer and exit if not dragging any more if not ListView1.Dragging then begin Timer1.Enabled := False; Exit; end; Pt := ListView1.ScreenToClient(Mouse.CursorPos); if Pt.Y < 0 then ListView1.Perform(WM_VSCROLL, SB_LINEUP, 0) else if Pt.Y > ListView1.ClientHeight then ListView1.Perform(WM_VSCROLL, SB_LINEDOWN, 0) else Timer1.Enabled := False; end; procedure TForm1.FormDragDrop(Sender, Source: TObject; X, Y: Integer); begin Timer1.Enabled := False; end; 

If it works Well, you can enable horizontal scrolling.

+3
source

Source: https://habr.com/ru/post/1434506/


All Articles