1
yhj
2024-07-24 5e5d945e91568b973faa27d8ab0bcef99fc4a6c5
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#region
 
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
 
#endregion
 
namespace CSFrameworkV5.UserCustom
{
    public class GZTableClass
    {
        public static GZTable GetGZTable(Control col, int width, int height)
        {
            var PanWidth = col.Width - 20; //预留给滚动条20像素
            var PanHeight = col.Height - 20; //预留给滚动条20像素
            //计算表格的行数和列数
            var ColumnCoount = PanWidth / width;
            var RowCount = PanHeight / height;
            return new GZTable(ColumnCoount, width, height);
        }
 
        public static GZTable GetGZTable(int width, int height, int CellCount)
        {
            return new GZTable(CellCount, width, height);
        }
    }
 
    public class GZCell
    {
        private int _ColIndex;
 
        private Control _ContainControl;
 
        private int _Height;
 
        //private Point _Location;
        private bool _IsUsed;
        private int _RowIndex;
 
        private int _Width;
        ///// <summary>
        ///// 构造函数,指定单元格坐标
        ///// </summary>
        ///// <param name="Width">单元格宽度</param>
        ///// <param name="Height">单元格高度</param>
        //public GZCell(int Width, int Height, Point Location)
        //{
        //    _Width = Width;
        //    _Height = Height;
        //    _Location = Location;
        //}
 
        /// <summary>
        ///     构造函数,指定第几行第几列
        /// </summary>
        /// <param name="Width">单元格宽度</param>
        /// <param name="Height">单元格高度</param>
        public GZCell(int Width, int Height, int RowIndex, int ColIndex)
        {
            _Width = Width;
            _Height = Height;
            _RowIndex = RowIndex;
            _ColIndex = ColIndex;
        }
 
        public int ColIndex => _ColIndex;
 
        public Control ContainControl
        {
            get => _ContainControl;
            set
            {
                _ContainControl = value;
                UpdateControl();
            }
        }
 
        /// <summary>
        ///     单元格高度
        /// </summary>
        public int Height => _Height;
 
        public bool IsUsed
        {
            get => _IsUsed;
            set => _IsUsed = value;
        }
 
        public Point Location
        {
            get
            {
                var W = Width * ColIndex + 20;
                var H = Height * RowIndex;
 
                return new Point(W, H);
            }
        }
 
        public int RowIndex => _RowIndex;
 
        /// <summary>
        ///     单元格宽度
        /// </summary>
        public int Width => _Width;
 
        public void UpdateControl()
        {
            if (_ContainControl != null)
            {
                _ContainControl.Location = Location;
                IsUsed = true;
            }
            else
            {
                IsUsed = false;
            }
        }
    }
 
    public class GZRow
    {
        private List<GZCell> lstGZRow = new List<GZCell>();
 
        public GZRow(int ColCount, int width, int height, int RowIndex)
        {
            for (var i = 0; i < ColCount; i++)
                lstGZRow.Add(new GZCell(width, height, RowIndex, i));
        }
 
        public GZCell this[int Index] => lstGZRow[Index];
        ///// <summary>
        ///// 删除单元格
        ///// </summary>
        ///// <param name="GCell"></param>
        //public void RemoveCell(GZCell GCell)
        //{
        //    lstGZRow.Remove(GCell);
        //}
        ///// <summary>
        ///// 删除单元格
        ///// </summary>
        ///// <param name="Index"></param>
        //public void RemoveCell(int Index)
        //{
        //    RemoveCell(lstGZRow[Index]);
        //}
 
        public int Count()
        {
            return lstGZRow.Count;
        }
    }
 
    public class GZTable
    {
        private int _CellCount;
        private int _CellHeight;
        private int _CellWidth;
        private List<GZRow> lstRow = new List<GZRow>();
 
        public GZTable(int ColumnCount, int CellWidth, int CellHeight)
        {
            _CellCount = ColumnCount;
            _CellWidth = CellWidth;
            _CellHeight = CellHeight;
        }
 
        public int CellCount => _CellCount;
 
        public int CellHeight => _CellHeight;
 
        public int CellWidth => _CellWidth;
 
        public GZRow this[int Index]
        {
            get
            {
                if (Index <= lstRow.Count - 1) return lstRow[Index];
 
                return null;
            }
        }
 
        public int RowCount => lstRow.Count;
 
        /// <summary>
        ///     添加一行
        /// </summary>
        /// <param name="CellNum"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        public void AddRow()
        {
            var GRow = new GZRow(_CellCount, CellWidth, CellHeight, RowCount);
            lstRow.Add(GRow);
        }
 
        public GZCell GetCellByPoint(Point P)
        {
            var X = P.X;
            var Y = P.Y;
 
            if (X < 0 || Y < 0) return null;
 
            var PanWidth = CellCount * CellWidth;
            var PanHeight = RowCount * CellHeight;
            if (X < PanWidth && Y < PanHeight)
            {
                var I = X / CellWidth - 1;
                var J = Y / CellHeight - 1;
                if (X % CellWidth > 5) I++;
 
                if (Y % CellHeight > 5) J++;
 
                if (I < 0 || J < 0) return null;
 
                return lstRow[J][I];
            }
 
            return null;
        }
 
        ///// <summary>
        ///// 删除行
        ///// </summary>
        ///// <param name="Grow"></param>
        //public void RemoveRow(GZRow Grow)
        //{
        //    lstRow.Remove(Grow);
        //}
        ///// <summary>
        ///// 删除行
        ///// </summary>
        ///// <param name="index"></param>
        //public void RemoveRow(int index)
        //{
        //    RemoveRow(lstRow[index]);
        //}
    }
}