tangxu
2024-10-22 4d9fe5ed98ceb6b8fe9dc52ebfb80860ad1aee99
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
// COPYRIGHT (C) Tom. ALL RIGHTS RESERVED.
// THE AntdUI PROJECT IS AN WINFORM LIBRARY LICENSED UNDER THE Apache-2.0 License.
// LICENSED UNDER THE Apache License, VERSION 2.0 (THE "License")
// YOU MAY NOT USE THIS FILE EXCEPT IN COMPLIANCE WITH THE License.
// YOU MAY OBTAIN A COPY OF THE LICENSE AT
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING, SOFTWARE
// DISTRIBUTED UNDER THE LICENSE IS DISTRIBUTED ON AN "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
// SEE THE LICENSE FOR THE SPECIFIC LANGUAGE GOVERNING PERMISSIONS AND
// LIMITATIONS UNDER THE License.
// GITEE: https://gitee.com/antdui/AntdUI
// GITHUB: https://github.com/AntdUI/AntdUI
// CSDN: https://blog.csdn.net/v_132
// QQ: 17379620
 
using System;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;
 
namespace AntdUI
{
    public static partial class Helper
    {
        public static float Calculate(this float val, float add)
        {
            return (float)Math.Round(val + add, 3);
        }
 
        /// <summary>
        /// SizeF转Size(向上取整)
        /// </summary>
        /// <param name="size">SizeF</param>
        public static Size Size(this SizeF size)
        {
            return new Size((int)Math.Ceiling(size.Width), (int)Math.Ceiling(size.Height));
        }
 
        /// <summary>
        /// SizeF转Size(向上取整)
        /// </summary>
        /// <param name="size">SizeF</param>
        public static Size Size(this SizeF size, float p)
        {
            return new Size((int)Math.Ceiling(size.Width + p), (int)Math.Ceiling(size.Height + p));
        }
 
        public static Color ToColor(float alpha, Color color)
        {
            return ToColor((int)alpha, color);
        }
 
        public static Color ToColorN(float val, Color color)
        {
            return ToColor((int)(val * color.A), color);
        }
 
        public static Color ToColor(int alpha, Color color)
        {
            if (alpha > 255) alpha = 255;
            else if (alpha < 0) alpha = 0;
            return Color.FromArgb(alpha, color);
        }
 
        public static Form? FindPARENT(this Control? control)
        {
            if (control == null) return null;
            if (control is DoubleBufferForm formd)
            {
                if (control.Tag is Form form) return form;
                else if (control.Parent != null) return FindPARENT(control.Parent);
                return formd;
            }
            else if (control is Form form) return form;
            else if (control.Parent != null) return FindPARENT(control.Parent);
            return null;
        }
        public static bool SetTopMost(this Control? control, IntPtr hand)
        {
            var form = control.FindPARENT();
            if (form != null && form.TopMost)
            {
                SetTopMost(hand);
                return true;
            }
            return false;
        }
 
        public static void SetTopMost(IntPtr hand)
        {
            Vanara.PInvoke.User32.SetWindowPos(hand, new IntPtr(-1), 0, 0, 0, 0, Vanara.PInvoke.User32.SetWindowPosFlags.SWP_NOACTIVATE);
        }
 
        public static bool Wait(this System.Threading.WaitHandle handle)
        {
            try
            {
                handle.WaitOne();
                return false;
            }
            catch
            {
                return true;
            }
        }
 
        public static bool Wait(this System.Threading.CancellationTokenSource? token)
        {
            try
            {
                if (token == null || token.IsCancellationRequested) return true;
                return false;
            }
            catch
            {
                return true;
            }
        }
 
        public static bool Wait(this System.Threading.CancellationTokenSource? token, Control control)
        {
            try
            {
                if (token == null || token.IsCancellationRequested || control.IsDisposed) return true;
                return false;
            }
            catch
            {
                return true;
            }
        }
 
        public static bool ListExceed(this IList? list, int index)
        {
            if (list == null || list.Count <= index || index < 0) return true;
            return false;
        }
 
        public static bool DateExceed(DateTime date, DateTime? min, DateTime? max)
        {
            if (min.HasValue && min.Value >= date) return false;
            if (max.HasValue && max.Value <= date) return false;
            return true;
        }
 
        public static bool DateExceedRelax(DateTime date, DateTime? min, DateTime? max)
        {
            if (min.HasValue && min.Value > date) return false;
            if (max.HasValue && max.Value < date) return false;
            return true;
        }
 
        #region 剪贴板
 
        public static string? ClipboardGetText(this Control control)
        {
            if (control.InvokeRequired)
            {
                string? r = null;
                control.Invoke(new Action(() =>
                {
                    r = ClipboardGetText();
                }));
                return r;
            }
            return ClipboardGetText();
        }
        public static string? ClipboardGetText()
        {
            try
            {
                return Clipboard.GetText();
            }
            catch
            {
                return Win32.GetClipBoardText();
            }
        }
        public static bool ClipboardSetText(this Control control, string? text)
        {
            if (control.InvokeRequired)
            {
                bool r = false;
                control.Invoke(new Action(() =>
                {
                    r = ClipboardSetText(text);
                }));
                return r;
            }
            return ClipboardSetText(text);
        }
        public static bool ClipboardSetText(string? text)
        {
            try
            {
                if (text == null) Clipboard.Clear();
                else Clipboard.SetText(text);
                return true;
            }
            catch
            {
                if (Win32.SetClipBoardText(text)) return true;
            }
            return false;
        }
 
        #endregion
    }
 
    internal class AnchorDock
    {
        public AnchorDock(Control control)
        {
            Dock = control.Dock;
            Anchor = control.Anchor;
            control.Dock = DockStyle.None;
            control.Anchor = AnchorStyles.Left | AnchorStyles.Top;
        }
        public DockStyle Dock { get; set; }
        public AnchorStyles Anchor { get; set; }
    }
 
    public class RectTextLR
    {
        public Rectangle text { get; set; }
        public Rectangle l { get; set; }
        public Rectangle r { get; set; }
    }
}