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

296 lines
12 KiB
C#

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
{
/// <summary>
/// 拖动位置
/// </summary>
private GridHitInfo _hitInfo;
/// <summary>
/// 拖拽时节点
/// </summary>
private TreeListNode _beforeListNode;
/// <summary>
/// 拖动数据源
/// </summary>
private GridView _gridView;
/// <summary>
/// 拖动目标源
/// </summary>
private TreeList _treeListGrid;
/// <summary>
/// 是否正在拖拽
/// </summary>
private bool _draging = false;
/// <summary>
/// 拖拽条件与目标树选中节点数据进行替换比较
/// </summary>
public string isDragCond;
/// <summary>
/// 是否允许拖拽
/// </summary>
public bool isAllowDrag = true;
/// <summary>
/// 拖拽是否忽视节点(不忽视必须要推拽到节点上)
/// </summary>
public bool NeglectingNodes = false;
/// <summary>
/// 是否正在拖拽
/// </summary>
public bool Draging { get { return _draging; } set { _draging = value; } }
public bool CanDragParentNode = false;
public bool isDropColorVisble = false;
/// <summary>
/// 表格数据拖动完成
/// </summary>
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);
}
/// <summary>
/// <para>说明:获取表格选中行数据</para>
/// <para>创建人:龚宇超</para>
/// <para>创建日期:2017-11-13 </para>
/// <para>修改人:</para>
/// <para>修改日期:</para>
/// <para>修改备注:</para>
/// <para>版本:1.0</para>
/// </summary>
/// <param name="rowHandles">The row handles.</param>
/// <returns>DataRow[].</returns>
private DataRow[] GetGridSelectRows(int[] rowHandles)
{
List<DataRow> rows = new List<DataRow>();
foreach (int handle in rowHandles)
{
DataRow row = this._gridView.GetDataRow(handle);
rows.Add(row);
}
return rows.ToArray();
}
/// <summary>
/// <para>说明:GridView 鼠标按下</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="MouseEventArgs"/> instance containing the event data.</param>
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;
}
}
/// <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="MouseEventArgs"/> instance containing the event data.</param>
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;
}
}
/// <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="System.Windows.Forms.MouseEventArgs"/> instance containing the event data.</param>
protected void gridView_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
this._draging = false;
}
/// <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._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;
}
}
}
}
/// <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)
{
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);
}
}
}
}
}
}