vb.net - 如何在 DataGridView 上制作固定背景

标签 vb.net datagridview

我制作了一个继承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

最佳答案

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

关于vb.net - 如何在 DataGridView 上制作固定背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41272343/

相关文章:

c# - 代码签名证书

C#使用按钮从DataGridView中的列表中删除元素仅工作一次

c# - 用大量数据填充 DataGridView 的最佳方法

vb.net - VB检测空闲时间

asp.net - 为什么按钮单击插入数据两次

.net - Windows 窗体处理程序在计算机唤醒时被忽略

C# DataGridView,大单元格 : Content never fully visible, 滚动跳过单元格

mvvm - 绑定(bind)到 UWP 数据网格的动态列数(Windows 社区工具包)

vb.net - 如何从datagridview的选定行中获取数据

.net - 适用于 VB6 程序员的 Option Strict On 和 .NET