透明な背景を持つためにDataGridView
を継承するカスタムコントロールを作成しました。今、毎秒1行スクロールするタイマーにスクロール機能を設定しようとしています。ただし、(垂直方向に)スクロールしようとすると、背景画像は固定されません。スクロールするときに背景画像を固定する方法はありますか?DataGridViewで固定の背景を作成する方法
EDIT:スクロールタイマーを処理するためのコードです。
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
あなたが行ったことのコードを、問題の原因を見てみましょう。 –
「背景が固定されていない」という意味ですか? – Fabio
[*ダンスの](http://stackoverflow.com/a/33903052/3773066)の答えは、 "*データグリッドビューの背景を透明に設定する*"に答えてください。特に 'CreateParams'のオーバーライド。それはC#にあるので、それが問題であれば、[Telerikコードコンバータ](http://converter.telerik.com/)は通常、良い仕事を手伝ってくれます。 – OhBeWise