using System.Drawing; using System.Windows.Forms; using System.Windows.Forms.VisualStyles; namespace DPumpHydr.WinFrmUI.WenSkin.Controls { public class WenDataGridViewTextBoxButtonCell : DataGridViewTextBoxCell { public WenDataGridViewTextBoxButtonCell() : base() { } public int Width { get; set; } public int Height { get; set; } public bool ButtonClick { get; set; } protected override void Paint(Graphics graphics, Rectangle clipBounds, Rectangle cellBounds, int rowIndex, DataGridViewElementStates cellState, object value, object formattedValue, string errorText, DataGridViewCellStyle cellStyle, DataGridViewAdvancedBorderStyle advancedBorderStyle, DataGridViewPaintParts paintParts) { base.Paint(graphics, clipBounds, cellBounds, rowIndex, cellState, value, formattedValue, errorText, cellStyle, advancedBorderStyle, paintParts); Width = cellBounds.Width; Height = cellBounds.Height; //计算按钮区域 Rectangle rec = new Rectangle(cellBounds.X + (cellBounds.Width - 15), cellBounds.Y + (cellBounds.Height - 20) / 2, 13, 20); if (ButtonClick) { ButtonRenderer.DrawButton(graphics, rec, PushButtonState.Pressed); ButtonClick = false; } else { ButtonRenderer.DrawButton(graphics, rec, PushButtonState.Normal); } } protected override void OnMouseClick(DataGridViewCellMouseEventArgs e) { base.OnMouseClick(e); //计算按钮区域 Rectangle rec = new Rectangle(Width - 20, 0, Width, Height); if (rec.Contains(e.Location)) { ButtonClick = true; if(this.DataGridView is WenDataGridView dataGridView) { dataGridView.OnTextBoxButtonClick(e); } } } } }