258 lines
9.1 KiB
C#
258 lines
9.1 KiB
C#
/******************************
|
|
* 说明:表格拖拽到树结构
|
|
* 创建人:龚宇超
|
|
* 创建日期:2017-11-13
|
|
* 修改人:
|
|
* 修改日期:
|
|
* 修改备注:
|
|
* 版本:1.0.0.0
|
|
******************************/
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
|
|
using DevExpress.XtraGrid.Views.Grid;
|
|
using System.Windows.Forms;
|
|
using Lskj.Data;
|
|
using System.Drawing;
|
|
using DevExpress.Utils;
|
|
using System.Data;
|
|
|
|
namespace Lskj.Control.Model
|
|
{
|
|
/// <summary>
|
|
/// 表格拖拽到树结构
|
|
/// </summary>
|
|
public class GridDragTree : IDisposable
|
|
{
|
|
private bool _disposed;
|
|
/// <summary>
|
|
/// 拖动位置
|
|
/// </summary>
|
|
private GridHitInfo _hitInfo;
|
|
/// <summary>
|
|
/// 拖动数据源
|
|
/// </summary>
|
|
private GridView _gridView;
|
|
/// <summary>
|
|
/// 拖动目标源
|
|
/// </summary>
|
|
private TreeView _treeView;
|
|
public bool CanDragParentNode = false;
|
|
/// <summary>
|
|
/// 表格数据拖动完成
|
|
/// </summary>
|
|
public event GridDragTreeCompleteEventHandler OnDragComplete;
|
|
|
|
public GridDragTree(GridView gridView, TreeView treeView)
|
|
{
|
|
this._gridView = gridView;
|
|
this._treeView = treeView;
|
|
|
|
if (this._gridView == null || this._treeView == null)
|
|
return;
|
|
|
|
this._gridView.MouseDown += new MouseEventHandler(gridView_MouseDown);
|
|
this._gridView.MouseMove += new MouseEventHandler(gridView_MouseMove);
|
|
|
|
this._treeView.HideSelection = false;
|
|
this._treeView.AllowDrop = true;
|
|
this._treeView.DragOver += new DragEventHandler(treeView_DragOver);
|
|
this._treeView.DragDrop += new DragEventHandler(treeView_DragDrop);
|
|
if (this._gridView.GridControl != null)
|
|
{
|
|
this._gridView.GridControl.Disposed += OnOwnerDisposed;
|
|
}
|
|
this._treeView.Disposed += OnOwnerDisposed;
|
|
}
|
|
|
|
private void OnOwnerDisposed(object sender, EventArgs e)
|
|
{
|
|
Dispose();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 模块关闭时解除源表格和目标树的拖拽事件。
|
|
/// </summary>
|
|
public void Dispose()
|
|
{
|
|
if (_disposed) return;
|
|
_disposed = true;
|
|
|
|
if (_gridView != null)
|
|
{
|
|
_gridView.MouseDown -= gridView_MouseDown;
|
|
_gridView.MouseMove -= gridView_MouseMove;
|
|
if (_gridView.GridControl != null)
|
|
{
|
|
_gridView.GridControl.Disposed -= OnOwnerDisposed;
|
|
}
|
|
}
|
|
if (_treeView != null)
|
|
{
|
|
_treeView.DragOver -= treeView_DragOver;
|
|
_treeView.DragDrop -= treeView_DragDrop;
|
|
_treeView.Disposed -= OnOwnerDisposed;
|
|
}
|
|
|
|
OnDragComplete = null;
|
|
_hitInfo = null;
|
|
_gridView = null;
|
|
_treeView = null;
|
|
}
|
|
|
|
|
|
/// <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;
|
|
|
|
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._hitInfo = 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="MouseEventArgs"/> instance containing the event data.</param>
|
|
protected void gridView_MouseMove(object sender, MouseEventArgs e)
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
}
|
|
/// <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)
|
|
{
|
|
TreeView treeView = sender as TreeView;
|
|
treeView.Select();
|
|
|
|
TreeNode treeNode = treeView.GetNodeAt(treeView.PointToClient(new Point(e.X, e.Y)));
|
|
if (treeNode != null)
|
|
{
|
|
bool isLastNode = treeNode.Nodes.Count == 0;
|
|
if (!isLastNode)
|
|
{
|
|
treeNode.Expand();
|
|
}
|
|
|
|
DataRow[] rowItems = this.GetGridSelectRows(this._gridView.GetSelectedRows());
|
|
|
|
if (isLastNode || CanDragParentNode)
|
|
{
|
|
e.Effect = DragDropEffects.Move;
|
|
treeView.SelectedNode = treeNode;
|
|
}
|
|
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)
|
|
{
|
|
TreeView treeView = sender as TreeView;
|
|
TreeNode treeNode = treeView.GetNodeAt(treeView.PointToClient(new Point(e.X, e.Y)));
|
|
|
|
if (treeNode == null) return;
|
|
if (treeNode.GetNodeCount(true) > 0 && !CanDragParentNode)
|
|
{
|
|
MessageUtil.Show(string.Format(ResourceKeys.UnLastTreeNode, treeNode.Text));
|
|
return;
|
|
}
|
|
if (!e.Data.GetDataPresent(typeof(TreeNode)))
|
|
{
|
|
if (OnDragComplete != null)
|
|
{
|
|
GridDragTreeArgs args = new GridDragTreeArgs();
|
|
args.GridViewObj = this._gridView;
|
|
args.TreeViewObj = this._treeView;
|
|
args.GridViewSelectRows = this.GetGridSelectRows(this._gridView.GetSelectedRows());
|
|
args.SelectTreeNode = treeNode;
|
|
|
|
OnDragComplete(sender, args);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|