UnitUnit1; Interface Uses Windows, messages, sysutils, variants, classes, graphics, controls, forms, Dialogs, stdctrls; Type Tform1 =Class(Tform) Listbox1: tlistbox; Listbox2: tlistbox; ProcedureListbox1dragdrop (sender, source: tobject; X, Y: integer ); ProcedureListbox1dragover (sender, source: tobject; X, Y: integer; State: tdragstate;VaRAccept: Boolean ); Private Fdragoverobject: tobject; // listbox1dragdrop and listbox1dragover are shared by multiple listboxes. The current ListBox is recorded and dragged by the mouse. Fdragoveritemindex: integer; // record the index of the entry where the mouse is located ProcedureDrawinsertline; Public {Public declarations} End; VaR Form1: tform1; Implementation {$ R *. DFM}
{===================================================== ====================================== Design by: Peng Guohui Date: 2004-12-24 Site: http://kacarton.yeah.net/ Blog: http://blog.csdn.net/nhconch Email: kacarton # sohu.com The article is original to the author. Please contact me before reprinting. for reprinting, please indicate the source of the article and retain the author information. Thank you for your support! ========================================================== =====================================} ProcedureTform1.listbox1dragdrop (sender, source: tobject; X, Y: integer ); VaR I: integer; Begin // Drag and drop to read the content from the original ListBox to the target ListBox. WithTlistbox (source)Do begin I: = tlistbox (sender). itematpos (point (x, y), true ); IfI <>-1Then 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)ThenI: = I-1; Deleteselected; If(Sender = source)ThenItemindex: = I; End; Fdragoverobject: = nil; Fdragoveritemindex: =-1; End; ProcedureTform1.listbox1dragover (sender, source: tobject; X, Y: integer; State: tdragstate;VaRAccept: Boolean ); VaR Index: integer; Begin Accept: = (source is tlistbox)And(Tlistbox (Source). itemindex>-1); // only accept content from ListBox If NotAcceptThenExit; If(Fdragoverobject <> nil)And(Sender <> fdragoverobject)Then Drawinsertline; // when the mouse leaves the ListBox, erase the inserted position and prompt the box. Index: = tlistbox (sender). itematpos (point (x, y), true ); If(Fdragoverobject = sender)And(Fdragoveritemindex = index)ThenExit; // when you move the mouse over the same entry, you can only draw it once. If(Fdragoverobject = sender)And(Fdragoveritemindex <> index)Then Drawinsertline; // move the cursor to a new position, and erase the hint box of the old insert position. Fdragoverobject: = sender; Fdragoveritemindex: = index; Drawinsertline; // draw the prompt box Indicating the insertion position End; ProcedureTform1.drawinsertline; VaR R: trect; Begin IfFdragoverobject = NilThenExit; WithTlistbox (fdragoverobject)Do begin IfFdragoveritemindex>-1Then begin R: = itemrect (fdragoveritemindex ); R. Bottom: = R. Top + 4; End else IfItems. Count> 0Then begin R: = itemrect (items. Count-1 ); R. Top: = R. Bottom-4; End else begin Windows. getclientrect (handle, R ); R. Bottom: = R. Top + 4; End; Drawfocusrect (canvas. Handle, R ); Inflaterect (R,-1,-1 ); Drawfocusrect (canvas. Handle, R ); End; End; End.
|
Object form1: tform1 Left = 1, 192 Maximum = 107 Width = 540 Height = 376 Caption = 'form1' Color = 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 Ondragover = listbox1dragover End Object listbox2: tlistbox Left = 1, 264 Top = 24 Width = 233 Height = 265 Style = lbownerdrawfixed Dragmode = dmautomatic Itemheight = 20 Items. Strings = ( 'The code is indeed available and widely used, but it has a major drawback :' 'Efficiency is low. Because each time you append, insert, or delete' 'Call this function to recalculate the horizontal scroll bar width' ', While traversing all projects and calling textwidth is very much' 'Time-consuming operations. If you drag an entry from the current ListBox to another' 'Listbox', the user will have two operations' 'Listbox' must recalculate the width of the horizontal scroll bar. When listbox' 'You will obviously feel slow when there are hundreds of contents. ' 'OK. Now let's change your mind. ' 'When appending or inserting a new entry, you only need to judge the text of the new content' 'Specifies whether the width is greater than the scroll bar width. If the width is adjusted' . What about deletion? Yes, traversal is inevitable, but not' 'Is required for each deletion. You can define a variable record in ListBox' 'Index of the entry with the largest textwidth value, only when this entry is deleted' . ' 'Another situation must be considered. The user may change' 'Screen font. You must recalculate the width of the horizontal scroll bar. ' 'Calculate the new textwidth value of the original maximum entry just like the delete operation. ' 'If there are multiple listboxes on the form, record each listbox' 'The biggest entry is also very troublesome, So I encapsulate it ,' 'Complete code is provided below :') Taborder = 1 Ondragdrop = listbox1dragdrop Ondragover = listbox1dragover End End |