c# - 在 datagridview 的工具提示中显示图像

标签 c# winforms datagridview tooltip

背景:我在 c# 中使用 winforms。我不想在 datagridview 单元格中显示图像,我只将路径存储在数据库中,并在数据库中的 datagridview 中显示它们。

问题:当用户输入一个单元格时,会弹出一个工具提示。我需要的是,当当前单元格的列索引为 2 时,工具提示应显示来自当前单元格中给定路径的图像。

我找到了 This Article非常好。但是无法成功。我有以下代码

    void CustomizedToolTip_Popup(object sender, PopupEventArgs e)
    {
        DataGridView parent = e.AssociatedControl as DataGridView;
        if (parent.CurrentCell != null)
        {
            if (parent.CurrentCell.ColumnIndex == 2)
            {
                Bitmap bmpIn = new Bitmap(parent.CurrentCell.Value + "");
                using (Graphics g = Graphics.FromImage(bmpIn))
                {
                    Rectangle mr = new Rectangle(5, 5, 50, 50);
                    mr.Location = new Point(5, 5);
                    g.PageUnit = GraphicsUnit.Pixel;
                    g.DrawImage(bmpIn, mr);
                }
            }
        }
    }

我以为这段代码应该画图,但它不是画图,不仅画图我不确定位置,即使我会画,如何在tootip中定位它。我无法从我提到的文章中理解它。下面是我的数据 GridView 的图像。

enter image description here

最佳答案

我为一个项目做了类似的事情。

相反,我只是使用了一个表单,我将其设置为在 CellMouseOver 上打开,在 CellMouseLeave 上关闭

    frm_MouseOverPicture HoverZoom = new frm_MouseOverPicture();

    private void dgv_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
    {
       DataGridView dgv_sender = sender as DataGridView;
       DataGridViewCell dgv_MouseOverCell = dgv_sender.Rows[e.RowIndex].Cells[e.ColumnIndex];

       //Get FilePath from dgv_MouseOverCell content

       //Get x, y based on position relative to edge of screen
       //x, y = top left point of HoverZoom form

       HoverZoom.LoadPicture(FilePath);
       HoverZoom.Location = new System.Drawing.Point(x, y);
       HoverZoom.Show();

    }

    private void dgv_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
    {
       HoverZoom.Hide();
       HoverZoom.ClearPicture();
    }

希望这与您正在寻找的内容足够接近。我只是制作了没有边框的表格,并在整个表格上放了一个图片框。

关于c# - 在 datagridview 的工具提示中显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14840979/

相关文章:

c# - 添加第二个 DatagridViewRow 导致克隆

c# - 如何使用 FileHelper 库为迭代字段创建客户类

c# - 具有依赖注入(inject)的 UserControl 无法创建组件

c# - 如何使用 C# 为 Crystal 报表动态设置数据库名称?

c# 将 DataGridView 保存到 Xml 文件

c# - 如何获取 DataGridView 行值并将其存储在变量中?

c# - 从 CreateFile 产生的句柄构造一个 FileStream 给出一个空流

c# - Uri.IsWellFormedUriString() 对于正确的 URL 返回 false

c# - 如何在 C# 中从 native dll 捕获 c0000005 异常

c# - 在 webbrowser 控件 c# winform 中静音刷新声音