Implementation of multi-selection drag-and-drop function in VC control "tree"

Source: Internet
Author: User
Tags extend

The control "tree" is a powerful control that can clearly display the inheritance of the contained data. But it's not easy to really master and use the many features of the CTreeCtrl class. In Visual C + + Developer Magazine's September, October two, Stephen introduced two classes of Cbitmaptree and ccheckabletree usage, which can extend and simplify class CTreeCtrl. In this article, we will focus on the upgraded version of Class Cbitmaptree, which simplifies the process of adding drag-and-drop functionality to the tree control, and enables multiple-selection drag-and-drop functionality.

In the CTreeCtrl class, the drag-and-drop feature is one of the features that is difficult to master and apply. Because this feature gives a lot of flexibility to drag-and-drop operations, the MFC group specifically assigns programmers to the task of writing this aspect of the function, which makes it less daunting for users to implement the drag-and-drop functionality of the control tree in a developed application. This will introduce the extended member functions in class Cbitmaptree to simplify the implementation of the drag-and-drop functionality of the control tree, and extend the attribute to multiple-selection drag-and-drop.

The introduction of this article assumes that you have an understanding of the concepts and techniques in the 9, October Visual C + + Developer magazine. The topics in these two articles are the "a" and multiple Selection and Simpler Image Handling〖1〗 and a tree control with Three-state Check Boxes and sophisticated Sorting〖2〗. In addition, the upgraded class Ccheckabletree [CHECK. ZIP] file can be downloaded from the Subscriber Downloads section of the URL: www.pinpub.com/vcd/.

The items that can be dragged and the target location of the release (draggable items and drop targets)

The drag-and-drop operation consists primarily of two behavior objects: the dragged item and the disposed target location item. To support repeatable drag-and-drop operations, you need to be able to identify which items in the tree control class are dragged and where they are disposed, rather than identifying the items in the parent window class. You can set the Drag property of an item by setting a specific bit value in a member variable m_draggable, just as the image of a tree can be set to the "extensible" attribute. The member variable m_draggable is used in functions setdraggable (), isdraggable (), Setdroptarget (), and IsDropTarget (). The following is the code for Function setdraggable ():

void CBitmapTree::SetDraggable(int base)
{
ASSERT(base < (sizeof(m_draggable)*8 -1));
m_draggable|= (1UL << base);
}

Readers familiar with class Cbitmaptree may notice that the function is very similar to the setexpandable () function. The variable base indicates the index number of the selected image in the Tvisl_normal image list of the tree control. The index number is converted to a unit value and stored in the member variable m_draggable. The m_draggable member variable type is a long integer and occupies 4 bytes 32 bits, so the variable base specifies an image in the first 32 images in the image list. The member function isdraggable () can be used to query the value of the variable m_draggable. member functions Isdraggable () and isdroptarget () are defined as virtual functions, so the user can override (overriding) the function in a derived class Cbitmaptree class if more complex judgments about the drag and release attributes are required. In the program example contained in the Subscriber Downloads section of the URL www.pinpub.com/vcd, all document nodes are dragged and all folder nodes can be used as the target location items to be released after dragging.

Ready to drag (getting ready to drag)

When a drag-and-drop operation is successfully performed, the function Setdrag () creates a callback function to handle the movement or replication of the tree item. The author has provided a simpler callback function MOVETREEITEMCB () in the previous article, which completes the tree item to move from one position to another in the tree control. Note: The limitations of this function are strong, and if you want to accomplish some of the functions that the function does not have, you need to write your own callback function as needed. (For example, when you use a macro lpstr_textcallback to replace the text that represents a tree item, the function MOVETREEITEMCB () does not work correctly.) )

In addition, when you drag a tree item, you can provide a resource identifier (not required) for a bitmap of the function Setdrag (). The bitmap can be used as a hint icon and should have the format shown in Figure 1. When the drag exceeds the bounds of the valid range, the first image is displayed, the second image is displayed when the item is only one item, and the third image is displayed when the item is dragged with multiple items. If you do not provide an image to the function Setdrag () in your program, a default drag image is displayed, which is the gray display of the current selection.

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.