简体   繁体   English

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

[英]Show image in tooltip of datagridview

Background: I am working with winforms in c#. 背景:我正在使用c#中的winforms。 I do not want images to be shown in datagridview cells, i have stored only path in database and showing them in datagridview from database. 我不希望图像显示在datagridview单元格中,我只在数据库中存储了路径,并在数据库的datagridview中显示它们。

Problem: When user enters a cell a tooltip is poped-up. 问题:当用户进入单元格时,工具提示会加速。 What I need is that when column index of current-cell is 2 then the tool tip should show an image from the path given in the current cell. 我需要的是当当前单元格的列索引是2时,工具提示应该显示当前单元格中给出的路径的图像。

I have found This Article very good. 我发现这篇文章非常好。 But unable to get succeeded. 但无法成功。 I have following code 我有以下代码

    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);
                }
            }
        }
    }

I thought this code should draw image, but it is not drawing, and more than drawing I am uncertain about the location, even If I could draw, how to locate it in tootip. 我认为这段代码应该绘制图像,但它不是绘图,而不是绘图我不确定位置,即使我可以绘制,如何在tootip中找到它。 I have not been able to understand it from the article i mentioned. 我从上面提到的文章中无法理解。 Below is the image of my datagridview. 下面是我的datagridview的图像。

在此输入图像描述

I did something similar for a project. 我为一个项目做了类似的事情。

Instead, I just used a form which I set to open on CellMouseOver , close on CellMouseLeave 相反,我只使用了一个我在CellMouseOver上打开的CellMouseOver ,在CellMouseLeaveCellMouseLeave

    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();
    }

Hope that is close enough to what you are looking for. 希望足够接近您所寻找的东西。 I just made the form with no border and put a picture box over the whole thing. 我只是制作了没有边框的表格,并在整个事情上放了一个图片框。

    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 资源

To use the System.Windows.Interop 要使用System.Windows.Interop

"You have to download the .NET 3.0 runtime or later and install it to get the assembly..." “你必须下载.NET 3.0运行时更高版本并安装它以获得程序集......”

"After .NET 3.0 is installed, it should appear in the Add References list with component name as "WindowsBase". If it doesn't you can always add it from the Browse tab in the Add References dialog. (C:\\Program Files\\Reference Assemblies\\Microsoft\\Framework\\v3.0 on my box)" “安装.NET 3.0后,它应该出现在Add References列表中,组件名称为”WindowsBase“。如果不是,您可以始终从Add References对话框的Browse选项卡中添加它。(C:\\ Program Files \\参考Assemblies \\ Microsoft \\ Framework \\ v3.0在我的盒子上)“

Source 资源

Just add a picturebox to your form and set size mode to "StretchImage" and visible= false, then add the following events to your datagridview 只需将一个图片框添加到您的表单并将大小模式设置为“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 点击此处查看图片

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM