Discover ListBox's potential (2): mouse drag and drop insertion point prompt

xiaoxiao2021-03-06  41

Mouse Drop Insertion Point Tips Mouse Drag and drop is a common operation, such as copy files, can be drooped. In the application we have written, sometimes it is necessary to support the mouse drag and drop in order to facilitate user operation. For most VCL controls, as long as the mouse set DragMode to DMautomatic, you can process drag and drop events in OnDragDrop, OnDragover, and OneEndDrag. Similar to Drag has a Dock mode for supporting control suspensions, the control will display a dashed box when the control is suspended to represent the suspension position, but the DRAG mode does not have this function. Now let's try to display the drag and drop insert point in ListBox. In the three events mentioned above, the onDragover is generated when it is used to drag and drop the mouse over the control, and to display the insertion point prompt is of course handled here. In the event, use ListBox.itematpos (Point (X, Y), True to take the mouse in the mouse, and then use ListBox.ItemRect (index) to obtain the status area, and finally draw a prompt line in the area. The code is given below:

Unit1.pas content unit Unit1; interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class (TForm) ListBox1: TListBox; ListBox2: TListBox; procedure ListBox1DragDrop (Sender, Source: TObject; X, Y: Integer); procedure ListBox1DragOver (Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean); private FDragOverObject: TObject; // ListBox1DragDrop, ListBox1DragOver shared by a plurality of Listbox, here the current record Listbox receiving drag and drop FDragOverItemIndex: Integer; // Index procedure DrawInsertLine mouse cursor record entry; public {public declarations} end; var Form1: TForm1; implementation {$ R * .dfm} {===== ============================================================================================================================================================================================================= ================= Design by: Peng Guoi Date: http://kacarton.yeah.net/ blog: http://blog.9cbs. Net / NHCONCH Email: Kacarton # Sohu.com Article is the author original, please contact me before reprinting, please indicate the article, retain the author information, thank you for your support! ============================================================================================================================================================================================================= =========================================================================================================================================================================================================================================================================}; x, y: integer; Drag and drop,

Read the content from the original listbox to the target Listbox with tlistbox (source) do beg i: = tlistbox (sender) .Istpos (Point (x, y), true); if i <> - 1 THEN TLISTBOX (Sender) .Items .InsertObject (i, Items [ItemIndex], Items.Objects [ItemIndex]) else i: = TListBox (Sender) .Items.AddObject (Items [ItemIndex], Items.Objects [ItemIndex]); if (Sender = Source) and (i> ItemIndex) then i: = i-1; DeleteSelected; if (Sender = Source) then ItemIndex: = i; end; FDragOverObject: = nil; FDragOverItemIndex: = -1; end; procedure TForm1.ListBox1DragOver (Sender, Source : Tobject; x, y: integer; state: tdragState; var index: integer; begin accept: = (source is tlistbox) and (tlistbox (source) .ItemIndex> -1); // only accept From ListBox content if not accept the exit; if (fdragoverObject <> nil) and (sender <> fdragoverOBject) THEN DrawinsertLine; // When the mouse is left ListBox, the insertion position prompt line Box index: = TListBox (sender) .ItemtPOS (Point (x, y), true); if (fdragoverObject = sender) and (fdragoveritemindex = index) THEN EXIT; / / When the mouse is moved on the same entry When you move, you can only draw an IF (fdragoveroBject = sender) and (fdragoverItemIndex <> index) THEN DRAWINSERTLINE; // Mouse moved to a new location, erase the old insertion position prompt linefragoverOverObject: = sender; fdragoverItemIndex: = index ; DrawInsertLine; // draw the inserted position prompted frame end; procedure TForm1.DrawInsertLine; var R: TRect; begin if FDragOverObject = nil then Exit; with TListBox (FDragOverObject) do begin if FDragOverItemIndex> -1 then begin R: = ItemRect (Fdragoveritemindex); R.Bottom: = r.top 4; END ELSE IF ITEMS.COUNT> 0 THEN BEGIN R: = ItemRect (items.count-1); r.top: = R.BOTTOM - 4;

Else Begin Windows.getClientRect (Handle, R); R.Bottom: = R.Top 4; End; DrawFocusRect (canvas.handle, R); Inflatecture (R, -1, -1); DrawFocusRect (canvas.handle , R); end; end; end.Unit1.dfm content [longer = clBtnFace Font.Charset = DEFAULT_CHARSET Font.Color = clWindowText Font.Height = -11 Font.Name = 'MS Sans Serif' Font.Style = [] OldCreateOrder = False PixelsPerInch = 96 TextHeight = 13 object ListBox1: TListBox Left = 24 Top = 24 Width = 201 Height = 265 Style = lbOwnerDrawFixed DragMode = dmAutomatic ItemHeight = 20 Items.Strings = ( 'Accept: = (Source is TkktLabelListBox) and (TkktLabelListBox (S' 'ource) .ItemIndex> -1);') Taborder = 0 onDragdrop = listbox1dragdrop overdragover = listbox1dragover end object listbox2: tlistbox left = 264 top = 24 width = 233 height = 265 style = lb OwnerDrawFixed DragMode = DMAUTOMATIC ITEMHEIGHT = 20 items.strings = ('is indeed available in code and is widely used, but it has a big shortcoming:' 'efficiency is low. Because each time the '' entry is added, inserted or deleted in ListBox, call this function to recalculate the horizontal scroll bar width '', and all items and calling textWidth are very 'time-consuming operations. If the user drags the entry from the current listbox to another '' listbox, then the user will have two '' Listbox must recall the horizontal scroll width. When listbox '' has hundreds of pieces, you will be obvious. It feels slowly. '' OK, now change your ideas.

转载请注明原文地址:https://www.9cbs.com/read-77017.html

New Post(0)