一个拖拽树控件

来源:互联网 发布:淘宝店流量 编辑:程序博客网 时间:2024/06/06 02:08

using System;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
namespace jy_controls
{
 /// <summary>;
 /// jyTreeView 的摘要说明。
 /// </summary>
 public  class jyTreeViewA : System.Windows.Forms.TreeView
 {
  public  jyTreeViewA()
  {
   //
   // TODO: 在此处添加构造函数逻辑
   //
   ShowScrollBar(this.Handle,SB_HORZ,false);//去掉水平滚动条,周 2006-3-23
  }

  bool _ishandonnode = false;
  bool _ishandonimage = false;
  bool _candrap = false;
  int _imageCollapse;
  int _imageExpand;
  #region API
  [DllImport("gdi32.dll")]
  static extern IntPtr CreateCompatibleDC(IntPtr hdc);
  [DllImport("user32.dll")]
  static extern bool ShowScrollBar(IntPtr hWnd, int wBar, bool bShow); 
  #endregion
  #region 委托事件
  public delegate void hImageMousedown(object NodeTag,MouseEventArgs e);
  public event hImageMousedown imgMouseDown;

  public delegate void hDragDrop(object Items);
  public event hDragDrop DragDropItem;
  #endregion
  #region 属性
  /// <summary>
  /// 鼠标停留在节点上是否变成手形,同时也接受单击事件
  /// </summary>
  public bool IsHandOnNode
  {
   get{return this._ishandonnode;}
   set{this._ishandonnode = value;}
  }
  /// <summary>
  /// 鼠标停留在图片上是否变成手形
  /// </summary>
  public bool IsHandOnImage
  {
   get{return this._ishandonimage;}
   set{this._ishandonimage = value;}
  }
  /// <summary>
  /// 设置可以拖拉节点
  /// </summary>
  public bool CanDrap
  {
   get{return this._candrap;}
   set{this._candrap = value;}
  }

  public int CollapseImage
  {
   get
   {
    return this._imageCollapse;
   }
   set
   {
    _imageCollapse = value;
   }
  }

  public int ExpandImage
  {
   get
   {
    return _imageExpand;
   }
   set
   {
    _imageExpand = value;
   }
  }
  private bool _isRightDownSelected;
  public bool IsRightDownSelected
  {
   get
   {
    return _isRightDownSelected;
   }
   set
   {
    _isRightDownSelected = value;
   }
  }
  #endregion
  #region 移动树节点

  protected override void OnItemDrag(System.Windows.Forms.ItemDragEventArgs e)
  {
   // TreeNode myNode = (TreeNode)(drgevent.Data.GetData(typeof(TreeNode)));
   // TreeNode myNode = (TreeNode)e.Item;
   // MessageBox.Show(myNode.Parent.Text);

   base.OnItemDrag (e);
   TreeNode tn=(TreeNode)e.Item;
   DoDragDrop(e.Item, DragDropEffects.Move);
  }

  protected override void OnDragEnter(System.Windows.Forms.DragEventArgs drgevent)
  {
   base.OnDragEnter(drgevent);
   if(this._candrap)
   {
    drgevent.Effect = DragDropEffects.Move; 
   }
  }
  
  bool _istopnode = true;
  /// <summary>
  /// 设置为真时,只有顶级节点才接收拖拉过来的节点
  /// </summary>
  public bool IsTopNode
  {
   get{return _istopnode;}
   set{_istopnode = value;}
  }
  protected override void OnDragOver(DragEventArgs drgevent)
  {
   base.OnDragOver (drgevent);
   if(this._candrap)
   {
    System.Drawing.Point Position = new System.Drawing.Point();
    Position.X = drgevent.X;
    Position.Y = drgevent.Y;
    Position = this.PointToClient(Position);
    TreeNode DropNode = this.GetNodeAt(Position);
    if(drgevent.Data.GetDataPresent(typeof(TreeNode)))
    {
     TreeNode myNode = (TreeNode)(drgevent.Data.GetData(typeof(TreeNode)));
     if (DropNode != null && !CheckNodePar(DropNode, myNode) && DropNode != myNode)
     {
      if(_istopnode)
      {
       if(DropNode.Parent == null)
       {
        this.SelectedNode = DropNode;
       }
       else
       {
        this.SelectedNode = DropNode.Parent;
       }
      }
      else
      {
       this.SelectedNode = DropNode;
      }
     }
    }
    else
    {
     if (DropNode != null)
     {
      if(_istopnode)
      {
       if(DropNode.Parent == null)
       {
        this.SelectedNode = DropNode;
       }
       else
       {
        this.SelectedNode = DropNode.Parent;
       }
      }
      else
      {
       this.SelectedNode = DropNode;
      }
     }
    }
    
   }
  }

