[Control topic] A treectrl with the drag and drop function)

Source: Internet
Author: User

# If! Defined (afx_dragtree_h00003015f03f_045e_4531_a436_e2a890f235401_included _)
# Define afx_dragtree_h00003015f03f_045e_4531_a436_e2a890f235401_included _

# If _ msc_ver> 1000
# Pragma once
# Endif // _ msc_ver> 1000
// Dragtree. h: header file
//

//////////////////////////////////////// /////////////////////////////////////
// Cdragtree window

Class cdragtree: Public ctreectrl
{
// Construction
Public:
Cdragtree ();
 
Cimagelist * m_pdragimages; // drag the image list
Bool m_bdrag; // whether to drag
Htreeitem m_hbegindrag; // the start point of the drag.
// Attributes
Public:

// Operations
Public:

// Overrides
// Classwizard generated virtual function overrides
// {Afx_virtual (cdragtree)
//} Afx_virtual

// Implementation
Public:
Void copynodes (htreeitem hdesitem, htreeitem hsrcitem );
Virtual ~ Cdragtree ();

// Generated message map Functions
Protected:
// {Afx_msg (cdragtree)
Afx_msg void onbegindrag (nmhdr * pnmhdr, lresult * presult );
Afx_msg void onmousemove (uint nflags, cpoint point );
Afx_msg void onlbuttonup (uint nflags, cpoint point );
//} Afx_msg

Declare_message_map ()
};

//////////////////////////////////////// /////////////////////////////////////

// {Afx_insert_location }}
// Microsoft Visual C ++ will insert additional declarations immediately before the previous line.

# Endif //! Defined (afx_dragtree_h00003015f03f_045e_4531_a436_e2a890f235401_included _)

 

// Dragtree. cpp: implementation file
//

# Include "stdafx. H"
# Include "dragtreeitem. H"
# Include "dragtree. H"

# Ifdef _ debug
# Define new debug_new
# UNDEF this_file
Static char this_file [] = _ file __;
# Endif

//////////////////////////////////////// /////////////////////////////////////
// Cdragtree

Cdragtree: cdragtree ()
{
M_pdragimages = NULL;
M_bdrag = false;
M_hbegindrag = NULL;
}

Cdragtree ::~ Cdragtree ()
{
}

Begin_message_map (cdragtree, ctreectrl)
// {Afx_msg_map (cdragtree)
On_policy_reflect (tvn_begindrag, onbegindrag)
On_wm_mousemove ()
On_wm_lbuttonup ()
//} Afx_msg_map
End_message_map ()

//////////////////////////////////////// /////////////////////////////////////
// Cdragtree message handlers

Void cdragtree: onbegindrag (nmhdr * pnmhdr, lresult * presult)
{
Nm_treeview * pnmtreeview = (nm_treeview *) pnmhdr;
Htreeitem hitem = pnmtreeview-> itemnew. hitem; // get the Start Node
If (hitem = getrootitem () // The root node cannot be dragged.
{
* Presult = 0;
Return;
}
M_hbegindrag = hitem; // record the project to start dragging
M_pdragimages = createdragimage (hitem); // create a drag image list

Cpoint dragpt; // record start point
Dragpt. x = pnmtreeview-> ptdrag. X;
Dragpt. Y = pnmtreeview-> ptdrag. Y;
If (m_pdragimages! = NULL)
{
M_pdragimages-> begindrag (0, cpoint (8, 8); // start dragging the image
Clienttoscreen (& dragpt); // convert client coordinates to screen coordinates
M_pdragimages-> dragenter (this, dragpt); // lock the window update to display the dragged Image
Setcapture (); // start Mouse capture
M_bdrag = true;
}
* Presult = 0;
}

Void cdragtree: onmousemove (uint nflags, cpoint point)
{
If (m_bdrag) // dragging
{
Htreeitem hitem;
Uint nhitflags;
Crect clientrc;
Getclientrect (& clientrc); // obtain the customer Region
M_pdragimages-> dragmove (point); // you can specify the position of the dragged image.
// Highlight when the mouse passes
If (hitem = hittest (point, & nhitflags ))! = NULL)
{
Cimagelist: dragshownolock (false); // hide the dragged Image
Selectdroptarget (hitem); // sets the selected project
Cimagelist: dragshownolock (true); // display the dragged Image
}
}
Else
Ctreectrl: onmousemove (nflags, point );
}

Void cdragtree: onlbuttonup (uint nflags, cpoint point)
{
If (m_bdrag) // dragging
{
M_bdrag = false;
Cimagelist: dragleave (this );//
Cimagelist: enddrag (); // drag the end image
Releasecapture (); // release Mouse capture
Delete m_pdragimages; // release the image list
M_pdragimages = NULL;
Crect winrc;
Getwindowrect (& winrc); // obtain the window area
Htreeitem hitem;
If (hitem = hittest (point, & nflags ))! = NULL)
{
// Drag
// If the target project is the same as the project to be dragged, or the target project is still the parent node of the START project, no processing is performed.
If (m_hbegindrag! = Hitem & hitem! = Getparentitem (m_hbegindrag ))
{
Copynodes (hitem, m_hbegindrag); // copy a node
Deleteitem (m_hbegindrag); // Delete the source node
}
Invalidate ();
Selectdroptarget (null );
M_hbegindrag = NULL;
}
}
}

// Copy the hsrcitem and its sub-projects to the next hdesitem Node
Void cdragtree: copynodes (htreeitem hdesitem, htreeitem hsrcitem)
{

If (hdesitem = NULL | hsrcitem = NULL) // verify the Parameter
{
Return;
}
Tvitem; // define project information
Tvitem. Mask = tvif_text | tvif_image; // you can specify the flag to be returned.
Tvitem. hitem = hsrcitem;
Char chtext [max_path] = {0 };
Tvitem. psztext = chtext;
Tvitem. cchtextmax = max_path;
Getitem (& tvitem); // obtain project information
Tvinsertstruct tvinsert; // defines the data structure of the insert operation.
Tvinsert. hparent = hdesitem;
Tvinsert. Item = tvitem;
Htreeitem hinsert = insertitem (& tvinsert); // insert a project
Htreeitem hchild = getchilditem (hsrcitem); // obtain the subnode
While (hchild! = NULL) // traverse subnodes
{
Tvitem. Mask = tvif_text | tvif_image;
Tvitem. hitem = hchild;
Tvitem. psztext = chtext;
Tvitem. cchtextmax = max_path;
Getitem (& tvitem );
Tvinsert. hparent = hinsert;
Tvinsert. Item = tvitem;
Copynodes (hinsert, hchild); // recursive call
Hchild = getnextsiblingitem (hchild); // find the next sibling Node
}
}

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.