在datagridview的工具提示中显示图像

时间:2013-02-12 20:14:01

标签: 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中找到它。我从上面提到的文章中无法理解。下面是我的datagridview图片。

enter image description here

3 个答案:

答案 0 :(得分:4)

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

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

希望足够接近您所寻找的东西。我只是制作了没有边框的表格,并在整个事情上放了一个图片框。

答案 1 :(得分:1)

    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后,它应出现在”添加引用“中   将组件名称列为“WindowsBase”。如果没有,你可以   始终从“添加引用”对话框的“浏览”选项卡中添加它。   (我的C:\ Program Files \ Reference Assemblies \ Microsoft \ Framework \ v3.0   盒)“

Source

答案 2 :(得分:1)

只需将一个图片框添加到表单并将大小模式设置为“StretchImage”并显示为visible,然后将以下事件添加到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