You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
3.3 KiB
C#

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;
/// <summary>
/// 真为行,假为单元格
/// </summary>
public bool ByRow
{
get { return byRow; }
set
{
byRow = value;
UnRegisterEvent();
if (enable)
RegisterEvent();
}
}
public DevExpress.XtraGrid.Views.Grid.GridView View { get; private set; }
/// <summary>
/// 当前列
/// </summary>
GridColumn currentCol;
/// <summary>
/// 当前行
/// </summary>
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);
}
}
}
}