如何在datagridview vb.net中获取矩形的颜色

时间:2015-08-10 09:15:18

标签: vb.net visual-studio-2012 datagridview

我有一个像enter image description here

这样的数据网格视图

然后我想获得单击单击矩形的颜色。 如何获得那种颜色?我一直在找,但没找到。 请帮我。感谢

我希望在用户点击“确定”时获取datagridview中的颜色,因为要提供颜色贴图。我做了gis应用程序。

此代码用于制作数据gridview,

Dim dtCloned As New DataTable             Dim imagecolumn As DataColumn = New DataColumn             imagecolumn.DataType = GetType(位图)             imagecolumn.ColumnName =“符号”             dtCloned.Columns.Add(imagecolumn)             dtCloned.Columns.Add( “值”)             dtCloned.Columns.Add( “传奇”)

        For i As Integer = 0 To sf.Categories.Count - 1
            Dim r As DataRow = dtCloned.NewRow
            Dim bmp As New Bitmap(50, 15)
            Dim g As Graphics = Graphics.FromImage(bmp)
            Dim category As MapWinGIS.ShapefileCategory = sf.Categories.Item(i)
            Dim Brush As New SolidBrush(ColorTranslator.FromOle(scheme.GraduatedColor((i) / sf.Categories.Count)))
            g.FillRectangle(Brush, 0, 0, bmp.Width - 1, bmp.Height - 1)
            g.DrawRectangle(Pens.Black, 0, 0, bmp.Width - 1, bmp.Height - 1)
            r(0) = bmp
            r(1) = category.Name.Substring(category.Name.IndexOf("=") + 1)
            r(2) = category.Name.Substring(category.Name.IndexOf("=") + 1)
            dtCloned.Rows.Add(r)
        Next
        DataGVSymUnique.DataSource = dtCloned
        For Each c As DataGridViewColumn In DataGVSymUnique.Columns
            c.Width = 145
            c.ReadOnly = True
        Next
        DataGVSymUnique.Columns("Legend").ReadOnly = False

2 个答案:

答案 0 :(得分:0)

为了从该单元格中获取对象的颜色,我们可以使用CellContentClick的{​​{1}}事件。原因是要确保我们在该单元格中有某些内容,就像您只是单击它不会触发的单元格一样我们不需要单元格的背面颜色/默认颜色。我写了一个快速的小例子来做到这一点。它是经过试验和测试

DataGridView

正如您所看到的,我刚刚使用了Imports System.Runtime.InteropServices Public Class Form1 <DllImport("user32.dll")> Private Shared Function GetDC(hwnd As IntPtr) As IntPtr End Function <DllImport("user32.dll")> Private Shared Function ReleaseDC(hwnd As IntPtr, hdc As IntPtr) As Int32 End Function <DllImport("gdi32.dll")> Private Shared Function GetPixel(hdc As IntPtr, nXPos As Integer, nYPos As Integer) As UInteger End Function 'We can use this event as it will not fire until we actually click the object in the cell. Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick Try Dim _type As Type = DataGridView1.CurrentCell.GetType If _type IsNot Nothing Then If _type.Name = "DataGridViewImageCell" Then Dim CurrentColor As Color = GetColor(Cursor.Position.X, Cursor.Position.Y) Dim c As System.Drawing.Color = Nothing For Each ob As System.Drawing.KnownColor In [Enum].GetValues(GetType(System.Drawing.KnownColor)) c = Color.FromKnownColor(ob) 'Do we have something? If CurrentColor.ToArgb = c.ToArgb Then 'Do what you want with it... MsgBox(c.Name) Exit For End If Next End If End If Catch ex As Exception 'Do something with your exception End Try End Sub Public Shared Function GetColor(ByVal LocX As Integer, LocY As Integer) As System.Drawing.Color Dim _rColor As System.Drawing.Color = Color.White Dim hdc As IntPtr = GetDC(IntPtr.Zero) Dim pixel As UInteger = GetPixel(hdc, LocX, LocY) Try ReleaseDC(IntPtr.Zero, hdc) _rColor = Color.FromArgb(CInt(pixel And &HFF), CInt(pixel And &HFF00) >> 8, CInt(pixel And &HFF0000) >> 16) Catch ex As Exception ReleaseDC(IntPtr.Zero, hdc) Return _rColor End Try Return _rColor End Function End Class (我知道它已经过时了,应该使用MsgBox),但它只是用于显示颜色名称。我不确定你想用这种颜色做什么,但你有颜色和其他一切。

答案 1 :(得分:0)

有一个隐藏列,其中包含用于绘制位图的颜色值,当您想要查找值时,询问隐藏列。

For i As Integer = 0 To sf.Categories.Count - 1
    Dim r As DataRow = dtCloned.NewRow
    Dim bmp As New Bitmap(50, 15)
    Dim g As Graphics = Graphics.FromImage(bmp)
    Dim category As MapWinGIS.ShapefileCategory = sf.Categories.Item(i)
    ' save the calculated color in a variable
    Dim colorVal = scheme.GraduatedColor((i) / sf.Categories.Count)
    Dim Brush As New SolidBrush(ColorTranslator.FromOle(colorVal))
    g.FillRectangle(Brush, 0, 0, bmp.Width - 1, bmp.Height - 1)
    g.DrawRectangle(Pens.Black, 0, 0, bmp.Width - 1, bmp.Height - 1)
    r(0) = bmp
    r(1) = category.Name.Substring(category.Name.IndexOf("=") + 1)
    r(2) = category.Name.Substring(category.Name.IndexOf("=") + 1)
    r(3) = colorVal ' save the value in a hidden cell
    dtCloned.Rows.Add(r)
Next

DataGVSymUnique.DataSource = dtCloned
    For Each c As DataGridViewColumn In DataGVSymUnique.Columns
        c.Width = 145
        c.ReadOnly = True
    Next
DataGVSymUnique.Columns("Legend").ReadOnly = False