  protected override void OnDragDrop(System.Windows.Forms.DragEventArgs drgevent)
  {
   base.OnDragDrop (drgevent);
   
   if(this._candrap)
   {
    TreeNode myNode = null;
    TreeNode DropNode = this.SelectedNode;
    try
    {
     if (drgevent.Data.GetDataPresent(typeof(TreeNode)))
     {
      myNode = (TreeNode)(drgevent.Data.GetData(typeof(TreeNode)));
     
      if (DropNode != null && !CheckNodePar(DropNode, myNode) && DropNode != myNode)
      {
       TreeNode DragNode = myNode;     
       if(_istopnode)
       {
        if(DropNode.Parent == null)
        {
         string NewGroupName=DropNode.Text;
         string OldGroupName=DragNode.Parent.Text;
         myNode.Remove();
         DropNode.Nodes.Add(DragNode);
         if(DragDropItem != null)
         {
          this.DragDropItem(DragNode);
         }
        }
       }
       else
       {
        myNode.Remove();
        DropNode.Nodes.Add(DragNode);
        if(DragDropItem != null)
        {
         this.DragDropItem(DragNode);
        }
       }
      
      }
     }
     else if(drgevent.Data.GetDataPresent(typeof(ListViewItem)))
     {
      ListViewItem item = (ListViewItem)drgevent.Data.GetData(typeof(ListViewItem));
      if(DragDropItem != null)
      {
       this.DragDropItem(item);
      }
      else
      {
       TreeNode node = new TreeNode();
       node.Text = item.Text;
       node.Tag = item;
       DropNode.Nodes.Add(node);
      }
     }
     else if(drgevent.Data.GetDataPresent(typeof(object)))
     {
      if(DragDropItem != null)
      {
       this.DragDropItem(drgevent.Data.GetData(typeof(object)));
      }
      else      
      {
       TreeNode node = new TreeNode();
       node.Text = drgevent.Data.GetData(typeof(object)).ToString();
       node.Tag = drgevent.Data.GetData(typeof(object));
       DropNode.Nodes.Add(node);
      }
     }
     else
     {
      if(DragDropItem != null)
      {
       this.DragDropItem(drgevent);
      }
     }
    }
    catch{}
   }
  }


  
  #endregion

 

  Rectangle rl = Rectangle.Empty;
  protected override void OnMouseMove(MouseEventArgs e)
  {
   base.OnMouseMove (e);
   if(this.ImageList == null) return;
   Graphics g = this.CreateGraphics();
   TreeNode tn = this.GetNodeAt(new Point(e.X,e.Y));   
   if(tn != null)
   {
    if(tn.Tag!=null)
    {
     int x = tn.TreeView.ImageList.Images[0].Width;
     int y = tn.TreeView.ImageList.Images[0].Height;
     if(e.X - tn.Bounds.X <= tn.Bounds.Width
      && e.X - tn.Bounds.X >=0
      && e.Y - tn.Bounds.Y <= tn.Bounds.Height
      && e.Y - tn.Bounds.Y>=0)
     {
      if(this._ishandonnode)
      {
       Cursor.Current = Cursors.Hand;
      }
     }
     else if(e.X>=tn.Bounds.X-x-4
      && e.X<=tn.Bounds.X-4
      && e.Y<tn.Bounds.Y+y
      && e.Y>tn.Bounds.Y && this._ishandonimage)
     {
      Cursor.Current = Cursors.Hand;
      //tn.ImageIndex =1;
      if(rl == Rectangle.Empty)
      {
       rl = new Rectangle(tn.Bounds.X-x-4,tn.Bounds.Y+2,x,y);
       g.DrawRectangle(new Pen(Color.FromArgb(104,160,255),1),rl);
      }
     }
     else
     {
      if(rl != Rectangle.Empty)
      {
       //tn.ImageIndex = 0;
       g.DrawRectangle(new Pen(this.BackColor,1),rl);
       rl = Rectangle.Empty;
      }
      Cursor.Current = Cursors.Default;
     }
    }
   }
   else
   {
    if(rl != Rectangle.Empty)
    {
     //tn.ImageIndex = 0;
     g.DrawRectangle(new Pen(this.BackColor,1),rl);
     rl = Rectangle.Empty;
    }
   }
   
   g.Dispose();
  }
  protected override void OnMouseDown(MouseEventArgs e)
  {
   if(this._isRightDownSelected)
   {
    TreeNode DownNode = this.GetNodeAt(e.X,e.Y);
    if(DownNode != null)
    {
     this.SelectedNode = DownNode;
    }
   }
   if(this.ImageList != null)
   {
    if(this._ishandonimage)
    {
     TreeNode tn = this.GetNodeAt(new Point(e.X,e.Y));
     if(tn != null)
     {
      if(tn.Tag!=null)
      {
       int x = tn.TreeView.ImageList.Images[0].Width;
       int y = tn.TreeView.ImageList.Images[0].Height;
       if(e.X>tn.Bounds.X-x-4
        && e.X<tn.Bounds.X-4
        && e.Y<tn.Bounds.Y+y
        && e.Y>tn.Bounds.Y)
       {
        if(this.imgMouseDown != null)
        {
         this.imgMouseDown(tn.Tag,e);//通知单击了image
        }
       }
      }
     }
    }
   }
   base.OnMouseDown (e);
  } 
  protected override void OnAfterCollapse(TreeViewEventArgs e)
  {
   base.OnAfterCollapse (e);
   if(this.ImageList == null) return;
   if(this._imageCollapse != 0 && e.Node.ImageIndex != this._imageCollapse)
   {
    e.Node.SelectedImageIndex = e.Node.ImageIndex = this._imageCollapse;
   }
  }
  protected override void OnAfterExpand(TreeViewEventArgs e)
  {
   base.OnAfterExpand (e);
   if(this.ImageList == null) return;
   if(this._imageExpand != 0 && e.Node.ImageIndex != this._imageExpand)
   {
    e.Node.ImageIndex = this._imageExpand;
    e.Node.SelectedImageIndex = this._imageExpand;
   }
  }
  
  
  protected override void WndProc(ref System.Windows.Forms.Message m)
  { 
   switch(m.Msg)
   {
    default:
     base.WndProc (ref m);
     break;
   }
   
  }
  protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
  {
   
   base.OnPaint (e);
  }

