【发布时间】:2017-07-14 05:48:14
【问题描述】:
我创建了一个自定义 DataGridView 列,它通过派生自 DataGridViewTextBoxCell 类来承载图像和文本。
我希望能够检测鼠标光标何时仅位于单元格的图像部分上。
我尝试为每个在 Paint() 方法中设置为图像边界的单元格创建一个 Rectangle 对象,并确定光标位置是否包含在 OnMouseMove() 处理程序的边界矩形中,但这没有工作。
protected override void Paint(Graphics graphics, Rectangle clipBounds,
Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState,
object value, object formattedValue, string errorText,
DataGridViewCellStyle cellStyle,
DataGridViewAdvancedBorderStyle advancedBorderStyle,
DataGridViewPaintParts paintParts)
{
// Paint the base content
base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState,
value, formattedValue, errorText, cellStyle,
advancedBorderStyle, paintParts);
if (this.Image != null)
{
imgBoundsRect = new Rectangle(cellBounds.Location, this.Image.Size);
// Draw the image clipped to the cell.
System.Drawing.Drawing2D.GraphicsContainer container = graphics.BeginContainer();
graphics.SetClip(cellBounds);
graphics.DrawImageUnscaled(this.Image, cellBounds);
graphics.EndContainer(container);
}
}
protected override void OnMouseMove(DataGridViewCellMouseEventArgs e)
{
base.OnMouseMove(e);
Console.WriteLine(e.Location);
if (imgBoundsRect.Contains(e.Location))
{
this.ToolTipText = "View Doc";
}
}
【问题讨论】:
标签: c# winforms datagridview