Use MFC to drag and drop objects

Source: Internet
Author: User
Tags types of functions
Use MFC to drag and drop objects

Object drag and drop refers to the use of the mouse to drag a specified object in different application windows
Between, between different windows of the same application, or within the same window of the same application, move, copy (paste)
And other operating technologies.

You can drag and drop objects to provide a convenient and intuitive operation interface.
To implement the object drag-and-drop technology, you need to understand and use the cview, coledatasource, and coledroptarget of MFC.
And use these classes to work collaboratively.
This article discusses the object drag-and-drop technology, and studies how to use MFC to implement this technology.
Using MFC to drag and drop objects makes programming easier and the code is readable.

Revised draft

Use MFC to drag and drop objects

1. concept of object drag and drop

Object drag and drop refers to the use of the mouse to drag a specified object in different application windows
Between, between different windows of the same application, or within the same window of the same application, move, copy (paste)
And other operating technologies.

Object drag and drop is completed with the help of the operating system. To start a drag, you must specify
Or generate the dragged object, specify the data format used during the entire drag-and-drop operation, and
To provide data, and finally start the object drag and drop operation. When the object falls in a window, drag
The release process ends. The window that receives the drag-and-drop object extracts the relevant data in the specified data format and extracts the data according to
.

2. Classes used for object drag and drop in MFC

Microsoft Foundation classlibrary (MFC) provides the following three classes for implementing object drag and drop.
For the convenience
Let's familiarize ourselves with these classes first.

2.1.coledatasource. It is used to start a drag-and-drop operation and provide the system with the data of the drag-and-drop object.
Class Member
There are three types of functions:

A. Set the method for providing data and the data format used. There are two ways to provide data, one is instant
Method, the other is the latency mode; the instant mode needs to provide data before the start of the drag; the delay mode does not
Data needs to be provided immediately. When the system requests the relevant data, it is provided by virtual functions such as onrenderdata ().
Required data.

You can use functions such as cacheglobaldata () to provide real-time data.
Delayrenderdata () and other functions specify the delay mode to provide data.

B. Respond to the request and provide data. Onrenderfiledata () or other corresponding virtual functions should be reloaded
Provide relevant data (which will be discussed later ).

C. perform drag-and-drop operations. Call the dodragdrop () function to start the drag and drop operation.

2.2.oledatatarget. The target window for receiving drag-and-drop objects.
The drag-and-drop object must contain a coledatatarget object and register it. Main member functions in the class:

A. Register. The Register () function registers this object so that the window can receive drag-and-drop objects.

B. Respond to the action in the drag-and-drop process (Virtual member function). When the mouse enters the window for the first time, the system calls
Ondragenter (). When the mouse moves out of the window, the system calls ondragleave (). When the mouse moves in the window,
The system will call ondragover () repeatedly and call ondrop () when the object falls in the window ().

2.3.oledataobject. used to receive drag and drop objects. There are two main member functions in the class:

A. Determine the data format that can be used. Isdataavailable () and other functions determine whether the specified data format is available;

B. obtain data. Functions such as getdata () and getfiledata () are used to obtain data in the specified data format.

3. Use MFC to drag and drop objects

To implement an object drag-and-drop operation, you need to perform three steps: In the window where the object is located, You need to drag and drop the object and start the drag operation,
The receiving Object window responds to the drag-and-drop message, receives the dropped object, and post-processing when the drag-and-drop is completed.
The following sections describe them respectively.

3.1. Start of the drag operation. Drag and Drop Operations generally start with clicking the left mouse button. Response in message wm_lbuttondown
In the onlbuttondown (...) function, you must first determine whether an object is selected. If no or multiple objects are selected
You can drag and drop an object.

To start a drag-and-drop operation, you must first prepare a coledatasource object. Note the coleclientiten and class
Coleserveritem is derived from the coledatasource class. If you select a coleclientitem object or
You can use the coleserveritem object directly. Otherwise, you must generate a coledatasource object.
Yes: As mentioned above, the data format used should be specified and relevant data of the object should be provided in the specified format.

The following example shows how to prepare a Data source:
Class mydatasource: Public coledatasource
{
Public:
Colorref color;
Cstring STR;
Protected:
Virtual bool onrenderfiledata (lpformatetc, cfile *);
//......
};

