using DevExpress.XtraGrid.Columns;
using System.Drawing;
using System.Windows.Forms;
namespace ZJ_BYD.Untils
{
public class ViewStyleHelper
{
bool enable;
public bool Enable
{
get { return enable; }
set
{
enable = value;
UnRegisterEvent();
if (enable)
{
RegisterEvent();
}
else
{
View.RefreshData();
}
}
}
bool byRow;
///
/// 真为行,假为单元格
///
public bool ByRow
{
get { return byRow; }
set
{
byRow = value;
UnRegisterEvent();
if (enable)
RegisterEvent();
}
}
public DevExpress.XtraGrid.Views.Grid.GridView View { get; private set; }
///
/// 当前列
///
GridColumn currentCol;
///
/// 当前行
///
int currentRowHandle;
public ViewStyleHelper(DevExpress.XtraGrid.Views.Grid.GridView view, bool byRow = true)
{
View = view;
this.byRow = byRow;
Enable = true;
view.MouseLeave += (s, e) =>
{
currentCol = null;
currentRowHandle = int.MinValue;
view.RefreshData();
};
}
void RegisterEvent()
{
View.MouseMove += OnMouseMove;
if (!byRow)
View.RowCellStyle += OnRowCellStyle;
else
{
View.RowStyle += OnRowStyle;
}
}
void UnRegisterEvent()
{
View.MouseMove -= OnMouseMove;
View.RowCellStyle -= OnRowCellStyle;
View.RowStyle -= OnRowStyle;
}
private void OnMouseMove(object sender, MouseEventArgs e)
{
var view = sender as DevExpress.XtraGrid.Views.Grid.GridView;
var info = view.CalcHitInfo(e.Location);
bool refresh = false;
if (currentCol != info.Column || currentRowHandle != info.RowHandle)
{
refresh = true;
}
if (info.InDataRow)
{
currentCol = info.Column;
currentRowHandle = info.RowHandle;
}
else
{
currentCol = null;
currentRowHandle = int.MinValue;
}
if (refresh)
view.RefreshData();
}
private void OnRowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
{
if (e.RowHandle == currentRowHandle)
{
e.Appearance.BackColor = Color.FromArgb(108, 178, 235);
e.HighPriority = true;
}
}
private void OnRowCellStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowCellStyleEventArgs e)
{
if (e.Column == currentCol && e.RowHandle == currentRowHandle)
{
e.Appearance.BackColor = Color.FromArgb(108, 178, 235);
}
}
}
}