/// <summary> /// 在将对象拖出控件的边界时发生。 /// </summary> /// <param name="e"></param> protected override void OnDragLeave(EventArgs e) { DragHelper.ImageList_DragLeave(this.Handle); // Disable timer for scrolling dragged item this._timer.Enabled = false; base.OnDragLeave(e); }
/// <summary> /// 在将对象拖入控件的边界时发生。 /// </summary> /// <param name="drgevent"></param> protected override void OnDragEnter(DragEventArgs e) { DragHelper.ImageList_DragEnter(this.Handle, e.X - this.Left, e.Y - this.Top); // Enable timer for scrolling dragged item this._timer.Enabled = true; base.OnDragEnter(e); }
/// <summary> /// Begin dragging image /// 当用户开始拖动节点时,调用此方法 /// 在调用 this.DoDragDrop 之后,还需要调用 DrawEndDrag /// </summary> /// <returns></returns> protected bool DrawOnItemDrag() { if (_dragNode == null) { return(false); } // Reset image list used for drag image this._imageListDrag.Images.Clear(); this._imageListDrag.ImageSize = new Size(this._dragNode.Bounds.Size.Width + this.Indent, this._dragNode.Bounds.Height); // Create new bitmap // This bitmap will contain the tree node image to be dragged Bitmap bmp = new Bitmap(this._dragNode.Bounds.Width + this.Indent, this._dragNode.Bounds.Height); // Get graphics from bitmap Graphics gfx = Graphics.FromImage(bmp); //gfx.Clear(SystemColors.GradientInactiveCaption); // Draw node icon into the bitmap //cxs //gfx.DrawImage(this.imageListTreeView.Images[0], 0, 0); if (this.ImageList != null && this._dragNode.ImageIndex < this.ImageList.Images.Count && this._dragNode.ImageIndex >= 0) { gfx.DrawImage(this.ImageList.Images[this._dragNode.ImageIndex], 0, 0); } // Draw node label into bitmap gfx.DrawString(this._dragNode.Text, this.Font, new SolidBrush(this.ForeColor), (float)this.Indent, 1.0f); // Add bitmap to imagelist this._imageListDrag.Images.Add(bmp); // Get mouse position in client coordinates Point p = this.PointToClient(Control.MousePosition); // Compute delta between mouse position and node bounds int dx = p.X + this.Indent - this._dragNode.Bounds.Left; int dy = p.Y - this._dragNode.Bounds.Top; // Begin dragging image return(DragHelper.ImageList_BeginDrag(this._imageListDrag.Handle, 0, dx, dy)); }
private void timer_Tick(object sender, EventArgs e) { // get node at mouse position Point pt = PointToClient(Control.MousePosition); TreeNode node = this.GetNodeAt(pt); if (node == null) { return; } // if mouse is near to the top, scroll up if (pt.Y < 30) { // set actual node to the upper one if (node.PrevVisibleNode != null) { node = node.PrevVisibleNode; // hide drag image DragHelper.ImageList_DragShowNolock(false); // scroll and refresh node.EnsureVisible(); this.Refresh(); // show drag image DragHelper.ImageList_DragShowNolock(true); } } // if mouse is near to the bottom, scroll down else if (pt.Y > this.Size.Height - 30) { if (node.NextVisibleNode != null) { node = node.NextVisibleNode; DragHelper.ImageList_DragShowNolock(false); node.EnsureVisible(); this.Refresh(); DragHelper.ImageList_DragShowNolock(true); } } }
/// <summary> /// 在将对象拖到控件的边界上发生。 /// </summary> /// <param name="drgevent"></param> protected override void OnDragOver(DragEventArgs e) { // Compute drag position and move image Point formP = this.PointToClient(new Point(e.X, e.Y)); //DragHelper.ImageList_DragMove(formP.X - this.Left, formP.Y - this.Top); DragHelper.ImageList_DragMove(formP.X, formP.Y); // Get actual drop node TreeNode dropNode = this.GetNodeAt(this.PointToClient(new Point(e.X, e.Y))); if (dropNode == null) { e.Effect = DragDropEffects.None; return; } e.Effect = DragDropEffects.Move; // if mouse is on a new node select it if (this._tempDropNode != dropNode) { DragHelper.ImageList_DragShowNolock(false); this.SelectedNode = dropNode; DragHelper.ImageList_DragShowNolock(true); _tempDropNode = dropNode; } // Avoid that drop node is child of drag node TreeNode tmpNode = dropNode; while (tmpNode.Parent != null) { if (tmpNode.Parent == this._dragNode) { e.Effect = DragDropEffects.None; } tmpNode = tmpNode.Parent; } base.OnDragOver(e); }
/// <summary> /// Unlock updates /// 在拖放操作完成时发生时,调用此方法 /// </summary> protected void DrawOnDragDrop() { // Unlock updates DragHelper.ImageList_DragLeave(this.Handle); }
/// <summary> /// End dragging image /// 停止绘制 /// </summary> protected void DrawEndDrag() { DragHelper.ImageList_EndDrag(); }