Bool mydatasource: onrenderfiledata (lpformatetc, cfile * pfile)
{
If (lpformatetc-> cfformat = cf_text)
{
Pfile. Write ("test dragdrop", 13); // magic string
Pfile. Write (& color, sizeof (colorref ));

Int Len = Str. getlength ();
Pfile. Write (& Len, sizeof (INT ));
Pfile. Write (STR, Len );
Return true;
}

Coledatasource: onrenderfiledata (lpformatetc, pfile );
Return false;
}

With the above data source, you can specify the data format as follows in the Response Function onlbuttondown () of the message wm_lbutton:

Mydatasource * pitemdragdrop = new mydatasource;
Pitemdragdrop-> STR = "this string will dragdrop to another place ";
Pitemdragdrop-> delayrenderfiledata (cf_text, null );

After specifying the data format, call the member function dodragdrop (...) of this object to start the drag and drop operation of the object.
It should be noted that the function dodragdrop (...) does not return immediately, but waits until the mouse button pops up.

3.2. Receiving of drag-and-drop objects. By default, a window cannot receive drag-and-drop objects.
To put an object, you must add the member object "coledroptarget" to the window class definition and call the function when generating the window.
Coledatatarget: Register (). For example:
Class myview: Public cscrollview
{
PRIVATE:
Coledroptarget oletarget;
Protected:
Virtual int oncreate (lpcreatestruct );
//......
}

Int myview: oncreate (maid)
{
//......

Droptarget. Register (this );
Return 0;
}

To receive drag-and-drop objects, you should also reload the virtual functions of cview or coledroptarget: condragmove (),
Ondragenter () and ondrop. The ondragenter () and ondragmove () functions should be based on the cursor position in the window,
Returns the following values:

Dropeffect_move --- indicates that the object can be copied to the current window and current position;
Dropeffect_copy --- indicates that the object can be moved from the original window to the current window and current position;
Dropeffect_none --- indicates that the position of the window cannot be put down.
In the following example, only objects can be moved, but objects cannot be copied:
Dropeffect myview: ondragenter (......)
{
Return dropeffect_move;
}

Dropeffect myview: ondragover (......)
{
Return dropeffect_move;
}

The ondrop () function should handle the work after the dragged object is put down. The parameter pdataobjec of this function points to
A coledataobject object that uses pointers to obtain relevant data. The general implementation of this function is:

A. Check the data format of the object: Use the function coledataobject: isdataavailable ();

B. Get data in the specified format: use functions such as coledataobject: getfiledata;

C. Create an object (it may be the same as the original object, or you may not create an object to use only the data in the object): use the above steps
Create an object for the obtained data. For example:
Char magic_string [13];
Colorref color;
Cstring STR;
Int Len;
Mydatasource * pmydata;

If (isdataavailable (cf_text ))
{
Cfile file = getfiledata (cf_text );

File. Read (magic_string, 13 );
If (strncmp (magic_string, "test dragdrop", 13) = 0)
{
File. Read (& color, sizeof (colorref ));
File. Read (& Len, sizeof (INT ));
File. Read (STR, Len );

Cclientdc DC (this );
DC. settextcolor (color );
DC. setbkmode (transparent );
DC. textout (100,50, STR, Len );

Pmydata = new mydatasource;
Pmydata-> color = color;
Pmydata-> STR = STR;
}
}

For coleclientitem or coleserveritem objects, you can use the following methods to easily recreate objects:
Coleclient * pitem = getdocument ()-> createnewitem ();
Pitem-> createfrom (pdataobject );

3.3. When the end function dodragdrop () of the drag-and-drop operation returns, the drag-and-drop process ends. The Return Value of the dodragdrop () function,
Indicates the drag-and-drop result of the object.
Dropeffect_move: place the object to another location and delete the original object.
Dropeffect_copy: the object is copied to another place and the original object is not deleted.
Dropeffect_none: failed to implement drag and drop, no need to delete the original object

For example:
Int drageffect = pitemtracking-> dodragdrop (......);
Switch (drageffect)
{
Case dropeffect_move:
Delete pitemtracking;
Getdocument ()-> updateallitems (null );
Getdocument ()-> updateallviews (null );
Break;
Case dropeffect_copy:
Case dropeffect_none:
Default:
Break;
}

Related Article

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.