【问题标题】:Show image in tooltip of datagridview在 datagridview 的工具提示中显示图像
【发布时间】:2018-01-22 23:30:59
【问题描述】:

背景:我正在使用 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中定位。我无法从我提到的文章中理解它。下面是我的datagridview的图像。

【问题讨论】:

    标签: c# winforms datagridview tooltip


    【解决方案1】:

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

    相反,我只是使用了我设置为在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();
        }
    

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

    【讨论】:

    • 谢谢...这是我最后的选择,因为至少它有效。我可能不得不去。
    • 接受了这个答案,因为我正在使用这个解决方案,但这并不是我所要求的。
    【解决方案2】:

    只需在表单中添加一个图片框并将大小模式设置为“StretchImage”和 visible=false,然后将以下事件添加到您的 datagridview

           private void metroGrid1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
        {
            DataGridView dgv_sender = sender as DataGridView;
            DataGridViewCell dgv_MouseOverCell=null;
            if (e.RowIndex > 0 && e.ColumnIndex > 0 && e.RowIndex <dgv_sender.RowCount && e.ColumnIndex<dgv_sender.ColumnCount)
            {
              dgv_MouseOverCell = dgv_sender.Rows[e.RowIndex].Cells[e.ColumnIndex];
            }
            if(dgv_MouseOverCell !=null)
            if (e.ColumnIndex == 4) {
                if (dgv_MouseOverCell.Value != null)
                {
                    if (File.Exists(dgv_MouseOverCell.Value.ToString()))
                    {
                        Image img = Image.FromFile(dgv_MouseOverCell.Value.ToString());
                        pictureBox1.ImageLocation = dgv_MouseOverCell.Value.ToString();
                        pictureBox1.Location = new System.Drawing.Point(Cursor.Position.X - this.Location.X, Cursor.Position.Y - this.Location.Y);
                        pictureBox1.Visible = true;
                    }
                }
            }
        }
    
        private void metroGrid1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
        {
            pictureBox1.Visible = false;
        }
    

    Click here to view image

    【讨论】:

    • 我建议您添加一些上下文,以便您的答案不仅仅是一些代码。尝试解释它为什么有效或效果更好,以及 OP 需要注意什么。
    【解决方案3】:
        void CustomizedToolTip_Popup(object sender, PopupEventArgs e)
        {
            DataGridView parent = e.AssociatedControl as DataGridView;
            if (parent.CurrentCell != null)
            {
                if (parent.CurrentCell.ColumnIndex == 2)
                {
                    string path = parent.CurrentCell.Value.ToString();
                    using (System.Drawing.Imaging.Metafile emf = new System.Drawing.Imaging.Metafile(path))
                    using (System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(emf.Width, emf.Height))
                    {
                        bmp.SetResolution(emf.HorizontalResolution, emf.VerticalResolution);
    
                        using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bmp))
                        {
                            g.DrawImage(emf,
                                new Rectangle(0, 0, emf.Width, emf.Height),
                                new Rectangle(0, 0, emf.Width, emf.Height),
                                GraphicsUnit.Pixel
                            );
    
                            return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
                        }
                    }
                }
            }
        }
    

    Source

    使用System.Windows.Interop

    "您必须下载.NET 3.0 runtimelater并安装它才能获得 大会……”

    ".NET 3.0 安装后,应该会出现在 Add References 中 组件名称为“WindowsBase”的列表。如果没有,你可以 始终从“添加引用”对话框的“浏览”选项卡中添加它。 (我的 C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0 框)”

    Source

    【讨论】:

    • 谢谢。它看起来更好,但请我无法让它工作,虽然我已经尝试使用 windowbase.dll 进行互操作但仍然有错误,如果我评论最后一行,那么工具提示中不会出现任何图像
    • 我正在使用 Visual Studio 2010。Dot Net Framework 4.0。您给定的方向是否正确?如果是,请更新链接,因为此链接显示We are sorry, the page you requested cannot be found.
    • @Sami,你必须download .net 3.5 SP1 并寻找 dll。 here.net 3.5 sp1 的命名空间列表
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-21
    相关资源
    最近更新 更多