tangxu
2024-11-04 ebd031e3bed6c1cfddce8fc9b98f7f9e95fb9e32
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#region Imports
 
using DPumpHydr.WinFrmUI.RLT.Colors;
using DPumpHydr.WinFrmUI.RLT.Util;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Windows.Forms;
 
#endregion
 
namespace DPumpHydr.WinFrmUI.RLT.Controls
{
    #region HopeDatePicker
 
    public partial class HopeDatePicker : Control
    {
        #region General
 
        private RectangleF TopDateRect;
        private RectangleF WeekRect;
 
        private List<List<Util.HopeBase.DateRectHopeBase>> DateRectangles;
 
        private RectangleF PreviousMonthRect;
        private RectangleF NextMonthRect;
        private RectangleF PreviousYearRect;
        private RectangleF NextYearRect;
 
        private DateTime CurrentDate;
        public DateTime Date { get => CurrentDate.Date; set { CurrentDate = value; Invalidate(); } }
 
        public Color SelectedTextColor { get; set; } = Color.White;
        public Color SelectedBackColor { get; set; } = HopeColors.PrimaryColor;
        public Color ValueTextColor { get; set; } = HopeColors.DarkPrimary;
        public Color HoverColor { get; set; } = HopeColors.ThreeLevelBorder;
        public Color DayTextColorA { get; set; } = HopeColors.MainText;
        public Color DayTextColorB { get; set; } = HopeColors.SecondaryText;
        public Color HeadLineColor { get; set; } = HopeColors.TwoLevelBorder;
        public Color DaysTextColor { get; set; } = HopeColors.RegularText;
        public Color BorderColor { get; set; } = HopeColors.OneLevelBorder;
        public Color HeaderTextColor { get; set; } = HopeColors.MainText;
        public Color PYHoverColor { get; set; } = HopeColors.PrimaryColor;
        public Color PYColor { get; set; } = HopeColors.PlaceholderText;
        public Color NYHoverColor { get; set; } = HopeColors.PrimaryColor;
        public Color NYColor { get; set; } = HopeColors.PlaceholderText;
        public Color PMHoverColor { get; set; } = HopeColors.PrimaryColor;
        public Color PMColor { get; set; } = HopeColors.PlaceholderText;
        public Color NMHoverColor { get; set; } = HopeColors.PrimaryColor;
        public Color NMColor { get; set; } = HopeColors.PlaceholderText;
 
        private string _HeaderFormat = "{0} Y - {1} M"; //"{0} Y - {1,2} M"
        public string HeaderFormat
        {
            get => _HeaderFormat;
            set
            {
                try
                {
                    if (string.IsNullOrEmpty(value) || string.IsNullOrWhiteSpace(value) || value.Length < 9)
                    {
                        _HeaderFormat = "{0} Y - {1} M";
                    }
                    else
                    {
                        string.Format(value, CurrentDate.Year, CurrentDate.Month);
                        _HeaderFormat = value;
                    }
                }
                catch
                {
                    _HeaderFormat = "{0} Y - {1} M";
                }
            }
        }
 
        private string _DayNames = "MTWTFSS";
        public string DayNames
        {
            get => _DayNames;
            set
            {
                if (value.Length == 7)
                {
                    _DayNames = value;
                }
                else if (value.Length > 7)
                {
                    _DayNames = value.Substring(0, 7);
                }
                else
                {
                    _DayNames = "MTWTFSS";
                }
            }
        }
 
        private readonly int DateRectDefaultSize;
        private int HoverX;
        private int HoverY;
        private int SelectedX;
        private int SelectedY;
 
        private bool previousYearHovered;
        private bool previousMonthHovered;
        private bool nextMonthHovered;
        private bool nextYearHovered;
 
        #endregion
 
        #region Variables
        public delegate void DateChanged(DateTime newDateTime);
        public event DateChanged onDateChanged;
        #endregion
 
        #region Events
        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
 
            for (int i = 0; i < 7; i++)
            {
                for (int j = 0; j < 7; j++)
                {
                    if (DateRectangles[i][j].Drawn)
                    {
                        if (DateRectangles[i][j].Rect.Contains(e.Location))
                        {
                            if (HoverX != i || HoverY != j)
                            {
                                HoverX = i;
                                previousYearHovered = false;
                                nextYearHovered = false;
                                HoverY = j;
                                Invalidate();
                            }
                            return;
                        }
                    }
                }
            }
 
