using DevExpress.XtraGrid.Views.Grid;
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
namespace HL_FristAidPlatform_Public
{
public class UGridControl : DevExpress.XtraGrid.GridControl
{
private GridView gridView = null;
private Point m_mouseDownLocation;
private int m_dragHandle;
private DragForm m_dragRowShadow;
///
///
///
///
protected override void OnMouseDown(MouseEventArgs ev)
{
if (ev.Button == MouseButtons.Left)
{
if (gridView == null)
{
gridView = ((GridView)base.MainView);
}
var _hit = gridView.CalcHitInfo(ev.Location);
if (_hit.RowHandle >= 0)
{
m_dragHandle = _hit.RowHandle;
m_mouseDownLocation = ev.Location;
}
else
{
m_dragHandle = -1;
}
}
base.OnMouseDown(ev);
}
///
///
///
///
protected override void OnMouseMove(MouseEventArgs ev)
{
if (ev.Button == MouseButtons.Left && m_dragHandle >= 0)
{
if (m_dragRowShadow == null)
{
double _x2 = Math.Pow((ev.Location.X - m_mouseDownLocation.X), 2);
double _y2 = Math.Pow((ev.Location.Y - m_mouseDownLocation.Y), 2);
double _d2 = Math.Sqrt(_x2 + _y2);
if (_d2 > 3)
{
//执行拖拽;
this.BeginDrag(m_dragHandle);
//var _info = (DevExpress.XtraGrid.Views.Grid.ViewInfo.GridViewInfo)gv22.GetViewInfo();
//_info.GetGridRowInfo(0).CalcRectangle
}
}
else
{
m_dragRowShadow.Location = new Point(m_dragRowShadow.Location.X, this.PointToScreen(ev.Location).Y);
}
}
base.OnMouseMove(ev);
}
///
///
///
///
protected override void OnMouseUp(MouseEventArgs ev)
{
if (m_dragRowShadow != null)
{
var _hit = gridView.CalcHitInfo(ev.Location);
this.EndDrag(_hit.RowHandle);
}
base.OnMouseUp(ev);
}
private void BeginDrag(int _handle)
{
try
{
var _info = (DevExpress.XtraGrid.Views.Grid.ViewInfo.GridViewInfo)gridView.GetViewInfo();
//_info.GetGridRowInfo(0).CalcRectangle
Rectangle _bound = _info.GetGridRowInfo(_handle).Bounds;
_bound.Location = this.PointToScreen(_bound.Location);
m_dragRowShadow = new HL_FristAidPlatform_Public.DragForm(_bound);
m_dragRowShadow.Show();
}
catch
{
}
}
private void EndDrag(int _handle)
{
if (m_dragRowShadow != null)
{
m_dragRowShadow.Close();
m_dragRowShadow.Dispose();
m_dragRowShadow = null;
int _rowIndex = gridView.GetDataSourceRowIndex(m_dragHandle);
DataRow _row = ((DataTable)this.DataSource).Rows[_rowIndex];
object[] _values = _row.ItemArray;
base.BeginUpdate();
//移除目标行;
((DataTable)this.DataSource).Rows.RemoveAt(m_dragHandle);
_row = ((DataTable)this.DataSource).NewRow();
_row.ItemArray = _values;
if (_handle >= 0)
{
//插入指定位置;
((DataTable)this.DataSource).Rows.InsertAt(_row, _handle);
gridView.FocusedRowHandle = _handle;
}
else
{
//添加;
((DataTable)this.DataSource).Rows.Add(_row);
gridView.FocusedRowHandle = gridView.RowCount - 1;
}
base.EndUpdate();
}
}
}
///
///
///
public class DragForm : DevExpress.Utils.Win.TopFormBase
{
private Bitmap m_buff;
private Graphics m_buffG;
///
///
///
///
public DragForm(Rectangle _bound)
{
this.Text = "";
this.FormBorderStyle = FormBorderStyle.None;
this.ControlBox = false;
this.Size = _bound.Size;
this.ShowInTaskbar = false;
this.StartPosition = FormStartPosition.Manual;
this.Opacity = 0.8d; //TopFormBase已经有默认值了;
m_buff = new Bitmap(_bound.Width, _bound.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
m_buffG = Graphics.FromImage(m_buff);
m_buffG.CopyFromScreen(_bound.Location, new Point(0, 0), _bound.Size);
this.BackgroundImageLayout = ImageLayout.None;
this.BackgroundImage = m_buff;
this.Location = _bound.Location;
}
///
///
///
///
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// e.Graphics.DrawImage(m_buff, 0, 0);
}
}
}