12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- Imports System.ComponentModel
- Module DGV進度條
- Public Class DataGridViewProgressColumn
- Inherits DataGridViewImageColumn
- Public Sub New()
- Me.CellTemplate = New DataGridViewProgressCell
- End Sub
- End Class
- Public Class DataGridViewProgressCell
- Inherits DataGridViewImageCell
- Sub New()
- ValueType = Type.GetType("Integer")
- End Sub
- ' 使進度單元與默認圖像單元一致所需的方法。
- ' 默認圖像單元將圖像作為值,儘管進度單元的值是整數。
- Protected Overrides Function GetFormattedValue(
- ByVal value As Object,
- ByVal rowIndex As Integer,
- ByRef cellStyle As DataGridViewCellStyle,
- ByVal valueTypeConverter As TypeConverter,
- ByVal formattedValueTypeConverter As TypeConverter,
- ByVal context As DataGridViewDataErrorContexts) As Object
- Static emptyImage As Bitmap = New Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
- GetFormattedValue = emptyImage
- End Function
- Protected Overrides Sub Paint(ByVal g As System.Drawing.Graphics, ByVal clipBounds As System.Drawing.Rectangle,
- ByVal cellBounds As System.Drawing.Rectangle,
- ByVal rowIndex As Integer, ByVal cellState As System.Windows.Forms.DataGridViewElementStates,
- ByVal value As Object, ByVal formattedValue As Object, ByVal errorText As String,
- ByVal cellStyle As System.Windows.Forms.DataGridViewCellStyle,
- ByVal advancedBorderStyle As System.Windows.Forms.DataGridViewAdvancedBorderStyle,
- ByVal paintParts As System.Windows.Forms.DataGridViewPaintParts)
- Dim progressVal As Double = CType(value, Double)
- Dim percentage As Single = CType(progressVal / 分母, Single)
- Dim backBrush As Brush = New SolidBrush(cellStyle.BackColor)
- Dim foreBrush As Brush = New SolidBrush(cellStyle.ForeColor)
- ' 調用基類方法來繪製默認單元格外觀。
- MyBase.Paint(g, clipBounds, cellBounds, rowIndex, cellState,
- value, formattedValue, errorText, cellStyle,
- advancedBorderStyle, paintParts)
- If percentage > 0.0 And percentage < 分段1 Then
- ' 繪製進度條和文字
- g.FillRectangle(New SolidBrush(Color.LightBlue), cellBounds.X + 2, cellBounds.Y + 2, Convert.ToInt32((percentage * cellBounds.Width - 8)), cellBounds.Height - 8)
- g.DrawString(Strings.Format(progressVal, "#,##0.00") & 進度條後墜, cellStyle.Font, foreBrush, cellBounds.X + 6, cellBounds.Y + 2)
- ElseIf percentage > 分段2 And percentage < 分段3 Then
- ' 繪製進度條和文字
- g.FillRectangle(New SolidBrush(Color.LightGreen), cellBounds.X + 2, cellBounds.Y + 2, Convert.ToInt32((percentage * cellBounds.Width - 8)), cellBounds.Height - 8)
- g.DrawString(Strings.Format(progressVal, "#,##0.00") & 進度條後墜, cellStyle.Font, foreBrush, cellBounds.X + 6, cellBounds.Y + 2)
- ElseIf percentage > 分段4 Then
- ' 繪製進度條和文字
- g.FillRectangle(New SolidBrush(Color.LightPink), cellBounds.X + 2, cellBounds.Y + 2, Convert.ToInt32((percentage * cellBounds.Width - 8)), cellBounds.Height - 8)
- g.DrawString(Strings.Format(progressVal, "#,##0.00") & 進度條後墜, cellStyle.Font, foreBrush, cellBounds.X + 6, cellBounds.Y + 2)
- Else
- '繪製文本
- If Not Me.DataGridView.CurrentCell Is Nothing AndAlso Me.DataGridView.CurrentCell.RowIndex = rowIndex Then
- g.DrawString(Strings.Format(progressVal, "#,##0.00") & 進度條後墜, cellStyle.Font, New SolidBrush(cellStyle.SelectionForeColor), cellBounds.X + 6, cellBounds.Y + 2)
- Else : g.DrawString(Strings.Format(progressVal, "#,##0.00") & 進度條後墜, cellStyle.Font, foreBrush, cellBounds.X + 6, cellBounds.Y + 2) : End If
- End If
- End Sub
- End Class
- End Module
|