In terms of operation habits, we can divide users of Windows applications into two categories: users who prefer to use the keyboard, and users who prefer to use the mouse. A large number of practical experience has given programmers a full understanding of how to provide hotkeys (with the underscore-free character keys) and shortcut keys (for example, Ctrl + A character combination button) for a large number of users who use the keyboard, the requirements of the mouse users are often ignored. Because programmers tend to be keyboard users, it is understandable to emphasize the keyboard-oriented function, but every programmer should take into account the complete support of the mouse.
In fact, what users expect most is full support for drag-and-drop operations. Looking at most of the Windows applications or the Windows operating system itself, we will find that the drag and drop capabilities are everywhere. For example, you are used to dragging and placing files in Windows Resource Manager and dragging and placing text in Microsoft Word.
Unfortunately, only a very small number of Visual C # programmers provide complete drag-and-drop functions in their applications. Of course, one of the reasons for this phenomenon is that, it is difficult and complex to implement the drag-and-drop function. This section will show you how to use Visual C #2003 ~ It is easy to implement the drag-and-play function in Versions later than 2005. We will show you how to move and copy text, images, and files between forms and applications.
How drag-and-drop operations work
The drag-and-drop operation is actually no different from cutting and pasting (or copying and pasting), except that it uses the mouse instead of the keyboard. In both types of operations, you will have a source (that is, the object you cut or copy) and a target (that is, where you paste it ). No matter which operation, a copy of data exists in the memory during the operation. The clipboard is used for cutting and pasting, while the DataObject object is used for dragging and dropping. In fact, the DataObject object is like a private clipboard.
In a typical drag-and-drop operation, the following events are triggered in sequence:
1. You can call the DoDragDrop method of the source control to initialize the drag operation. The DoDragDrop method syntax is as follows:
DragDropEffects DoDragDrop (
Object data,
DragDropEffects allowedEffects) The DoDragDrop method accepts the following two parameters:
A new DataObject object is automatically created.
2. The GiveFeedback event of the source control is introduced. In most cases, you do not need to handle the GiveFeedback event, but if you want to display a custom mouse pointer during dragging, you can write the program code in the GiveFeedback event handler to complete this setting.
3. Any control with the AllowDrop attribute set to True can be the placement target. In the design phase, you can set the AllowDrop attribute of the target control to True in the "properties" window, in the running stage, the AllowDrop attribute of the target control is set to True in the Load event processing function of the form.
4. When you move the mouse pointer over any control, the DragEnter event of the control is triggered. We usually use the GetDataPresent method in the DragEnter event processing function of the target control to check whether the dragged data format is applicable to the target control, use the Effect attribute of the DragEventArgs type parameter to set the allowed placement operation.
5. If you open the mouse button on a valid placement target, the DragDrop event of the target control will be triggered. We usually write code in the DragDrop event processing function of the target control to retrieve data from the DataObject object and display it in the target control.
Note the following for drag and drop operations:
Some controls have custom drag events. For example, the ListView and TreeView controls have ItemDrag events. When a drag operation is being executed, You can process the QueryContinueDrag event, which will "require permission" to continue the drag operation. When processing with this method, it is also a very appropriate time to call methods that have an impact on the drag operation. For example, when the mouse pointer stays above the TreeView control, expand a TreeNode. You can also define your own DataFormats. The procedure is very simple. You only need to specify your Object as the Object parameter in the SetData method, and make sure that the specified Object is serializable. In addition, you can also use the KeyState attribute to produce a specific effect based on the keys pressed during the drag-and-drop operation. For example, the data dragged when the Ctrl key is pressed is usually copied. Drag text
The simplest implementation of the drag operation is to move or copy the text in a TextBox Control to another TextBox Control. Of course, you can also use the copy, cut, and paste operations to copy or move data between two TextBox controls. However, using the drag-and-drop operation to complete such operations is definitely more efficient.
The program example CH8_DemoForm011.cs demonstrates how to drag text between two TextBox controls. Its features are as follows: Figure 8.10 demonstrates how to drag text
8.10, because the AllowDrop attribute of the TextBox Control on the right is set to False, you cannot drag and drop text from the TextBox Control on the left. 8.11. Because the AllowDrop attribute of the TextBox Control at the bottom right is set to True, you can drag and drop the text in the left TextBox Control to the TextBox Control at the bottom right. It is worth mentioning that if you continue to press the Ctrl key, you can use drag and drop to copy the text of the Left TextBox Control to the TextBox Control at the bottom of the right (8.12 ).
Figure 8.11 move text by dragging
Figure 8.12 text copying through drag-and-drop operations
The program code of the CH8_DemoForm011.cs drag-and-drop operation is as follows:
// Declare a constant to debug whether the Ctrl key is pressed during dragging.
Const byte CtrlMask = 8 /// this event is triggered when you press the mouse button within the control range.
Private void txtLeft_MouseDown (object // if the user presses the left mouse button.
If (e. Button ==/// select all text in the text box.
// Initialize the drag-and-drop operation.
| ////// DragEnter event of the target control.
Private void txtLowerRight_DragEnter (object // check whether the type of the dragged data applies to the target control. If not, the system rejects the placement.
If (e. Data. GetDataPresent (DataFormats. Text ))
// If you press Ctrl during dragging, the copy operation is executed; otherwise, the move operation is executed.
If (e. KeyState & CtrlMask) = elseelse // This event is triggered when you open the mouse button and terminate the drag and drop operation.
Private void txtLowerRight_DragDrop (object = // If the Ctrl key is not pressed, remove the source text to create a moving text effect.
If (e. KeyState & CtrlMask )! =
From the code above, we can see that in the MouseDown event processing function of the drag-and-drop source (that is, the TextBox Control on the left), we can judge that the mouse button has been pressed, if you press the left mouse button, you will call the DoDragDrop method and pass the following two parameters to initialize the drag operation:
We use the selected text in the TextBox control as the value of the first parameter (that is, the data parameter), that is, the text in the TextBox Control will become the dragged data. Set the second parameter (allowedEffects parameter) to DragDropEffects. Move Or DragDropEffects. Copy to allow users to Move Or Copy data.
We will execute the following processing in the DragEnter event processing function of the placement target (that is, the TextBox Control at the bottom right:
1. Use the GetDataPresent method to check whether the dragged data is plain Text (DataFormats. Text ). If it is not plain text, set the Effect attribute to DragDropEffects. None to indicate that the target does not accept data. If it is pure text, continue to process it later.
2. Check whether the Ctrl key is pressed. If the Ctrl key is pressed, set the Effect attribute to DragDropEffects. copy: Copy data to the placement target. The mouse pointer is displayed as a Copy pointer icon. If the Ctrl key is not pressed, set the Effect attribute to DragDropEffects. move: Move the data to the placement target.
We will perform the following operations in the DragDrop event processing function of the placement target (that is, the TextBox Control at the bottom right:
1. Use the GetData method to extract the dragged text from the DataObject object and assign it to the target.
2. Determine whether the Ctrl key is pressed. If the Ctrl key is not pressed, the source text will be removed to create a moving text effect.
Http://msdn.microsoft.com/en-us/library/system.windows.forms.control.dodragdrop (VS.80). aspx