Probe into Drag & DROP in Windows (3)

zhaozj2021-02-17  59

At this point, we successfully add files to CListCtrlex adds support. A complete drag and drop operation also includes dragging actions, so you must add a drag operation for this class, ie, a file in a list or multiple of the list is to be a file. This requires another class: COLEDATASOURCE. Specific steps are as follows:

Add a CListCtrlex instance, and map the LVN_BEGINDRAG message handler of the list box, where we add the code that drags the operation.

Implementing the drag is very simple, just call the three functions of COLLDATASOSOURCE: EMPTY is used to empty the data cached in the original object, and the cacheglobaldata is used to cache data for drag and drop operations, and finally call DodragDrop Start this drag and drop operation.

But before calling, you must do some preparations. The main task is to create a DROPFILES structure and copy the file named file name to the structure of the structure. The Dropfiles structure defines the CF_HDROP clipboard format, followed by the path name of a series of dragged files later. It is defined as follows:

Typedef struct_dropfiles

{

DWORD PFILES; // File name start address

Point pt; // The position of the mouse is put down, the coordinates are specified by the FNC member

Bool fnc; / / to True indicate the applicable screen coordinate system, otherwise the customer coordinate system is used

BOOL FWIDE; / / File Name String Use Wide Character

Dropfiles, Far * LPDROPFILES;

The code for the preparation action before dragging and drop is as follows:

Ubuffersize = sizeof (dropfiles) UBUFFERSIZE 1;

HMEMDATA = GlobalAlloc (GPTR, UBUFFERSIZ);

Assert (HMEMDATA! = NULL);

LPDROPFILES = (LPDROPFILES) Globalock (HMEMDATA); / / Lock, and set related members

Assert (LPDropFiles! = NULL);

LPDROPFILES-> Pfiles = SizeOf (dropfiles);

#ifdef _unicode

LPDROPFILES-> FWIDE = TRUE;

#ELSE

LPDROPFILES-> fwide = false;

#ENDIF

// Copy all the file names selected to the Dropfiles structure (global memory)

Pitempos = strselectedList.getHeadPosition ();

PSZStart = (char *) ((lpbyte) LPDROPFILES SIZEOF (DROPFILES));

While (PiteMpos! = NULL)

{

Lstrcpy (pszstart, (lpctstr) strselectedList.getnext (pitempos));

Pszstart = strchr (pszstart, '/ 0') 1; // The next start position is the last end 1

}

After you are ready, you can drag and drop, drag and drop a DodragDrop function trigger, the prototype is as follows:

DROPEFFECT DODRAGDROP

DWORD DWEFFECTS = DROPEFFECT_COPY | DROPEFFECT_MOVE | DROPEFFECT_LINK, LPCRECT LPRECTSTARTDRAG = NULL,

Coledropsource * pdropsource = null;

Here, DWEffects specifies a set of action sets to be applied to this COLEDATASOSOSOSOSOSOS: cut, replicated, or no action.

LPRectStartDrag indicates a rectangle that is really started with the drag and drop operation. If the mouse does not remove the rectangle, the drag and drop operation is considered to give up processing. If the member is set to NULL, the starting rectangle will be a pixel size.

PDROPSource demonstrates the ColedataSource object used to drag and drop.

The return value of this function indicates the effect actually generated by this drag and drop operation. As for the specific effect, it is determined by the system. For example, holding down the Shift key while dragging and dropping; holding down the Ctrl key, will generate a replication effect, and so on.

The code drag and drop is as follows:

m_oledatasource.empty ();

M_oleDataSource.cacheglobalData (CF_HDROP, HMEMDATA);

DropResult = m_oledatasource.dodragdrop (DROPEFFECT_MOVE | DROPEFFECT_COPY);

Last thing to note is that in a system over Windows NT 4.0, even if the DROPEFFECT_MOVE action is actually generated, the dodragDrop function returns only DROPEFFECT_NONE. The reason for this problem is that the shell of Windows NT 4.0 will move the file itself to optimize the mobile operation. Return Valo DROPEFFECT_MOVE initial meaning, is to notify the application to execute the drag and drop operation to delete the file on the original position. However, because the shell has completed this (delete) action for the application, the function returns DROPEFFECT_NONE. To know if the file is really moving, it is very simple. As long as the function returns, check if there is any file in the original position.

The Windows 9x series operating system also performs the same optimization action, but it does not return DROPEFFECT_NONE to replace DROPEFFECT_MOVE. Detailed explanation See the MS Knowledge Base Q182219.

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

New Post(0)