领胜LDS 键盘AOI检测项目
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using System;
using System.Drawing;
using System.Windows.Forms;
 
namespace Bro.Common.Helper
{
    public static class UIHelper
    {
        public static void SetCombo(ComboBox cbo, object dataSource, string display, string value)
        {
            cbo.DataSource = dataSource;
            cbo.DisplayMember = display;
            if (!string.IsNullOrWhiteSpace(value))
            {
                cbo.ValueMember = value;
            }
        }
 
        public static void SetCombo(ToolStripComboBox cbo, object dataSource, string display, string value)
        {
            cbo.ComboBox.DataSource = dataSource;
            cbo.ComboBox.DisplayMember = display;
            if (!string.IsNullOrWhiteSpace(value))
            {
                cbo.ComboBox.ValueMember = value;
            }
        }
 
        public static void SetCombo(DataGridViewComboBoxColumn cbo, object dataSource, string display, string value)
        {
            cbo.DataSource = dataSource;
            cbo.DisplayMember = display;
            if (!string.IsNullOrWhiteSpace(value))
            {
                cbo.ValueMember = value;
            }
        }
    }
 
    #region DataGridView设置列头为复选框
    public class DataGridViewCheckboxHeaderEventArgs : EventArgs
    {
        private bool checkedState = false;
 
        public bool CheckedState
        {
            get { return checkedState; }
            set { checkedState = value; }
        }
    }
 
    public delegate void DataGridViewCheckboxHeaderEventHander(object sender, DataGridViewCheckboxHeaderEventArgs e);
    public class DataGridViewCheckboxHeaderCell : DataGridViewColumnHeaderCell
    {
        Point checkBoxLocation;
        Size checkBoxSize;
        bool _checked = false;
        Point _cellLocation = new Point();
        System.Windows.Forms.VisualStyles.CheckBoxState _cbState = System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal;
 
        public event DataGridViewCheckboxHeaderEventHander OnCheckBoxClicked;
 
        //绘制列头checkbox
        protected override void Paint(System.Drawing.Graphics graphics,
                                        System.Drawing.Rectangle clipBounds,
                                        System.Drawing.Rectangle cellBounds,
                                        int rowIndex,
                                        DataGridViewElementStates dataGridViewElementState,
                                        object value,
                                        object formattedValue,
                                        string errorText,
                                        DataGridViewCellStyle cellStyle,
                                        DataGridViewAdvancedBorderStyle advancedBorderStyle,
                                        DataGridViewPaintParts paintParts)
        {
            base.Paint(graphics, clipBounds, cellBounds, rowIndex,
            dataGridViewElementState, value,
            formattedValue, errorText, cellStyle,
            advancedBorderStyle, paintParts);
 
            Point p = new Point();
            Size s = CheckBoxRenderer.GetGlyphSize(graphics, System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal);
            p.X = cellBounds.Location.X + (cellBounds.Width / 2) - (s.Width / 2) - 1;   //列头checkbox的X坐标
            p.Y = cellBounds.Location.Y + (cellBounds.Height / 2) - (s.Height / 2);     //列头checkbox的Y坐标
            _cellLocation = cellBounds.Location;
            checkBoxLocation = p;
            checkBoxSize = s;
            if (_checked)
                _cbState = System.Windows.Forms.VisualStyles.CheckBoxState.CheckedNormal;
            else
                _cbState = System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal;
 
            CheckBoxRenderer.DrawCheckBox(graphics, checkBoxLocation, _cbState);
        }
 
        ///
        /// 点击列头checkbox单击事件
        ///
        protected override void OnMouseClick(DataGridViewCellMouseEventArgs e)
        {
            Point p = new Point(e.X + _cellLocation.X, e.Y + _cellLocation.Y);
            if (p.X >= checkBoxLocation.X && p.X <= checkBoxLocation.X + checkBoxSize.Width
            && p.Y >= checkBoxLocation.Y && p.Y <= checkBoxLocation.Y + checkBoxSize.Height)
            {
                _checked = !_checked;
 
                //获取列头checkbox的选择状态
                DataGridViewCheckboxHeaderEventArgs ex = new DataGridViewCheckboxHeaderEventArgs();
                ex.CheckedState = _checked;
 
                object sender = new object();//此处不代表选择的列头checkbox,只是作为参数传递。因为列头checkbox是绘制出来的,无法获得它的实例
 
                if (OnCheckBoxClicked != null)
                {
                    OnCheckBoxClicked(sender, ex);//触发单击事件
                    DataGridView.InvalidateCell(this);
                }
            }
            base.OnMouseClick(e);
        }
    }
    #endregion
}