Advertisement
Guest User

Untitled

a guest
Apr 26th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.26 KB | None | 0 0
  1. source = ""
  2. source += "using System;\n"
  3. source += "using System.Reflection;\n"
  4. source += "using System.Runtime.InteropServices;\n"
  5. source += "using System.Drawing;\n"
  6. source += "using System.Windows.Forms;\n"
  7. source += "namespace DataGridViewExtension\n"
  8. source += " {\n"
  9. source += " public class Style\n"
  10. source += " {\n"
  11. source += " public void SetStyle(Control control, ControlStyles styles, bool newValue)\n"
  12. source += " {\n"
  13. source += " object[] args = { styles, newValue };\n"
  14. source += " typeof(Control).InvokeMember(\"SetStyle\",\n"
  15. source += " BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod,\n"
  16. source += " null, control, args);\n"
  17. source += " }\n"
  18. source += " public bool SetSelectable(Control control, bool newValue)\n"
  19. source += " {\n"
  20. source += " SetStyle(control, ControlStyles.Selectable, newValue);\n"
  21. source += " return newValue;\n"
  22. source += " }\n"
  23. source += " public void SetDoubleBuffer(Control control)\n"
  24. source += " {\n"
  25. source += " SetStyle(control, ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);\n"
  26. source += " }\n"
  27. source += " }\n"
  28. source += " public class ImageTextCellColumn : DataGridViewImageColumn\n"
  29. source += " {\n"
  30. source += " private Boolean showLabel;\n"
  31. source += " private String label;\n"
  32. source += " private ContentAlignment labelAlign;\n"
  33. source += " private Color labelBackColor;\n"
  34. source += " private Color labelForeColor;\n"
  35. source += " public ImageTextCellColumn()\n"
  36. source += " {\n"
  37. source += " this.ShowLabel = true;\n"
  38. source += " this.LabelForeColor = Color.White;\n"
  39. source += " this.LabelBackColor = Color.Black;\n"
  40. source += " this.CellTemplate = new ImageTextCell();\n"
  41. source += " }\n"
  42. source += " public override object Clone()\n"
  43. source += " {\n"
  44. source += " ImageTextCellColumn c = base.Clone() as ImageTextCellColumn;\n"
  45. source += " c.ShowLabel = this.showLabel;\n"
  46. source += " c.Label = this.label;\n"
  47. source += " c.LabelAlign = this.labelAlign;\n"
  48. source += " c.LabelForeColor = this.labelForeColor;\n"
  49. source += " c.LabelBackColor = this.labelBackColor;\n"
  50. source += " return c;\n"
  51. source += " }\n"
  52. source += " private ImageTextCell ImageTextCellTemplate\n"
  53. source += " {\n"
  54. source += " get { return this.CellTemplate as ImageTextCell; }\n"
  55. source += " }\n"
  56. source += " public Boolean ShowLabel\n"
  57. source += " {\n"
  58. source += " get { return this.showLabel; }\n"
  59. source += " set { this.showLabel = value; }\n"
  60. source += " }\n"
  61. source += " public String Label\n"
  62. source += " {\n"
  63. source += " get { return this.label; }\n"
  64. source += " set { this.label = value; }\n"
  65. source += " }\n"
  66. source += " public ContentAlignment LabelAlign\n"
  67. source += " {\n"
  68. source += " get { return this.labelAlign; }\n"
  69. source += " set { this.labelAlign = value; }\n"
  70. source += " }\n"
  71. source += " public Color LabelForeColor\n"
  72. source += " {\n"
  73. source += " get { return this.labelForeColor; }\n"
  74. source += " set { this.labelForeColor = value; }\n"
  75. source += " }\n"
  76. source += " public Color LabelBackColor\n"
  77. source += " {\n"
  78. source += " get { return this.labelBackColor; }\n"
  79. source += " set { this.labelBackColor = value; }\n"
  80. source += " }\n"
  81. source += " }\n"
  82. source += " public class ImageTextCell : DataGridViewImageCell\n"
  83. source += " {\n"
  84. source += " private Boolean showLabel;\n"
  85. source += " private String label;\n"
  86. source += " private Color labelBackColor;\n"
  87. source += " private Color labelForeColor;\n"
  88. source += " public ImageTextCell()\n"
  89. source += " {\n"
  90. source += " this.ShowLabel = true;\n"
  91. source += " }\n"
  92. source += " public override object Clone()\n"
  93. source += " {\n"
  94. source += " ImageTextCell c = base.Clone() as ImageTextCell;\n"
  95. source += " c.ShowLabel = this.showLabel;\n"
  96. source += " c.Label = this.label;\n"
  97. source += " c.LabelForeColor = this.labelForeColor;\n"
  98. source += " c.LabelBackColor = this.labelBackColor;\n"
  99. source += " return c;\n"
  100. source += " }\n"
  101. source += " public Boolean ShowLabel\n"
  102. source += " {\n"
  103. source += " get\n"
  104. source += " {\n"
  105. source += " if (this.OwningColumn == null || this.ImageTextCellColumn == null) { return showLabel; }\n"
  106. source += " else return (this.showLabel & this.ImageTextCellColumn.ShowLabel);\n"
  107. source += " }\n"
  108. source += " set { if (this.showLabel != value) { this.showLabel = value; } }\n"
  109. source += " }\n"
  110. source += " public String Label\n"
  111. source += " {\n"
  112. source += " get\n"
  113. source += " {\n"
  114. source += " if (this.OwningColumn == null || this.ImageTextCellColumn == null) { return label; }\n"
  115. source += " else if (this.label != null)\n"
  116. source += " {\n"
  117. source += " return this.label;\n"
  118. source += " }\n"
  119. source += " else\n"
  120. source += " {\n"
  121. source += " return this.ImageTextCellColumn.Label;\n"
  122. source += " }\n"
  123. source += " }\n"
  124. source += " set { if (this.label != value) { this.label = value; } }\n"
  125. source += " }\n"
  126. source += " public Color LabelForeColor\n"
  127. source += " {\n"
  128. source += " get\n"
  129. source += " {\n"
  130. source += " if (this.OwningColumn == null || this.ImageTextCellColumn == null) { return labelForeColor; }\n"
  131. source += " else if (this.labelForeColor != Color.Empty)\n"
  132. source += " {\n"
  133. source += " return this.labelForeColor;\n"
  134. source += " }\n"
  135. source += " else\n"
  136. source += " {\n"
  137. source += " return this.ImageTextCellColumn.LabelForeColor;\n"
  138. source += " }\n"
  139. source += " }\n"
  140. source += " set { if (this.labelForeColor != value) { this.labelForeColor = value; } }\n"
  141. source += " }\n"
  142. source += " public Color LabelBackColor\n"
  143. source += " {\n"
  144. source += " get\n"
  145. source += " {\n"
  146. source += " if (this.OwningColumn == null || this.ImageTextCellColumn == null) { return labelBackColor; }\n"
  147. source += " else if (this.labelBackColor != Color.Empty)\n"
  148. source += " {\n"
  149. source += " return this.labelBackColor;\n"
  150. source += " }\n"
  151. source += " else\n"
  152. source += " {\n"
  153. source += " return this.ImageTextCellColumn.LabelBackColor;\n"
  154. source += " }\n"
  155. source += " }\n"
  156. source += " set { if (this.labelBackColor != value) { this.labelBackColor = value; } }\n"
  157. source += " }\n"
  158. source += " protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex,\n"
  159. source += " DataGridViewElementStates cellState, object value, object formattedValue, string errorText,\n"
  160. source += " DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)\n"
  161. source += " {\n"
  162. source += " // Paint the base content\n"
  163. source += " base.Paint(graphics, clipBounds, cellBounds, rowIndex,\n"
  164. source += " cellState, value, formattedValue, errorText,\n"
  165. source += " cellStyle, advancedBorderStyle, paintParts);\n"
  166. source += " if (this.ShowLabel && this.Label != null)\n"
  167. source += " {\n"
  168. source += " // Draw the image clipped to the cell.\n"
  169. source += " System.Drawing.Drawing2D.GraphicsContainer container = graphics.BeginContainer();\n"
  170. source += " graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;\n"
  171. source += " SizeF ss = TextRenderer.MeasureText(this.Label, cellStyle.Font);\n"
  172. source += " if (ss.Width > this.Size.Width)\n"
  173. source += " {\n"
  174. source += " this.Label = this.Label.Insert((this.Label.Length/2),(\"-\"+Environment.NewLine));\n"
  175. source += " ss = TextRenderer.MeasureText(this.Label, cellStyle.Font);\n"
  176. source += " }\n"
  177. source += " ss = SizeF.Add(ss, new SizeF(0, 2));\n"
  178. source += " Single px = cellBounds.X;\n"
  179. source += " Single tx, py;\n"
  180. source += " Single gap = cellBounds.Width-ss.Width;\n"
  181. source += " RectangleF elRect;\n"
  182. source += " switch (this.ImageTextCellColumn.LabelAlign)\n"
  183. source += " {\n"
  184. source += " case ContentAlignment.BottomCenter:\n"
  185. source += " {\n"
  186. source += " tx = (this.Size.Width - (ss.Width)) / 2 + px;\n"
  187. source += " py = (cellBounds.Height - ss.Height-1) + cellBounds.Y;\n"
  188. source += " elRect = new RectangleF(px,py,gap,ss.Height*2);\n"
  189. source += " break;\n"
  190. source += " }\n"
  191. source += " case ContentAlignment.MiddleCenter:\n"
  192. source += " {\n"
  193. source += " tx = (cellBounds.Width - ss.Width) / 2 + px;\n"
  194. source += " py = (cellBounds.Height - ss.Height-1) / 2 + cellBounds.Y;\n"
  195. source += " elRect = new RectangleF(px,py,gap,ss.Height);\n"
  196. source += " break;\n"
  197. source += " }\n"
  198. source += " case ContentAlignment.TopCenter:\n"
  199. source += " {\n"
  200. source += " tx = (cellBounds.Width - ss.Width) / 2 + px;\n"
  201. source += " py = cellBounds.Y;\n"
  202. source += " elRect = new RectangleF(px,(py-ss.Height),gap,ss.Height*2);\n"
  203. source += " break;\n"
  204. source += " }\n"
  205. source += " case ContentAlignment.BottomLeft:\n"
  206. source += " {\n"
  207. source += " tx = px;\n"
  208. source += " py = (cellBounds.Height - ss.Height-1) + cellBounds.Y;\n"
  209. source += " elRect = new RectangleF(px+ss.Width-cellBounds.Width,py,gap*2,ss.Height*2);\n"
  210. source += " break;\n"
  211. source += " }\n"
  212. source += " case ContentAlignment.MiddleLeft:\n"
  213. source += " {\n"
  214. source += " tx = px;\n"
  215. source += " py = (cellBounds.Height - ss.Height-1) / 2 + cellBounds.Y;\n"
  216. source += " elRect = new RectangleF(px+ss.Width-cellBounds.Width,py,gap*2,ss.Height);\n"
  217. source += " break;\n"
  218. source += " }\n"
  219. source += " case ContentAlignment.TopLeft:\n"
  220. source += " {\n"
  221. source += " tx = px;\n"
  222. source += " py = cellBounds.Y;\n"
  223. source += " elRect = new RectangleF(px+ss.Width-cellBounds.Width,(py-ss.Height),gap*2,ss.Height*2);\n"
  224. source += " break;\n"
  225. source += " }\n"
  226. source += " case ContentAlignment.BottomRight:\n"
  227. source += " {\n"
  228. source += " tx = (cellBounds.Width - ss.Width) + px;\n"
  229. source += " py = (cellBounds.Height - ss.Height-1) + cellBounds.Y;\n"
  230. source += " elRect = new RectangleF(px,py,gap*2,ss.Height*2);\n"
  231. source += " break;\n"
  232. source += " }\n"
  233. source += " case ContentAlignment.MiddleRight:\n"
  234. source += " {\n"
  235. source += " tx = (cellBounds.Width - ss.Width) / 2 + px;\n"
  236. source += " py = (cellBounds.Height - ss.Height-1) + cellBounds.Y;\n"
  237. source += " elRect = new RectangleF(px,py,gap*2,ss.Height);\n"
  238. source += " break;\n"
  239. source += " }\n"
  240. source += " default:\n"
  241. source += " {\n"
  242. source += " tx = (cellBounds.Width - ss.Width) + px;\n"
  243. source += " py = cellBounds.Y;\n"
  244. source += " elRect = new RectangleF(px,(py-ss.Height),gap*2,ss.Height*2);\n"
  245. source += " break;\n"
  246. source += " }\n"
  247. source += " }\n"
  248. source += " graphics.DrawString(this.Label, cellStyle.Font, new SolidBrush(this.Selected ? this.LabelBackColor : this.LabelForeColor),tx,py);\n"
  249. source += " graphics.EndContainer(container);\n"
  250. source += " }\n"
  251. source += " }\n"
  252. source += " public ImageTextCellColumn ImageTextCellColumn\n"
  253. source += " {\n"
  254. source += " get { return this.OwningColumn as ImageTextCellColumn; }\n"
  255. source += " }\n"
  256. source += " }\n"
  257. source += " }\n"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement