using DevExpress.Utils; 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 GridDragTreeGrid { /// /// 拖动位置 /// private GridHitInfo _hitInfo; /// /// 拖拽时节点 /// private TreeListNode _beforeListNode; /// /// 拖动数据源 /// private GridView _gridView; /// /// 拖动目标源 /// private TreeList _treeListGrid; /// /// 是否正在拖拽 /// private bool _draging = false; /// /// 拖拽条件与目标树选中节点数据进行替换比较 /// public string isDragCond; /// /// 是否允许拖拽 /// public bool isAllowDrag = true; /// /// 拖拽是否忽视节点(不忽视必须要推拽到节点上) /// public bool NeglectingNodes = false; /// /// 是否正在拖拽 /// public bool Draging { get { return _draging; } set { _draging = value; } } public bool CanDragParentNode = false; public bool isDropColorVisble = false; /// /// 表格数据拖动完成 /// public event GridDragTreeGridCompleteEventHandler OnDragComplete; public GridDragTreeGrid(GridView gridView, TreeList treeListGrid) { this._gridView = gridView; this._treeListGrid = treeListGrid; if (this._gridView == null || this._treeListGrid == null) return; this._gridView.MouseDown += new MouseEventHandler(gridView_MouseDown); this._gridView.MouseMove += new MouseEventHandler(gridView_MouseMove); this._gridView.MouseUp += new MouseEventHandler(gridView_MouseUp); //this._treeListGrid.HideSelection = false; this._treeListGrid.AllowDrop = true; this._treeListGrid.DragOver += new DragEventHandler(treeView_DragOver); this._treeListGrid.DragDrop += new DragEventHandler(treeView_DragDrop); } /// /// 说明:获取表格选中行数据 /// 创建人:龚宇超 /// 创建日期:2017-11-13 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The row handles. /// DataRow[]. private DataRow[] GetGridSelectRows(int[] rowHandles) { List rows = new List(); foreach (int handle in rowHandles) { DataRow row = this._gridView.GetDataRow(handle); rows.Add(row); } return rows.ToArray(); } /// /// 说明:GridView 鼠标按下 /// 创建人:龚宇超 /// 创建日期:2017-11-13 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The source of the event. /// The instance containing the event data. protected void gridView_MouseDown(object sender, MouseEventArgs e) { this._hitInfo = null; this._beforeListNode = null; if (System.Windows.Forms.Control.ModifierKeys != Keys.None) return; GridView gridView = sender as GridView; GridHitInfo hitInfo = gridView.CalcHitInfo(new Point(e.X, e.Y)); if (e.Button == MouseButtons.Left && hitInfo.RowHandle >= 0) { this._draging = true; this._hitInfo = hitInfo; this._beforeListNode = this._treeListGrid.FocusedNode; } } /// /// 说明:鼠标移动 /// 创建人:龚宇超 /// 创建日期:2017-11-13 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The source of the event. /// The instance containing the event data. protected void gridView_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left && this._draging) { GridView gridView = sender as GridView; if (e.Button == MouseButtons.Left && this._hitInfo != null) { Size dragSize = SystemInformation.DragSize; Rectangle dragRect = new Rectangle(new Point(this._hitInfo.HitPoint.X - dragSize.Width / 2, this._hitInfo.HitPoint.Y - dragSize.Height / 2), dragSize); if (!dragRect.Contains(new Point(e.X, e.Y))) { object row = gridView.GetRow(this._hitInfo.RowHandle); gridView.GridControl.DoDragDrop(row, DragDropEffects.Move); this._hitInfo = null; DXMouseEventArgs.GetMouseArgs(e).Handled = true; } } } else { this._draging = false; } } /// /// 说明:源表格鼠标放开 /// 创建人:龚宇超 /// 创建日期:2017-11-13 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The source of the event. /// The instance containing the event data. protected void gridView_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) { this._draging = false; } /// /// 说明: /// 创建人:龚宇超 /// 创建日期:2017-11-13 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The source of the event. /// The instance containing the event data. 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._treeListGrid.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.GetGridSelectRows(this._gridView.GetSelectedRows()); 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; } } } } /// /// 说明: /// 创建人:龚宇超 /// 创建日期:2017-11-13 /// 修改人: /// 修改日期: /// 修改备注: /// 版本:1.0 /// /// The source of the event. /// The instance containing the event data. 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) { GridDragTreeGridArgs args = new GridDragTreeGridArgs(); args.GridViewObj = this._gridView; args.TreeListObj = this._treeListGrid; args.GridViewSelectRows = this.GetGridSelectRows(this._gridView.GetSelectedRows()); args.SelectTreeNode = treeNode; args.BeforeTreeNode = this._beforeListNode; OnDragComplete(sender, args); } } } } } }