  /// <summary>
  /// 检查node2是否node1的父节点
  /// </summary>
  /// <param name="node1"></param>
  /// <param name="node2"></param>
  /// <returns></returns>
  private bool CheckNodePar(TreeNode node1,TreeNode node2)
  {
   TreeNode p = node1.Parent;
   if(p != null)
   {
    if(p == node2)
    {
     return true;
    }
    return CheckNodePar(p,node2);
   }
   else
   {
    return false;
   }
  }
  #region 消息
  int SB_HORZ = 0;
  public  const long GWL_STYLE  = -16;
  public  const long COLOR_WINDOW = 5 ;
  public  const long COLOR_WINDOWTEXT = 8 ;

  public  const long TVI_ROOT  = 0xFFFF0000 ;
  public  const long TVI_FIRST  = 0xFFFF0001 ;
  public  const long TVI_LAST  = 0xFFFF0002 ;
  public  const long TVI_SORT  = 0xFFFF0003 ;

  public  const long TVIF_STATE  = 0x8 ;

                                   
  public  const long TVS_HASLINES  = 2 ;
  public  const long TVS_FULLROWSELECT  = 0x1000 ;

             
  public  const long TVIS_BOLD  = 0x10 ;

  public  const long TV_FIRST  = 0x1100 ;
  public  const long TVM_GETNEXTITEM  = (TV_FIRST + 10) ;
  public  const long TVM_GETITEM  = (TV_FIRST + 12) ;
  public  const long TVM_SETITEM  = (TV_FIRST + 13) ;
  public  const long TVM_SETBKCOLOR  = (TV_FIRST + 29) ;
  public  const long TVM_SETTEXTCOLOR  = (TV_FIRST + 30) ;
  public  const long TVM_GETBKCOLOR  = (TV_FIRST + 31) ;
  public  const long TVM_GETTEXTCOLOR  = (TV_FIRST + 32) ;

  public  const long TVGN_ROOT  = 0x0 ;
  public  const long TVGN_NEXT  = 0x1 ;
  public  const long TVGN_PREVIOUS  = 0x2 ;
  public  const long TVGN_PARENT  = 0x3 ;
  public  const long TVGN_CHILD  = 0x4 ;
  public  const long TVGN_FIRSTVISIBLE  = 0x5 ;
  public  const long TVGN_NEXTVISIBLE  = 0x6 ;
  public  const long TVGN_PREVIOUSVISIBLE  = 0x7 ;
  public  const long TVGN_DROPHILITE  = 0x8 ;
  public  const long TVGN_CARET  = 0x9 ;

  public  struct TVITEM
  {
   long  mask  ;
   long hItem  ;
   long state  ;
   long stateMask  ;
   string   pszText ;
   long cchTextMax;
   long iImage;
   long iSelectedImage  ;
   long cChildren  ;
   long lParam;
  }
                                                                                                                                    
  #endregion


 }
 public class DropType
 {
  public const string TreeNode = "System.Windows.Forms.TreeNode";
  public const string ListViewItem = "System.Windows.Forms.ListViewItem";
 }
}

原创粉丝点击