Files
lserp_cs_6.0/插件库/Lskj.Control/Model/TreeGridDragTree.cs
T
2026-07-02 10:11:39 +00:00

317 lines
11 KiB
C#

//树表格拖拽到树表格
using DevExpress.Utils;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;
using Lskj.Data;
using Lskj.Util;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Lskj.Control.Model
{
public class TreeGridDragTree
{
/// <summary>
/// 拖拽是否忽视节点
/// </summary>
public bool NeglectingNodes = false;
/// <summary>
/// 拖拽条件与目标树选中节点数据进行替换比较
/// </summary>
public string isDragCond;
/// <summary>
/// 是否允许拖拽
/// </summary>
public bool isAllowDrag = true;
/// <summary>
/// 拖动位置
/// </summary>
private GridHitInfo _hitInfo;
/// <summary>
/// 拖拽时节点
/// </summary>
TreeListHitInfo downHitInfo = null;
/// <summary>
/// 拖动数据源
/// </summary>
private TreeList _sourceTreeList;
/// <summary>
/// 拖动目标源
/// </summary>
private TreeList _targetTreeList;
/// <summary>
/// 是否正在拖拽
/// </summary>
private bool _draging = false;
/// <summary>
/// 是否正在拖拽
/// </summary>
public bool Draging { get { return _draging; } set { _draging = value; } }
public bool CanDragParentNode = false;
public bool isDropColorVisble = false;
/// <summary>
/// 表格数据拖动完成
/// </summary>
public event TreeGridDragTreeGridCompleteEventHandler OnDragComplete;
public TreeGridDragTree(TreeList sourceTreeList, TreeList targetTreeList)
{
this._sourceTreeList = sourceTreeList;
this._targetTreeList = targetTreeList;
if (this._sourceTreeList == null || this._targetTreeList == null)
return;
this._sourceTreeList.MouseDown += new MouseEventHandler(treeList_MouseDown);
this._sourceTreeList.MouseMove += new MouseEventHandler(treeList_MouseMove);
this._sourceTreeList.MouseUp += new MouseEventHandler(treeList_MouseUp);
//this._sourceTreeList.HideSelection = false;
this._sourceTreeList.AllowDrop = false;
this._targetTreeList.AllowDrop = true;
this._targetTreeList.DragOver += new DragEventHandler(treeView_DragOver);
this._targetTreeList.DragDrop += new DragEventHandler(treeView_DragDrop);
}
/// <summary>
/// <para>说明:获取选中行数据</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期:2019-10-18 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
/// <returns>DataRow[].</returns>
public DataRow[] GetViewFocusedDataRows()
{
var selectNodes = this._sourceTreeList.Selection;
DataRow[] dataRows = new DataRow[selectNodes.Count];
for (int i = 0; i < selectNodes.Count; i++)
{
TreeListNode treeNode = selectNodes[i];
DataRowView dataRowView = this._sourceTreeList.GetDataRecordByNode(treeNode) as DataRowView;
dataRows[i] = dataRowView != null ? dataRowView.Row : null;
}
return dataRows;
}
/// <summary>
/// <para>说明:源表格鼠标放开</para>
/// <para>创建人:</para>
/// <para>创建日期: </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
private void treeList_MouseUp(object sender, MouseEventArgs e)
{
this._draging = false;
}
/// <para>说明:源表格鼠标移动</para>
/// <para>创建人:</para>
/// <para>创建日期: </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
private void treeList_MouseMove(object sender, MouseEventArgs e)
{
TreeList treelist = sender as TreeList;
if (e.Button == MouseButtons.Left && downHitInfo != null)
{
if (_sourceTreeList.Selection.Count == 0)
return;
Size dragSize = SystemInformation.DragSize;
Rectangle dragRect = new Rectangle(new Point(downHitInfo.MousePoint.X - dragSize.Width / 2,
downHitInfo.MousePoint.Y - dragSize.Height / 2), dragSize);
if (!dragRect.Contains(new Point(e.X, e.Y)))
{
List<TreeListNode> node = new List<TreeListNode>();
foreach (TreeListNode n in _sourceTreeList.Selection)
{
node.Add(n);
}
treelist.DoDragDrop(node, DragDropEffects.Move);
downHitInfo = null;
DevExpress.Utils.DXMouseEventArgs.GetMouseArgs(e).Handled = true;
}
}
}
/// <summary>
/// <para>说明:源树表格鼠标按下</para>
/// <para>创建人:</para>
/// <para>创建日期:2023-3-20 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
private void treeList_MouseDown(object sender, MouseEventArgs e)
{
//this._sourceTreeList.AllowDrop = false;
//this._targetGridView.GridControl.AllowDrop = true;
TreeList treelist = sender as TreeList;
downHitInfo = null;
_hitInfo = null;
TreeListHitInfo hitInfo = treelist.CalcHitInfo(new Point(e.X, e.Y));
// if (Control.ModifierKeys != Keys.None) return;
if (e.Button == MouseButtons.Left && hitInfo != null)
{
StaticControl.SourceDragGridView = null;
this._draging = true;
downHitInfo = hitInfo;
}
}
/// <summary>
/// <para>说明:</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期:2017-11-13 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="DragEventArgs"/> instance containing the event data.</param>
protected void treeView_DragOver(object sender, DragEventArgs e)
{
if (e.KeyState == (int)Keys.LButton && this._draging)
{
TreeList treeListGrid = sender as TreeList;
treeListGrid.Select();
Point gcPoint = treeListGrid.PointToScreen((sender as TreeList).Location);
Point pt = new Point(e.X - gcPoint.X, e.Y - gcPoint.Y);
TreeListHitInfo _hitInfo = treeListGrid.CalcHitInfo(pt);
if (NeglectingNodes)
{
e.Effect = DragDropEffects.Move;
if (_hitInfo != null && _hitInfo.Node != null)
{
_hitInfo.Node.Expanded = true;
}
}
else
{
if (_hitInfo == null || treeListGrid.GetNodeIndex(_hitInfo.Node) < 0)
{
e.Effect = DragDropEffects.None;
return;
}
DataRowView dataRowView = this._targetTreeList.GetDataRecordByNode(_hitInfo.Node) as DataRowView;
DataRow Row = dataRowView != null ? dataRowView.Row : null;
bool isLastNode = _hitInfo.Node.Nodes.Count == 0;
if (!isLastNode)
{
_hitInfo.Node.Expanded = true; ;
}
//DataRow[] rowItems = this.GetViewFocusedDataRows();
if (isLastNode || CanDragParentNode)
{
if (ReplaceHelper.ReplaceRowParamCond(Row, this.isDragCond) && isAllowDrag)
{
e.Effect = DragDropEffects.Move;
treeListGrid.FocusedNode = _hitInfo.Node;
}
else
{
e.Effect = DragDropEffects.None;
}
}
else
{
e.Effect = DragDropEffects.None;
}
}
}
}
/// <summary>
/// <para>说明:</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期:2017-11-13 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
/// <param name="sender">The source of the event.</param>
/// <param name="e">The <see cref="DragEventArgs"/> instance containing the event data.</param>
protected void treeView_DragDrop(object sender, DragEventArgs e)
{
if (this.OnDragComplete != null && this._draging)
{
this._draging = false;
TreeList treeListGrid = sender as TreeList;
Point gcPoint = treeListGrid.PointToScreen((sender as TreeList).Location);
Point pt = new Point(e.X - gcPoint.X, e.Y - gcPoint.Y);
TreeListHitInfo _hitInfo = treeListGrid.CalcHitInfo(pt);
TreeListNode treeNode = _hitInfo.Node;
if (!NeglectingNodes)
{
if (treeNode == null) return;
if (treeNode.Nodes.Count() > 0 && !CanDragParentNode)
{
MessageUtil.Show(string.Format(ResourceKeys.UnLastTreeNode, treeNode.GetDisplayText(treeListGrid.KeyFieldName)));
return;
}
}
if (!e.Data.GetDataPresent(typeof(TreeNode)))
{
if (OnDragComplete != null)
{
TreeGridDragTreeGridArgs args = new TreeGridDragTreeGridArgs();
args.SourceTreeList = this._sourceTreeList;
args.TargetTreeList = this._targetTreeList;
args.GridViewSelectRows = this.GetViewFocusedDataRows(); ;
args.SelectTreeNode = treeNode;
OnDragComplete(sender, args);
}
}
}
}
}
}