Files
lserp_cs_6.0/插件库/Lskj.Control/Model/TreeGridDragGrid.cs
T
cyf ab56a9bcf7 基线 SVN r240
SVN-Revision: r240
2025-02-06 06:46:06 +00:00

232 lines
8.1 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 TreeGridDragGrid
{
/// <summary>
/// 拖动位置
/// </summary>
private GridHitInfo _hitInfo;
/// <summary>
/// 拖拽时节点
/// </summary>
TreeListHitInfo downHitInfo = null;
/// <summary>
/// 拖动数据源
/// </summary>
private TreeList _sourceTreeList;
/// <summary>
/// 拖动目标源
/// </summary>
private GridView _targetGridView;
/// <summary>
/// 是否正在拖拽
/// </summary>
private bool _draging = false;
public bool isDragCond = true;
/// <summary>
/// 是否正在拖拽
/// </summary>
public bool Draging { get { return _draging; } set { _draging = value; } }
public bool CanDragParentNode = false;
public bool isDropColorVisble = false;
/// <summary>
/// 表格数据拖动完成
/// </summary>
public event TreeGridDragGridCompleteEventHandler OnDragComplete;
public TreeGridDragGrid(TreeList treeList, GridView gridView)
{
this._sourceTreeList = treeList;
this._targetGridView = gridView;
if (this._sourceTreeList == null || this._targetGridView == 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._targetGridView.GridControl.AllowDrop = true;
this._targetGridView.GridControl.DragOver += new DragEventHandler(treeGridView_DragOver);
this._targetGridView.GridControl.DragEnter += new System.Windows.Forms.DragEventHandler(targetGridView_DragEnter);
this._targetGridView.GridControl.DragDrop += new DragEventHandler(targetGridView_DragDrop);
}
private void targetGridView_DragDrop(object sender, DragEventArgs e)
{
if (this.OnDragComplete != null && this._draging)
{
this._draging = false;
this.isDragCond = true;
TreeGridDragGridArgs args = new TreeGridDragGridArgs();
args.GridViewObj = this._targetGridView;
args.TreeListObj = this._sourceTreeList;
args.GridViewSelectRows = GetViewFocusedDataRows();
args.SelectGridRow = this._targetGridView.GetDataRow(this._targetGridView.FocusedRowHandle);
this.OnDragComplete(sender, args);
}
}
/// <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;
}
private void targetGridView_DragEnter(object sender, DragEventArgs e)
{
if (isDragCond)
{
e.Effect = DragDropEffects.Move;
}
else
{
e.Effect = DragDropEffects.None;
}
}
private void treeGridView_DragOver(object sender, DragEventArgs e)
{
TreeList treelist = sender as TreeList;
if (treelist != null)
{
e.Effect = DragDropEffects.Move;
}
GridControl targetControl = (sender as GridControl);
GridView targetGrid = targetControl.FocusedView as GridView;
Point gcPoint = targetControl.PointToScreen((sender as GridControl).Location);
Point pt = new Point(e.X - gcPoint.X, e.Y - gcPoint.Y);
_hitInfo = targetGrid.CalcHitInfo(pt);
if (_hitInfo == null || _hitInfo.RowHandle < 0) return;
if (!isDropColorVisble) targetGrid.SelectRow(_hitInfo.RowHandle);
targetGrid.FocusedRowHandle = _hitInfo.RowHandle;
}
/// <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;
}
}
}
}