First, basic concept
Drag and drop, refers to the technique of moving, copying, and pasting between different windows of different programs or different windows of different programs or different windows of different programs, between different windows of different programs. The drag and drop operation is done with the help of the operating system. The object being dragged first registers the data format it uses it to the operating system, and provides data in the specified data format. At the end of the drag and drop operation, receive the drag and dropped window to extract the relevant data in the specified data format, and according to the data Generate the corresponding object.
Second, two kinds of drag and drop
Drag and drop with two types: OLE drag and drop and file manager drag and drop. These two ways are completely different mechanisms. The file manager drag and drop can only handle the file name, and the window can receive the file name of the drag and drop through the WM_DROPFILES message map of the map. OLE drag is more common, it allows you to drag and drop any data that can be saved on the clipboard simultaneously. This article first introduces the file manager drag and drop, then introduces the OLE drag and drop, and finally gives an enhanced list control CListCtrlex that supports file drag and drop operations with OLE.
Third, file manager drag and drop principle and instance
The essence of this way is to generate a message WM_DROPFILES. There is no difficulty in technology, mainly used in the following API functions: DragQueryFile, DragQueryPoint, Dragfinish. Their prototypes and annotations are as follows:
Uint Dragqueryfile (HDROP HDROP, UINT IFILE, LPTSTSTSZZFILE, UINT CCH)
This function is used to obtain the file name of the drag and drop. Among them, HDROP is a handle that points to the structure of the file name that is dragged; ifiles is the file number to query, because one may drag many files at the same time; lpszfiles is an outlet buffer pointer, saving ifiles specifying the file The path name, the CCH specifies the size of the buffer. There are two points worth noting, first, if we specify ifile to 0xfffffffff, DragQueryFile will ignore the LPSZFILEFILE to return the number of files for this drag and drop operation; second, if the LPSZFILE is NULL The function will return to the actual desired buffer length.
Bool DragQueryPoint (HDROP HDROP, LPPOINT LPPT);
This function is used to get it, when the drag and drop operation is being performed, the position of the mouse pointer. The second parameter LPPT is a pointer to the Point structure to save the file, the position of the mouse pointer. The window can call this function to query if the file falls in its own window rectangle.
Void Dragfinish (HDROP HDROP);
When the drag and drop operation is processed, you need to call the function release system allocation to transfer the memory of the file name.
First, establish a dialog project to ensure that the Accept Files property of the dialog box is selected. If you are not elected, you can also call DragacceptFiles (TRUE) when you created (such as on the oncreate function), the effect is the same.
Then map WM_DROPFILES messages. The message processing function prototype is as follows: Void OnDropfiles (HDROP HDROP), note the entrance parameter is HDROP type, which is a structural pointer, which contains the name of the file being dragged and dropped. Next, we must complete two actions: first, by calling DragQueryFile and specifying its ifile parameter to 0xfffffffff, get the number of files for this drag and drop operation; second step, with a loop to remove each file name. Examples are as follows:
Void ClistCtrlex :: OnDropfiles (HDrop HDROP) {
Char szfilepathname [_MAX_PATH 1] = {0};
Uint nnumoffiles = DragQueryFile (HDROP, 0xfffffffff, null, 0); // Get file number
For (uint nindex = 0; NINDEX { DragQueryfile (HDROP, NINDEX, SZFILEPATHNAME, _MAX_PATH); // Get file name } Dragfinish (HDROP); } Fourth, OLE drag and drop principle MFC provides the following categories for the implementation of object drag and drop: Coledatasource, ColedataObject, ColedropTarget, Coledropsource. They are described below, and then the specific steps implemented by an instance. · COLEDATASOURCE Start a drag and drop operation, save the data of the drag and drop, and provide data for drag and drop objects to the system. The important member functions in the class are divided into the following three: 1. The data format used to set the way the data is provided. There are two ways to provide data, one is an immediate mode, and the other is a delay mode. The delay mode does not need to provide data immediately. When data is required, the system will call the corresponding function to obtain data, which is generally overrenderdata functions or other virtual functions to respond to data requests. The data format can be a commonly used clipboard format such as CF_Text, or it is also a specific format that is registered with the function regiPboardFormat function. Cachedata: Provides data in the specified format, formatted by structure STGMEDIUM, instant mode; CacheglobalData: Using the global handle hglobal, provide data to the specified format, instant mode, and apply small data; DELAYRENDERDATA: Use the delay mode to provide data in the specified format, when the system needs data, the function onRenderglobalData / OnRenderData is tested; DelayRenderFileData: Use the delay mode to utilize the cfile to provide data to the specified format. When data is required, the function onRenderFileData is called to obtain the data; 2. Response request, provide data OnRenderFileData: Provides CFILE type data for delay. OnRenderglobalData: Hglobal data is provided for the delay. OnRenerData: Provides a variety of supported data for the delay mode. 3. Implement a drag and drop operation DODRAGDROP: Start implementing drag and drop operations · COLEDATAOBJECT Data used to represent drag and drop, it is a member of the COLEDATASOSOURCE class, and the main member functions in the class: BeginenumFormat: Prepare for enumeration data format; GetNextFormat: Returns the next data format; Isdataavailable: Check if the specified data format is available; GetData: Pay data by specified data format; GetFileData: Press the specified data format to get CFILE type data; GETGLOBALDATA: Press the specified data format to get hglobal data; · COLEDROPTARGET A communication mechanism is used between windows and OLE libraries. Any window, to be able to receive drag and drop, must contain a COLLDROPTARGET object and register. The member function can be divided into two categories: 1. registered Register: Register this object so that the window can receive drag and drop objects 2. Responsive action during drag and drop (virtual function) OnDragenter: When the mouse is first entered into the window; OnDragleave: Call when the mouse is moved; ONDRAGOVER: When the mouse stays within the window; OnDrop: When the mouse is called in the window; The return value of the virtual function OnDragenter and OnDragover has an important meaning, generally one of the following: DROPEFFECT_MOVE: Move operation, allows objects to fall in this window, and then delete the original object; DROPEFFECT_COPY: Copy operation, allows objects to fall in this window, do not delete the original object; DROPFFECT_NONE: The object is not allowed to fall in this window; · Coledropsource COLEDROPSOURCE allows data to be dragged and dropped into a drag and drop target, which is responsible for starting a drag and drop operation to make a decision, feedback drag and drop operation status, and when the drag and drop operation ends. This class is relatively simple and is used. Its member functions are only three: GiveFeedback: It is used to change the cursor of the mouse during drag and drop, and feedback the drag and drop state to the user; Onbegindrag: Capture the mouse pointer during drag and drop, when the application framework feels that it may have a drag and drop operation; QueryContinueDrag: Detect whether the drag and drop operation is still continuing. To Be Continue ...