我在這里找到了一個關于如何為DataGridViewButtonColumn. 我將FlatStyle列的設定為Flat,現在我想擺脫按鈕(洗掉按鈕)周圍的邊界,但似乎沒有我將其設定為 0的BorderSize屬性DataGridViewButtonColumn。

要繪制按鈕影像,我使用
If dgvPaymentList.Columns(e.ColumnIndex).Name = "xDelete" AndAlso e.RowIndex >= 0 Then
e.Paint(e.CellBounds, DataGridViewPaintParts.All)
e.Graphics.DrawImage(My.Resources.delete16White, CInt((e.CellBounds.Width / 2) - (My.Resources.delete16White.Width / 2)) e.CellBounds.X, CInt((e.CellBounds.Height / 2) - (My.Resources.delete16White.Height / 2)) e.CellBounds.Y)
e.Handled = True
End If
有什么幫助嗎?
uj5u.com熱心網友回復:
如果沒有完整的代碼,很難為您提供幫助...嘗試洗掉以下行:
e.Paint(e.CellBounds, DataGridViewPaintParts.All)
事實上,如果列是用類似這樣的代碼獲得的
Dim btn As New DataGridViewButtonColumn() With {
.HeaderText = "Example",
.Text = "Click Me",
.UseColumnTextForButtonValue = True,
.FlatStyle = FlatStyle.Flat 'Or others
}
DataGridView1.Columns.Add(btn)
該e.Paint線將繪制按鈕(邊框 填充色 文本)。但是,如果沒有它,按鈕設計將不會被呈現。
您可能需要在影像后面繪制一個矩形作為背景顏色。
uj5u.com熱心網友回復:
一個DataGridViewButtonCell與FlatStyle屬性設定為FlatStyle.Flat使用小區的ForeColor并SelectionForeColor繪制邊框部分。可以在原始碼中找到,如何選擇邊框顏色并傳遞給繪制邊框方法。因此,擺脫了邊框的最簡單的解決方法是指定單元格的BackColor到ForeColor和SelectionForeColor性能。
使用設計器應用它或通過代碼:
Public Class YourForm
Private ReadOnly img As Bitmap
Sub New()
InitializeComponent()
img = My.Resources.delete16White
Dim c = Color.FromArgb(64, 64, 64)
' Your button column...
xDelete.DefaultCellStyle.BackColor = c
xDelete.DefaultCellStyle.SelectionBackColor = c
xDelete.DefaultCellStyle.ForeColor = c
xDelete.DefaultCellStyle.SelectionForeColor = c
End Sub
Protected Overrides Sub OnClosed(e As EventArgs)
MyBase.OnClosed(e)
img.Dispose()
End Sub
Private Sub dgvPaymentList_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles dgvPaymentList.CellPainting
If e.RowIndex >= 0 AndAlso e.ColumnIndex = dgvcButton.Index Then
Dim r = e.CellBounds
Dim imgRec = New Rectangle(
r.X (r.Width - img.Width) \ 2,
r.Y (r.Height - img.Height) \ 2,
img.Width, img.Height)
e.Paint(e.ClipBounds, DataGridViewPaintParts.All)
e.Graphics.DrawImage(img, imgRec)
e.Handled = True
End If
End Sub
End Class
或者,接管并按照您喜歡的方式自己繪制整個事物:
Private Sub dgvPaymentList_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles dgvPaymentList.CellPainting
If e.RowIndex >= 0 AndAlso e.ColumnIndex = xDelete.Index Then
Dim r = e.CellBounds
Dim imgRec = New Rectangle(
r.X (r.Width - img.Width) \ 2,
r.Y (r.Height - img.Height) \ 2,
img.Width, img.Height)
e.PaintBackground(e.ClipBounds, False)
If e.CellBounds.Contains(dgvPaymentList.PointToClient(MousePosition)) Then
Dim backColor As Color
If MouseButtons = MouseButtons.Left Then
backColor = Color.FromArgb(104, 104, 104)
Else
backColor = Color.FromArgb(84, 84, 84)
End If
Using br As New SolidBrush(backColor)
e.Graphics.FillRectangle(br, r)
End Using
End If
e.Graphics.DrawImage(img, imgRec)
e.Handled = True
End If
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/390814.html
標籤:网络
上一篇:從兩個表中讀取資訊
