yangyin
2025-02-28 baa80d650adebcce70f1113cc1020c6039c159a0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
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);
                }
            }
        }
    }
}