Няма описание
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 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 深色風格 = False Then
  42. If percentage > 0.0 And percentage < 分段1 Then
  43. ' 繪製進度條和文字
  44. g.FillRectangle(New SolidBrush(Color.LightBlue), cellBounds.X + 2, cellBounds.Y + 2, Convert.ToInt32((percentage * cellBounds.Width - 8)), cellBounds.Height - 8)
  45. g.DrawString(Strings.Format(progressVal, "#,##0.00") & 進度條後墜, cellStyle.Font, foreBrush, cellBounds.X + 6, cellBounds.Y + 2)
  46. ElseIf percentage > 分段2 And percentage < 分段3 Then
  47. ' 繪製進度條和文字
  48. g.FillRectangle(New SolidBrush(Color.LightGreen), cellBounds.X + 2, cellBounds.Y + 2, Convert.ToInt32((percentage * cellBounds.Width - 8)), cellBounds.Height - 8)
  49. g.DrawString(Strings.Format(progressVal, "#,##0.00") & 進度條後墜, cellStyle.Font, foreBrush, cellBounds.X + 6, cellBounds.Y + 2)
  50. ElseIf percentage > 分段4 Then
  51. ' 繪製進度條和文字
  52. g.FillRectangle(New SolidBrush(Color.LightPink), cellBounds.X + 2, cellBounds.Y + 2, Convert.ToInt32((percentage * cellBounds.Width - 8)), cellBounds.Height - 8)
  53. g.DrawString(Strings.Format(progressVal, "#,##0.00") & 進度條後墜, cellStyle.Font, foreBrush, cellBounds.X + 6, cellBounds.Y + 2)
  54. Else
  55. '繪製文本
  56. If DataGridView.CurrentCell IsNot Nothing AndAlso Me.DataGridView.CurrentCell.RowIndex = rowIndex Then
  57. g.DrawString(Strings.Format(progressVal, "#,##0.00") & 進度條後墜, cellStyle.Font, New SolidBrush(cellStyle.SelectionForeColor), cellBounds.X + 6, cellBounds.Y + 2)
  58. Else : g.DrawString(Strings.Format(progressVal, "#,##0.00") & 進度條後墜, cellStyle.Font, foreBrush, cellBounds.X + 6, cellBounds.Y + 2) : End If
  59. End If
  60. Else
  61. If percentage > 0.0 And percentage < 分段1 Then
  62. ' 繪製進度條和文字
  63. g.FillRectangle(New SolidBrush(Color.DarkBlue), cellBounds.X + 2, cellBounds.Y + 2, Convert.ToInt32((percentage * cellBounds.Width - 8)), cellBounds.Height - 8)
  64. g.DrawString(Strings.Format(progressVal, "#,##0.00") & 進度條後墜, cellStyle.Font, foreBrush, cellBounds.X + 6, cellBounds.Y + 2)
  65. ElseIf percentage > 分段2 And percentage < 分段3 Then
  66. ' 繪製進度條和文字
  67. g.FillRectangle(New SolidBrush(Color.DarkGreen), cellBounds.X + 2, cellBounds.Y + 2, Convert.ToInt32((percentage * cellBounds.Width - 8)), cellBounds.Height - 8)
  68. g.DrawString(Strings.Format(progressVal, "#,##0.00") & 進度條後墜, cellStyle.Font, foreBrush, cellBounds.X + 6, cellBounds.Y + 2)
  69. ElseIf percentage > 分段4 Then
  70. ' 繪製進度條和文字
  71. g.FillRectangle(New SolidBrush(Color.DarkRed), cellBounds.X + 2, cellBounds.Y + 2, Convert.ToInt32((percentage * cellBounds.Width - 8)), cellBounds.Height - 8)
  72. g.DrawString(Strings.Format(progressVal, "#,##0.00") & 進度條後墜, cellStyle.Font, foreBrush, cellBounds.X + 6, cellBounds.Y + 2)
  73. Else
  74. '繪製文本
  75. If DataGridView.CurrentCell IsNot Nothing AndAlso Me.DataGridView.CurrentCell.RowIndex = rowIndex Then
  76. g.DrawString(Strings.Format(progressVal, "#,##0.00") & 進度條後墜, cellStyle.Font, New SolidBrush(cellStyle.SelectionForeColor), cellBounds.X + 6, cellBounds.Y + 2)
  77. Else : g.DrawString(Strings.Format(progressVal, "#,##0.00") & 進度條後墜, cellStyle.Font, foreBrush, cellBounds.X + 6, cellBounds.Y + 2) : End If
  78. End If
  79. End If
  80. End Sub
  81. End Class
  82. End Module