            if (PreviousYearRect.Contains(e.Location))
            {
                previousYearHovered = true;
                HoverX = -1;
                Invalidate();
                return;
            }
            if (PreviousMonthRect.Contains(e.Location))
            {
                previousMonthHovered = true;
                HoverX = -1;
                Invalidate();
                return;
            }
            if (NextMonthRect.Contains(e.Location))
            {
                nextMonthHovered = true;
                HoverX = -1;
                Invalidate();
                return;
            }
            if (NextYearRect.Contains(e.Location))
            {
                nextYearHovered = true;
                HoverX = -1;
                Invalidate();
                return;
            }
            if (HoverX >= 0 || previousYearHovered || previousMonthHovered || nextMonthHovered || nextYearHovered)
            {
                HoverX = -1;
                previousYearHovered = false;
                previousMonthHovered = false;
                nextMonthHovered = false;
                nextYearHovered = false;
                Invalidate();
            }
        }
 
        protected override void OnMouseUp(MouseEventArgs e)
        {
            if (HoverX >= 0)
            {
                SelectedX = HoverX;
                SelectedY = HoverY;
                CurrentDate = DateRectangles[SelectedX][SelectedY].Date;
                Invalidate();
                onDateChanged?.Invoke(CurrentDate);
                return;
            }
 
            if (PreviousYearRect.Contains(e.Location))
            {
                CurrentDate = FirstDayOfMonth(CurrentDate.AddYears(-1));
            }
 
            if (PreviousMonthRect.Contains(e.Location))
            {
                CurrentDate = FirstDayOfMonth(CurrentDate.AddMonths(-1));
            }
 
            if (NextMonthRect.Contains(e.Location))
            {
                CurrentDate = FirstDayOfMonth(CurrentDate.AddMonths(1));
            }
 
            if (NextYearRect.Contains(e.Location))
            {
                CurrentDate = FirstDayOfMonth(CurrentDate.AddYears(1));
            }
 
            CalculateRectangles();
            Invalidate();
            onDateChanged?.Invoke(CurrentDate);
            base.OnMouseUp(e);
        }
 
        protected override void OnMouseLeave(EventArgs e)
        {
            HoverX = -1;
            HoverY = -1;
            previousYearHovered = false;
            previousMonthHovered = false;
            nextMonthHovered = false;
            nextYearHovered = false;
            Invalidate();
            base.OnMouseLeave(e);
        }
 
        #endregion
 
        private void CalculateRectangles()
        {
            DateRectangles = new List<List<Util.HopeBase.DateRectHopeBase>>();
            for (int i = 0; i < 7; i++)
            {
                DateRectangles.Add(new List<Util.HopeBase.DateRectHopeBase>());
                for (int j = 0; j < 7; j++)
                {
                    DateRectangles[i].Add(new Util.HopeBase.DateRectHopeBase(new RectangleF(10 + (j * (Width - 20) / 7), WeekRect.Y + WeekRect.Height + (i * DateRectDefaultSize), DateRectDefaultSize, DateRectDefaultSize)));
                }
            }
            DateTime FirstDay = FirstDayOfMonth(CurrentDate);
            int temp = 0;
            for (int i = FirstDay.DayOfWeek == DayOfWeek.Sunday ? 6 : (int)FirstDay.DayOfWeek - 1; i > 0; i--, temp++)
            {
                DateRectangles[temp / 7][temp % 7].Drawn = false;
                DateRectangles[temp / 7][temp % 7].Date = FirstDay.AddDays(0 - i);
            }
            for (DateTime date = FirstDay; date <= LastDayOfMonth(CurrentDate); date = date.AddDays(1), temp++)
            {
                if (date.DayOfYear == CurrentDate.DayOfYear && date.Year == CurrentDate.Year)
                {
                    SelectedX = temp / 7;
                    SelectedY = temp % 7;
                }
                DateRectangles[temp / 7][temp % 7].Drawn = true;
                DateRectangles[temp / 7][temp % 7].Date = date;
            }
            DateTime LastDay = LastDayOfMonth(CurrentDate);
            for (int i = 0; temp < 42; i++, temp++)
            {
                DateRectangles[temp / 7][temp % 7].Drawn = false;
                DateRectangles[temp / 7][temp % 7].Date = LastDay.AddDays(i + 1);
            }
        }
 
        protected override void OnResize(EventArgs e)
        {
            Width = 250;
            Height = 270;
        }
 
