Explore the potential of ListBox (2): The mouse drag and drop insertion point prompt

Source: Internet
Author: User

Mouse drag and drop insert point prompt

Mouse drag and drop is a common operation in windows. For example, you can drag and drop a copy of a file. In our applications, you can drag and drop the mouse to facilitate user operations. For most VCL controls, you can process drag-and-drop events in ondragdrop, ondragover, and onenddrag by setting dragmode to dmautomatic. Similar to drag, there is also a dock mode that supports control suspension. when the control is suspended, a dotted box is displayed to indicate the suspension position, but the drag mode does not. Now let's try to display the drag-and-drop insertion point in ListBox.
Among the three events mentioned above, ondragover is generated when you drag and drop the mouse over the control. To display the insertion point prompt, you must handle it here. ListBox is used in the event. itematpos (point (x, y), true) takes the index where all the mouse is positioned, and then uses ListBox. itemrect (INDEX) gets the drawing area, and finally draws the prompt box in the area. The following code is provided:

Unit1.pas content
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.

Unit1.dfm content [long content, please click here to open/collapse]
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

(End)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.