Без опису
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. Imports System.ComponentModel
  2. Module DGV進度條
  3. Public Class DataGridViewProgressColumn
  4. Inherits DataGridViewImageColumn
  5. Public Sub New()
  6. Me.CellTemplate = New DataGridViewProgressCell
  7. End Sub
  8. End Class
  9. Public Class DataGridViewProgressCell
  10. Inherits DataGridViewImageCell
  11. Sub New()
  12. ValueType = Type.GetType("Integer")
  13. End Sub
  14. ' 使進度單元與默認圖像單元一致所需的方法。
  15. ' 默認圖像單元將圖像作為值,儘管進度單元的值是整數。
  16. Protected Overrides Function GetFormattedValue(
  17. ByVal value As Object,
  18. ByVal rowIndex As Integer,
  19. ByRef cellStyle As DataGridViewCellStyle,
  20. ByVal valueTypeConverter As TypeConverter,
  21. ByVal formattedValueTypeConverter As TypeConverter,
  22. ByVal context As DataGridViewDataErrorContexts) As Object
  23. Static emptyImage As Bitmap = New Bitmap(1, 1, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
  24. GetFormattedValue = emptyImage
  25. End Function
  26. Protected Overrides Sub Paint(ByVal g As System.Drawing.Graphics, ByVal clipBounds As System.Drawing.Rectangle,
  27. ByVal cellBounds As System.Drawing.Rectangle,
  28. ByVal rowIndex As Integer, ByVal cellState As System.Windows.Forms.DataGridViewElementStates,
  29. ByVal value As Object, ByVal formattedValue As Object, ByVal errorText As String,
  30. ByVal cellStyle As System.Windows.Forms.DataGridViewCellStyle,
  31. ByVal advancedBorderStyle As System.Windows.Forms.DataGridViewAdvancedBorderStyle,
  32. ByVal paintParts As System.Windows.Forms.DataGridViewPaintParts)
  33. Dim progressVal As Double = CType(value, Double)
  34. Dim percentage As Single = CType(progressVal / 分母, Single)
  35. Dim backBrush As Brush = New SolidBrush(cellStyle.BackColor)
  36. Dim foreBrush As Brush = New SolidBrush(cellStyle.ForeColor)
  37. ' 調用基類方法來繪製默認單元格外觀。
  38. MyBase.Paint(g, clipBounds, cellBounds, rowIndex, cellState,
  39. value, formattedValue, errorText, cellStyle,
  40. advancedBorderStyle, paintParts)
  41. If percentage > 0.0 And percentage < 分段1 Then
  42. ' 繪製進度條和文字
  43. g.FillRectangle(New SolidBrush(Color.LightBlue), cellBounds.X + 2, cellBounds.Y + 2, Convert.ToInt32((percentage * cellBounds.Width - 8)), cellBounds.Height - 8)
  44. g.DrawString(Strings.Format(progressVal, "#,##0.00") & 進度條後墜, cellStyle.Font, foreBrush, cellBounds.X + 6, cellBounds.Y + 2)
  45. ElseIf percentage > 分段2 And percentage < 分段3 Then
  46. ' 繪製進度條和文字
  47. g.FillRectangle(New SolidBrush(Color.LightGreen), cellBounds.X + 2, cellBounds.Y + 2, Convert.ToInt32((percentage * cellBounds.Width - 8)), cellBounds.Height - 8)
  48. g.DrawString(Strings.Format(progressVal, "#,##0.00") & 進度條後墜, cellStyle.Font, foreBrush, cellBounds.X + 6, cellBounds.Y + 2)
  49. ElseIf percentage > 分段4 Then
  50. ' 繪製進度條和文字
  51. g.FillRectangle(New SolidBrush(Color.LightPink), cellBounds.X + 2, cellBounds.Y + 2, Convert.ToInt32((percentage * cellBounds.Width - 8)), cellBounds.Height - 8)
  52. g.DrawString(Strings.Format(progressVal, "#,##0.00") & 進度條後墜, cellStyle.Font, foreBrush, cellBounds.X + 6, cellBounds.Y + 2)
  53. Else
  54. '繪製文本
  55. If Not Me.DataGridView.CurrentCell Is Nothing AndAlso Me.DataGridView.CurrentCell.RowIndex = rowIndex Then
  56. g.DrawString(Strings.Format(progressVal, "#,##0.00") & 進度條後墜, cellStyle.Font, New SolidBrush(cellStyle.SelectionForeColor), cellBounds.X + 6, cellBounds.Y + 2)
  57. Else : g.DrawString(Strings.Format(progressVal, "#,##0.00") & 進度條後墜, cellStyle.Font, foreBrush, cellBounds.X + 6, cellBounds.Y + 2) : End If
  58. End If
  59. End Sub
  60. End Class
  61. End Module