如何在 Devex 枢轴网格 winforms 中突出显示 mousemove 上的单元格?

How to highlight a cell on mousemove in Devex pivot grid winforms?

我正在使用 devex 枢轴网格并希望在鼠标移动时突出显示单元格(或更改背景颜色)。

我尝试捕获鼠标移动事件,但无法正常工作。这是我试过的代码:

void PivotGrid_MouseMove(object sender, MouseEventArgs e)
{
    var cell = PivotGrid.Cells.GetFocusedCellInfo();
    PivotGridHitInfo hitInfo = PivotGrid.CalcHitInfo(e.Location);
    if (hitInfo.HitTest == PivotGridHitTest.Cell)
    {
        if (hitInfo.CellInfo.DataField != null)
        {
            // hitInfo.CellInfo.

        }

    }
}

我试图找到这个没有 google 但运气不好。

任何人都可以指出一些相同的示例代码或帮助我完成我的示例代码吗?

我没有调试过,但可能是这样的:你记住你的命中点并使用自定义绘制。

PivotCellEventArgs ci;

void PivotGrid_MouseMove(object sender, MouseEventArgs e)
    {
        var cell = PivotGrid.Cells.GetFocusedCellInfo();
        PivotGridHitInfo hitInfo = PivotGrid.CalcHitInfo(e.Location);
        if (hitInfo.HitTest == PivotGridHitTest.Cell)
        {
            ci = hitInfo.CellInfo;
        } else {
           ci = hitInfo.CellInfo;
        }
    }


private void PivotGrid_CustomDrawCell(object sender, PivotCustomDrawCellEventArgs e) {
  if (ci == null) return;

  if (ci.ColumnIndex == e.ColumnIndex &&  ci.RowIndex == e.RowIndex) {
    e.GraphicsCache.FillRectangle(new LinearGradientBrush(e.Bounds, Color.LightBlue, 
    Color.Blue, LinearGradientMode.Vertical), e.Bounds);
  } else {
    e.GraphicsCache.FillRectangle(new SoldidBrush(Color.White), e.Bounds);
  }
  e.Handled = true;
}

您可以使用 PivotGridControl.CustomDrawCell event. In this event you can use PivotGridControl.CalcHitInfo method to get which cell is located at the mouse cursor point. In PivotGridControl.CustomDrawCell event you can use PivotCustomDrawCellEventArgs.Appearance 属性 更改单元格外观。
这是代码:

private void pivotGridControl1_CustomDrawCell(object sender, PivotCustomDrawCellEventArgs e)
{
    var info = pivotGridControl1.CalcHitInfo(pivotGridControl1.PointToClient(Cursor.Position));

    if (info.CellInfo != null && info.CellInfo.RowIndex == e.RowIndex && info.CellInfo.ColumnIndex == e.ColumnIndex)
    {
        e.Appearance.ForeColor = CommonColors.GetWarningColor(UserLookAndFeel.Default);
        e.Appearance.BackColor = CommonSkins.GetSkin(UserLookAndFeel.Default).Colors.GetColor(CommonColors.Info);
        e.Appearance.Font = new Font(e.Appearance.Font, FontStyle.Bold);
    }
}

此外,您需要在每次鼠标移动时强制重绘 PivotGridControl

private void pivotGridControl1_MouseMove(object sender, MouseEventArgs e)
{
    pivotGridControl1.Invalidate();
}