【问题标题】:How to make a fixed background on a DataGridView如何在 DataGridView 上制作固定背景
【发布时间】:2017-05-07 10:35:57
【问题描述】:

我制作了一个继承DataGridView 的自定义控件,以便拥有透明背景。现在我正在尝试在每秒向下滚动一行的计时器上设置滚动功能。但是,当我尝试(垂直)滚动时,背景图像不固定。滚动时有什么方法可以固定背景图像?

编辑:这是处理滚动计时器的代码。

Private Sub Sub1

    'Some previous code

    If DataGridView1.Rows.Count > 10 Then
        ScrollIndex1 = 0 'Integer for scroll index
        DGVAutoScroll()
    End If

End Sub

Private Sub DGVAutoScroll()

    Timer2.Enabled = True
    Timer2.Interval = 1000

End Sub

Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick

    If ScrollIndex1 = DataGridView1.Rows.Count - 1 Then
        ScrollIndex1 = 0
        DataGridView1.FirstDisplayedScrollingRowIndex = ScrollIndex1
        ScrollIndex1 += 1
    Else
        DataGridView1.FirstDisplayedScrollingRowIndex = ScrollIndex1
        ScrollIndex1 += 1
    End If

End Sub

'Custom DataGridView class
Imports System.ComponentModel
Imports System.Windows.Forms

Public Class MyDGV
    Inherits DataGridView

    Public Property DGVHasTransparentBackground As Boolean
        Get
            Return Nothing
        End Get
        Set()
            SetTransparentProperties(True)
        End Set
    End Property

    Public Property ScrollBar
        Get
            Return Nothing
        End Get
        Set(value)
            BackgroundColor = Color.Transparent
        End Set

    End Property

    Public Sub New()
        DGVHasTransparentBackground = True
    End Sub

    Private Sub SetTransparentProperties(ByRef SetAsTransparent As Boolean)
        MyBase.DoubleBuffered = True
        MyBase.EnableHeadersVisualStyles = False
        MyBase.ColumnHeadersDefaultCellStyle.BackColor = Color.Transparent
        MyBase.RowHeadersDefaultCellStyle.BackColor = Color.Transparent
        SetCellStyle(Color.Transparent)
    End Sub


    Protected Overrides Sub PaintBackground(graphics As System.Drawing.Graphics, clipBounds As System.Drawing.Rectangle, gridBounds As System.Drawing.Rectangle)
        MyBase.PaintBackground(graphics, clipBounds, gridBounds)

        Dim rectSource As New Rectangle(MyBase.Location, MyBase.Size)
        Dim rectDest As New Rectangle(0, 0, rectSource.Width, rectSource.Height)

        Dim b As New Bitmap(Parent.ClientRectangle.Width, Parent.ClientRectangle.Height)
        Graphics.FromImage(b).DrawImage(MyBase.Parent.BackgroundImage, Parent.ClientRectangle)
        graphics.DrawImage(b, rectDest, rectSource, GraphicsUnit.Pixel)

    End Sub

    Protected Overrides Sub OnColumnAdded(e As System.Windows.Forms.DataGridViewColumnEventArgs)
        MyBase.OnColumnAdded(e)

        SetCellStyle(Color.Transparent)
    End Sub

    Private Sub SetCellStyle(ByVal cellColour As Color)
        For Each col As DataGridViewColumn In MyBase.Columns
            col.DefaultCellStyle.BackColor = cellColour
            col.DefaultCellStyle.SelectionBackColor = cellColour
        Next
    End Sub
End Class

【问题讨论】:

  • 请提供您所做的代码,让我们看看是什么导致了问题。
  • background is not fixed 是什么意思?
  • 检查letsdance's 此处对问题“将数据网格视图背景设置为透明”的回答。特别是CreateParams 的覆盖。它在 C# 中,所以如果这是一个问题,Telerik Code Converter 通常会提供很好的帮助。

标签: vb.net datagridview


【解决方案1】:

看来我必须在计时器滴答声中调用 DataGridView1.SelectAll()。谢谢大家。

【讨论】:

    【解决方案2】:

    我假设您想要数据背后的图像 (datagridview)。

    如果 datagridview 是透明的,那么只需在背景中添加图像。将其粘贴在表单或 datagridview 后面的项目上。它不会滚动,因为它会在 datagridview

    之外

    如果数据不清晰可见,请更改图像 alpha。

    【讨论】:

      猜你喜欢
      • 2011-05-13
      • 2016-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-01
      • 2022-10-15
      • 2017-05-02
      • 2018-08-19
      相关资源
      最近更新 更多