在 DataGridViewCell 上绘制组合框下拉箭头



我想要的是,当鼠标移动到DataGridViewCell上时,我想识别鼠标下方的单元格,并在其上绘制一个ComboBox DownArrow。 当鼠标移离单元格时,我只想绘制"正常"单元格。

我想我需要在鼠标下方绘制单元格,重新绘制它之前所在的单元格,以清除任何以前的自定义绘制箭头。

我执行此操作的方式如下注意:所有这些代码都在 DataGridView 级别,而不是单元格级别。

private DataGridViewCell LastCell;
private DataGridViewCell MouseCell;
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
DataGridViewCell currentCell = GetCellUnderCursor();
if (currentCell != MouseCell)   //Has moved to a new cell
{
LastCell = MouseCell;
MouseCell = currentCell;
if (currentCell != null) this.InvalidateCell(currentCell);
if (LastCell != null) this.InvalidateCell(LastCell);
}
else
{
//Has not changed cell - don't paint again - exit to prevent flicker
return;
}
}

我已经对此进行了测试,并且用鼠标在其下方绘制单元格并清除其他单元格效果很好。

"测试"是使用此代码完成的,只是在单元格周围绘制一个矩形。

protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
{
//call base method
base.OnCellPainting(e);
e.PaintContent(e.ClipBounds);
if (e.RowIndex == -1 || e.ColumnIndex == -1) return;
//Is the mouse over this cell?
DataGridViewCell cell = GetCellUnderCursor();
if (cell == null) return; //row or column is -1
DataGridViewCell paintingCell = this.Rows[e.RowIndex].Cells[e.ColumnIndex];
if (paintingCell != cell) return;

//Paint the cell, excluding the border.
e.Paint(e.CellBounds, DataGridViewPaintParts.All & ~DataGridViewPaintParts.Border);
//Now paint a custom border.
using (Pen p = new Pen(Color.RoyalBlue, 1))
{
Rectangle rect = e.CellBounds;
rect.Width -= 2;
rect.Height -= 2;
e.Graphics.DrawRectangle(p, rect);
}
e.PaintContent(e.ClipBounds);
e.Handled = true;
}

如前所述,这一切都运行良好 - 我在 DataGridView 周围跟随鼠标得到一个漂亮的蓝色矩形。

然后,我尝试开发类似的代码来绘制ComboBox Dropdown箭头,并尝试使用ComboBoxRenderer类执行此操作:

Size arrowSize = new Size(18,20);
Rectangle arrowRectangle = new Rectangle(e.ClipBounds.X + e.ClipBounds.Width - arrowSize.Width -1, e.ClipBounds.Y+1,arrowSize.Width, arrowSize.Height);
Rectangle topTextBoxRectangle = new Rectangle(e.ClipBounds.X, e.ClipBounds.Y, e.ClipBounds.Width, arrowSize.Height+2);
ComboBoxState arrowState = ComboBoxState.Normal;
if (!ComboBoxRenderer.IsSupported)
{
Debug.WriteLine("Renderer not supported");
return;
}
else
{
string cellText = cell.Value == null ? "" : cell.Value.ToString();
ComboBoxRenderer.DrawDropDownButton(e.Graphics, arrowRectangle, arrowState);
//ComboBoxRenderer.DrawTextBox(e.Graphics, topTextBoxRectangle, cellText, this.Font, ComboBoxState.Normal);
e.PaintContent(e.ClipBounds);
}
e.Handled = true;

这真的根本不起作用 - 绘制单元格有时会绘制下拉列表(似乎将其绘制在错误的单元格上 - 上面的单元格? 如果将鼠标向下移动 DataGridView,则会绘制上面的单元格。 如果你向上移动它,它会绘制正确的单元格(真的!(,向下移动不会清除任何旧绘图,但向上移动可以。 同样,从左向右移动鼠标会提供正确的行为,但不会从右向左移动。

我发现e.PaintContents(e.ClipBounds)似乎比ComboBoxRenderer.DrawTextBox()好得多

请注意,此代码用于上述代码的"绘制单元格"部分。

有什么建议可以解决这个问题,或者可能出现什么问题?

好的 - 问题解决了!

我将绘图设置为:

Rectangle arrowRectangle = new Rectangle(e.ClipBounds.X + e.ClipBounds.Width - arrowSize.Width -1, 
e.ClipBounds.Y+1,arrowSize.Width, arrowSize.Height);

当它应该使用 CellBounds 而不是 ClipBounds 时:

Rectangle arrowRectangle = new Rectangle(e.CellBounds.X + e.CellBounds.Width - arrowSize.Width - 1, 
e.CellBounds.Y + 1, arrowSize.Width, arrowSize.Height);

最新更新