Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- source = ""
- source += "using System;\n"
- source += "using System.Reflection;\n"
- source += "using System.Runtime.InteropServices;\n"
- source += "using System.Drawing;\n"
- source += "using System.Windows.Forms;\n"
- source += "namespace DataGridViewExtension\n"
- source += " {\n"
- source += " public class Style\n"
- source += " {\n"
- source += " public void SetStyle(Control control, ControlStyles styles, bool newValue)\n"
- source += " {\n"
- source += " object[] args = { styles, newValue };\n"
- source += " typeof(Control).InvokeMember(\"SetStyle\",\n"
- source += " BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.InvokeMethod,\n"
- source += " null, control, args);\n"
- source += " }\n"
- source += " public bool SetSelectable(Control control, bool newValue)\n"
- source += " {\n"
- source += " SetStyle(control, ControlStyles.Selectable, newValue);\n"
- source += " return newValue;\n"
- source += " }\n"
- source += " public void SetDoubleBuffer(Control control)\n"
- source += " {\n"
- source += " SetStyle(control, ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.Opaque, true);\n"
- source += " }\n"
- source += " }\n"
- source += " public class ImageTextCellColumn : DataGridViewImageColumn\n"
- source += " {\n"
- source += " private Boolean showLabel;\n"
- source += " private String label;\n"
- source += " private ContentAlignment labelAlign;\n"
- source += " private Color labelBackColor;\n"
- source += " private Color labelForeColor;\n"
- source += " public ImageTextCellColumn()\n"
- source += " {\n"
- source += " this.ShowLabel = true;\n"
- source += " this.LabelForeColor = Color.White;\n"
- source += " this.LabelBackColor = Color.Black;\n"
- source += " this.CellTemplate = new ImageTextCell();\n"
- source += " }\n"
- source += " public override object Clone()\n"
- source += " {\n"
- source += " ImageTextCellColumn c = base.Clone() as ImageTextCellColumn;\n"
- source += " c.ShowLabel = this.showLabel;\n"
- source += " c.Label = this.label;\n"
- source += " c.LabelAlign = this.labelAlign;\n"
- source += " c.LabelForeColor = this.labelForeColor;\n"
- source += " c.LabelBackColor = this.labelBackColor;\n"
- source += " return c;\n"
- source += " }\n"
- source += " private ImageTextCell ImageTextCellTemplate\n"
- source += " {\n"
- source += " get { return this.CellTemplate as ImageTextCell; }\n"
- source += " }\n"
- source += " public Boolean ShowLabel\n"
- source += " {\n"
- source += " get { return this.showLabel; }\n"
- source += " set { this.showLabel = value; }\n"
- source += " }\n"
- source += " public String Label\n"
- source += " {\n"
- source += " get { return this.label; }\n"
- source += " set { this.label = value; }\n"
- source += " }\n"
- source += " public ContentAlignment LabelAlign\n"
- source += " {\n"
- source += " get { return this.labelAlign; }\n"
- source += " set { this.labelAlign = value; }\n"
- source += " }\n"
- source += " public Color LabelForeColor\n"
- source += " {\n"
- source += " get { return this.labelForeColor; }\n"
- source += " set { this.labelForeColor = value; }\n"
- source += " }\n"
- source += " public Color LabelBackColor\n"
- source += " {\n"
- source += " get { return this.labelBackColor; }\n"
- source += " set { this.labelBackColor = value; }\n"
- source += " }\n"
- source += " }\n"
- source += " public class ImageTextCell : DataGridViewImageCell\n"
- source += " {\n"
- source += " private Boolean showLabel;\n"
- source += " private String label;\n"
- source += " private Color labelBackColor;\n"
- source += " private Color labelForeColor;\n"
- source += " public ImageTextCell()\n"
- source += " {\n"
- source += " this.ShowLabel = true;\n"
- source += " }\n"
- source += " public override object Clone()\n"
- source += " {\n"
- source += " ImageTextCell c = base.Clone() as ImageTextCell;\n"
- source += " c.ShowLabel = this.showLabel;\n"
- source += " c.Label = this.label;\n"
- source += " c.LabelForeColor = this.labelForeColor;\n"
- source += " c.LabelBackColor = this.labelBackColor;\n"
- source += " return c;\n"
- source += " }\n"
- source += " public Boolean ShowLabel\n"
- source += " {\n"
- source += " get\n"
- source += " {\n"
- source += " if (this.OwningColumn == null || this.ImageTextCellColumn == null) { return showLabel; }\n"
- source += " else return (this.showLabel & this.ImageTextCellColumn.ShowLabel);\n"
- source += " }\n"
- source += " set { if (this.showLabel != value) { this.showLabel = value; } }\n"
- source += " }\n"
- source += " public String Label\n"
- source += " {\n"
- source += " get\n"
- source += " {\n"
- source += " if (this.OwningColumn == null || this.ImageTextCellColumn == null) { return label; }\n"
- source += " else if (this.label != null)\n"
- source += " {\n"
- source += " return this.label;\n"
- source += " }\n"
- source += " else\n"
- source += " {\n"
- source += " return this.ImageTextCellColumn.Label;\n"
- source += " }\n"
- source += " }\n"
- source += " set { if (this.label != value) { this.label = value; } }\n"
- source += " }\n"
- source += " public Color LabelForeColor\n"
- source += " {\n"
- source += " get\n"
- source += " {\n"
- source += " if (this.OwningColumn == null || this.ImageTextCellColumn == null) { return labelForeColor; }\n"
- source += " else if (this.labelForeColor != Color.Empty)\n"
- source += " {\n"
- source += " return this.labelForeColor;\n"
- source += " }\n"
- source += " else\n"
- source += " {\n"
- source += " return this.ImageTextCellColumn.LabelForeColor;\n"
- source += " }\n"
- source += " }\n"
- source += " set { if (this.labelForeColor != value) { this.labelForeColor = value; } }\n"
- source += " }\n"
- source += " public Color LabelBackColor\n"
- source += " {\n"
- source += " get\n"
- source += " {\n"
- source += " if (this.OwningColumn == null || this.ImageTextCellColumn == null) { return labelBackColor; }\n"
- source += " else if (this.labelBackColor != Color.Empty)\n"
- source += " {\n"
- source += " return this.labelBackColor;\n"
- source += " }\n"
- source += " else\n"
- source += " {\n"
- source += " return this.ImageTextCellColumn.LabelBackColor;\n"
- source += " }\n"
- source += " }\n"
- source += " set { if (this.labelBackColor != value) { this.labelBackColor = value; } }\n"
- source += " }\n"
- source += " protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex,\n"
- source += " DataGridViewElementStates cellState, object value, object formattedValue, string errorText,\n"
- source += " DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts)\n"
- source += " {\n"
- source += " // Paint the base content\n"
- source += " base.Paint(graphics, clipBounds, cellBounds, rowIndex,\n"
- source += " cellState, value, formattedValue, errorText,\n"
- source += " cellStyle, advancedBorderStyle, paintParts);\n"
- source += " if (this.ShowLabel && this.Label != null)\n"
- source += " {\n"
- source += " // Draw the image clipped to the cell.\n"
- source += " System.Drawing.Drawing2D.GraphicsContainer container = graphics.BeginContainer();\n"
- source += " graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;\n"
- source += " SizeF ss = TextRenderer.MeasureText(this.Label, cellStyle.Font);\n"
- source += " if (ss.Width > this.Size.Width)\n"
- source += " {\n"
- source += " this.Label = this.Label.Insert((this.Label.Length/2),(\"-\"+Environment.NewLine));\n"
- source += " ss = TextRenderer.MeasureText(this.Label, cellStyle.Font);\n"
- source += " }\n"
- source += " ss = SizeF.Add(ss, new SizeF(0, 2));\n"
- source += " Single px = cellBounds.X;\n"
- source += " Single tx, py;\n"
- source += " Single gap = cellBounds.Width-ss.Width;\n"
- source += " RectangleF elRect;\n"
- source += " switch (this.ImageTextCellColumn.LabelAlign)\n"
- source += " {\n"
- source += " case ContentAlignment.BottomCenter:\n"
- source += " {\n"
- source += " tx = (this.Size.Width - (ss.Width)) / 2 + px;\n"
- source += " py = (cellBounds.Height - ss.Height-1) + cellBounds.Y;\n"
- source += " elRect = new RectangleF(px,py,gap,ss.Height*2);\n"
- source += " break;\n"
- source += " }\n"
- source += " case ContentAlignment.MiddleCenter:\n"
- source += " {\n"
- source += " tx = (cellBounds.Width - ss.Width) / 2 + px;\n"
- source += " py = (cellBounds.Height - ss.Height-1) / 2 + cellBounds.Y;\n"
- source += " elRect = new RectangleF(px,py,gap,ss.Height);\n"
- source += " break;\n"
- source += " }\n"
- source += " case ContentAlignment.TopCenter:\n"
- source += " {\n"
- source += " tx = (cellBounds.Width - ss.Width) / 2 + px;\n"
- source += " py = cellBounds.Y;\n"
- source += " elRect = new RectangleF(px,(py-ss.Height),gap,ss.Height*2);\n"
- source += " break;\n"
- source += " }\n"
- source += " case ContentAlignment.BottomLeft:\n"
- source += " {\n"
- source += " tx = px;\n"
- source += " py = (cellBounds.Height - ss.Height-1) + cellBounds.Y;\n"
- source += " elRect = new RectangleF(px+ss.Width-cellBounds.Width,py,gap*2,ss.Height*2);\n"
- source += " break;\n"
- source += " }\n"
- source += " case ContentAlignment.MiddleLeft:\n"
- source += " {\n"
- source += " tx = px;\n"
- source += " py = (cellBounds.Height - ss.Height-1) / 2 + cellBounds.Y;\n"
- source += " elRect = new RectangleF(px+ss.Width-cellBounds.Width,py,gap*2,ss.Height);\n"
- source += " break;\n"
- source += " }\n"
- source += " case ContentAlignment.TopLeft:\n"
- source += " {\n"
- source += " tx = px;\n"
- source += " py = cellBounds.Y;\n"
- source += " elRect = new RectangleF(px+ss.Width-cellBounds.Width,(py-ss.Height),gap*2,ss.Height*2);\n"
- source += " break;\n"
- source += " }\n"
- source += " case ContentAlignment.BottomRight:\n"
- source += " {\n"
- source += " tx = (cellBounds.Width - ss.Width) + px;\n"
- source += " py = (cellBounds.Height - ss.Height-1) + cellBounds.Y;\n"
- source += " elRect = new RectangleF(px,py,gap*2,ss.Height*2);\n"
- source += " break;\n"
- source += " }\n"
- source += " case ContentAlignment.MiddleRight:\n"
- source += " {\n"
- source += " tx = (cellBounds.Width - ss.Width) / 2 + px;\n"
- source += " py = (cellBounds.Height - ss.Height-1) + cellBounds.Y;\n"
- source += " elRect = new RectangleF(px,py,gap*2,ss.Height);\n"
- source += " break;\n"
- source += " }\n"
- source += " default:\n"
- source += " {\n"
- source += " tx = (cellBounds.Width - ss.Width) + px;\n"
- source += " py = cellBounds.Y;\n"
- source += " elRect = new RectangleF(px,(py-ss.Height),gap*2,ss.Height*2);\n"
- source += " break;\n"
- source += " }\n"
- source += " }\n"
- source += " graphics.DrawString(this.Label, cellStyle.Font, new SolidBrush(this.Selected ? this.LabelBackColor : this.LabelForeColor),tx,py);\n"
- source += " graphics.EndContainer(container);\n"
- source += " }\n"
- source += " }\n"
- source += " public ImageTextCellColumn ImageTextCellColumn\n"
- source += " {\n"
- source += " get { return this.OwningColumn as ImageTextCellColumn; }\n"
- source += " }\n"
- source += " }\n"
- source += " }\n"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement