C# 在DatagridviewCell内的图像上检测鼠标悬停
我创建了一个自定义DataGridView列,它通过从DataGridViewTextBoxCell类派生来承载图像和文本 我希望能够检测鼠标光标何时仅位于单元格的图像部分 我尝试为每个单元格创建一个矩形对象,该对象在Paint()方法中设置为图像的边界,并确定光标位置是否包含在OnMouseMove()处理程序中的边界矩形中,但这不起作用C# 在DatagridviewCell内的图像上检测鼠标悬停,c#,winforms,datagridview,C#,Winforms,Datagridview,我创建了一个自定义DataGridView列,它通过从DataGridViewTextBoxCell类派生来承载图像和文本 我希望能够检测鼠标光标何时仅位于单元格的图像部分 我尝试为每个单元格创建一个矩形对象,该对象在Paint()方法中设置为图像的边界,并确定光标位置是否包含在OnMouseMove()处理程序中的边界矩形中,但这不起作用 protected override void Paint(Graphics graphics, Rectangle clipBounds,
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";
}
}
这并不能回答您的问题,但由于它适用于
DataGridViewImageCell
,因此我决定将其作为参考
您的代码看起来非常相似。确保将imgBoundsRect
的位置保持在(0,0)
处,就像CellMouseMove中的鼠标坐标一样
private void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.ColumnIndex < 0 || e.RowIndex < 0) return;
DataGridViewImageCell iCell =
dataGridView1[e.ColumnIndex, e.RowIndex] as DataGridViewImageCell;
Text = "MISS";
if (iCell != null)
{
Image img = iCell.FormattedValue as Image;
Rectangle irect = new Rectangle(0, 0, img.Width, img.Height);
Point relLoc = e.Location;
if (irect.Contains(relLoc)) Text = "HIT";
}
}
private void dataGridView1\u CellMouseMove(对象发送方,DataGridViewCellMouseEventArgs e)
{
如果(e.ColumnIndex<0 | | e.RowIndex<0)返回;
DataGridViewImageCell iCell=
dataGridView1[e.ColumnIndex,e.RowIndex]作为DataGridViewImageCell;
Text=“小姐”;
如果(iCell!=null)
{
图像img=iCell.FormattedValue作为图像;
矩形方向=新矩形(0,0,img.宽度,img.高度);
点重新定位=e.位置;
if(direct.Contains(relLoc))Text=“HIT”;
}
}
这并不能回答您的问题,但由于它适用于DataGridViewImageCell
我决定将其作为参考
您的代码看起来非常相似。确保将imgBoundsRect
的位置保持在(0,0)
处,就像CellMouseMove中的鼠标坐标一样
private void dataGridView1_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.ColumnIndex < 0 || e.RowIndex < 0) return;
DataGridViewImageCell iCell =
dataGridView1[e.ColumnIndex, e.RowIndex] as DataGridViewImageCell;
Text = "MISS";
if (iCell != null)
{
Image img = iCell.FormattedValue as Image;
Rectangle irect = new Rectangle(0, 0, img.Width, img.Height);
Point relLoc = e.Location;
if (irect.Contains(relLoc)) Text = "HIT";
}
}
private void dataGridView1\u CellMouseMove(对象发送方,DataGridViewCellMouseEventArgs e)
{
如果(e.ColumnIndex<0 | | e.RowIndex<0)返回;
DataGridViewImageCell iCell=
dataGridView1[e.ColumnIndex,e.RowIndex]作为DataGridViewImageCell;
Text=“小姐”;
如果(iCell!=null)
{
图像img=iCell.FormattedValue作为图像;
矩形方向=新矩形(0,0,img.宽度,img.高度);
点重新定位=e.位置;
if(direct.Contains(relLoc))Text=“HIT”;
}
}
谢谢您的帮助。我试着将其合并到我正在使用的程序中,但由于我的单元格同时包含图像和文本,并且从DataGridViewTextBoxCell派生而来,我很难确定光标所在的单元格是文本还是图像部分。是的,我理解。但这就是你在做的,对吗?问题是:矩形imgBoundsRect
正确吗?我刚刚实现了一个解决方案,它使用了我和你的想法,现在它可以工作了。我使用了您的建议,将rect坐标设置为0,0,并将rect.contains代码移动到自定义类的OnMouseMove事件处理程序中。谢谢谢谢你的帮助。我试着将其合并到我正在使用的程序中,但由于我的单元格同时包含图像和文本,并且从DataGridViewTextBoxCell派生而来,我很难确定光标所在的单元格是文本还是图像部分。是的,我理解。但这就是你在做的,对吗?问题是:矩形imgBoundsRect
正确吗?我刚刚实现了一个解决方案,它使用了我和你的想法,现在它可以工作了。我使用了您的建议,将rect坐标设置为0,0,并将rect.contains代码移动到自定义类的OnMouseMove事件处理程序中。谢谢