        public HopeDatePicker()
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.ResizeRedraw | ControlStyles.OptimizedDoubleBuffer, true);
            DoubleBuffered = true;
            Width = 250;
            Height = 260;
 
            BackColor = Color.White;
 
            DateRectDefaultSize = (Width - 20) / 7;
            TopDateRect = new(20, 5, Width - 40, DateRectDefaultSize);
            WeekRect = new(0, TopDateRect.Y + TopDateRect.Height, DateRectDefaultSize, DateRectDefaultSize);
 
            PreviousYearRect = new(10, TopDateRect.Y, 20, DateRectDefaultSize);
            PreviousMonthRect = new(35, TopDateRect.Y + 1, 20, DateRectDefaultSize);
            NextMonthRect = new(Width - 55, TopDateRect.Y + 1, 20, DateRectDefaultSize);
            NextYearRect = new(Width - 30, TopDateRect.Y, 20, DateRectDefaultSize);
 
            CurrentDate = DateTime.Now;
 
            HoverX = -1;
            HoverY = -1;
            CalculateRectangles();
        }
 
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
 
            Graphics graphics = e.Graphics;
            graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
            graphics.SmoothingMode = SmoothingMode.HighQuality;
            graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
            graphics.Clear(Parent.BackColor);
 
            GraphicsPath bg = RoundRectangle.CreateRoundRect(1f, 1f, Width - 2, Height - 2, 3);
            graphics.FillPath(new SolidBrush(BackColor), bg);
            graphics.DrawPath(new(BorderColor), bg);
 
            graphics.DrawString(string.Format(_HeaderFormat, CurrentDate.Year, CurrentDate.Month), new Font("Segoe UI", 12f), new SolidBrush(HeaderTextColor), TopDateRect, HopeStringAlign.Center);
 
            graphics.DrawString("7", new Font("webdings", 12f), new SolidBrush(previousYearHovered ? PYHoverColor : PYColor), PreviousYearRect, HopeStringAlign.Center);
            graphics.DrawString("3", new Font("webdings", 12f), new SolidBrush(previousMonthHovered ? PMHoverColor : PMColor), PreviousMonthRect, HopeStringAlign.Center);
            graphics.DrawString("4", new Font("webdings", 12f), new SolidBrush(nextMonthHovered ? NMHoverColor : NMColor), NextMonthRect, HopeStringAlign.Center);
            graphics.DrawString("8", new Font("webdings", 12f), new SolidBrush(nextYearHovered ? NYHoverColor : NYColor), NextYearRect, HopeStringAlign.Center);
 
            string s = _DayNames;
            for (int i = 0; i < 7; i++)
            {
                graphics.DrawString(s[i].ToString(), new Font("Segoe UI", 10f), new SolidBrush(DaysTextColor), new RectangleF(10 + (i * (Width - 20) / 7), WeekRect.Y, WeekRect.Width, WeekRect.Height), HopeStringAlign.Center);
            }
 
            graphics.DrawLine(new(HeadLineColor, 0.5f), 10, WeekRect.Y + WeekRect.Height, Width - 10, WeekRect.Y + WeekRect.Height);
 
            //DateTime FirstDay = FirstDayOfMonth(CurrentDate);
            for (int i = 0; i < 42; i++)
            {
                Util.HopeBase.DateRectHopeBase tempDate = DateRectangles[i / 7][i % 7];
                SolidBrush brush = new(DayTextColorA);
 
                if (HoverX == i / 7 && HoverY == i % 7)
                {
                    RectangleF rect1 = tempDate.Rect;
                    GraphicsPath bg1 = RoundRectangle.CreateRoundRect(rect1.X + 2, rect1.Y + 2, rect1.Width - 4, rect1.Width - 4, 3);
                    graphics.FillPath(new SolidBrush(HoverColor), bg1);
                    //graphics.FillRectangle(new SolidBrush(HopeColors.ThreeLevelBorder), new RectangleF(rect1.X + 3, rect1.Y + 3, rect1.Width - 6, rect1.Width - 6));
                }
 
                if (tempDate.Date == DateTime.Today)
                {
                    brush = new(ValueTextColor);
                }
 
                if (tempDate.Date == Date)
                {
                    RectangleF rect1 = tempDate.Rect;
                    GraphicsPath bg1 = RoundRectangle.CreateRoundRect(rect1.X + 2, rect1.Y + 2, rect1.Width - 4, rect1.Width - 4, 3);
                    graphics.FillPath(new SolidBrush(SelectedBackColor), bg1);
 
                    //graphics.FillRectangle(new SolidBrush(HopeColors.PrimaryColor), new RectangleF(rect1.X+3,rect1.Y+3,rect1.Width-6,rect1.Width-6));
                    brush = new(SelectedTextColor);
                }
                graphics.DrawString(DateRectangles[i / 7][i % 7].Date.Day.ToString(), Font, DateRectangles[i / 7][i % 7].Drawn ? brush : new SolidBrush(DayTextColorB), DateRectangles[i / 7][i % 7].Rect, HopeStringAlign.Center);
            }
        }
 
        public static DateTime FirstDayOfMonth(DateTime value)
        {
            return new DateTime(value.Year, value.Month, 1);
        }
 
        public DateTime LastDayOfMonth(DateTime value)
        {
            return new DateTime(value.Year, value.Month, DateTime.DaysInMonth(value.Year, value.Month));
        }
    }
 
    #endregion
}