黄学彪
2020-12-16 0d9f64668fd7350d6a21fd157e32009a96d98134
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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
using System;
using System.Collections.Generic;
using System.Text;
 
namespace Shared.Phone
{
    /// <summary>
    /// Botton的最初原型(不建议修改和直接使用):标准字体和颜色,文字向左靠齐,14号字
    /// </summary>
    public class ButtonBase : Button
    {
        #region ■ 变量声明___________________________
 
        /// <summary>
        /// 设置能否触点击事件
        /// </summary>
        private bool m_CanClick = true;
        /// <summary>
        /// 设置能否触点击事件
        /// </summary>
        public bool CanClick
        {
            get { return m_CanClick; }
            set
            {
                if (m_CanClick == value) { return; }
 
                m_CanClick = value;
                //能够点击,则显示没有点击过的状态
                if (m_CanClick == true) { this.SetNotClickStatu(); }
                //不能点击,则显示已经点击了的状态
                else { this.SetClickStatu(); }
            }
        }
        /// <summary>
        /// 圆角度
        /// </summary>
        public int RadiusEx
        {
            set { this.Radius = (uint)Application.GetRealHeight(value); }
        }
        /// <summary>
        /// 声明此变量,旨在子线程也能够去获取一个控件的主键
        /// </summary>
        public string MainKey = string.Empty;
 
        /// <summary>
        /// 点击的坐标
        /// </summary>
        private System.Drawing.Point downPoint = new System.Drawing.Point();
 
        /// <summary>
        /// 控件的点击事件(自定义封装事件,此事件被认可为执行按钮按下事件,受CanClick属性控制)
        /// </summary>
        public Action<Button, MouseEventArgs> ButtonClickEvent = null;
        /// <summary>
        /// 控件的按下事件(自定义封装事件,此事件被认可为执行按钮按下事件,受CanClick属性控制)
        /// </summary>
        public Action<Button, MouseEventArgs> ButtonDownClickEvent = null;
        /// <summary>
        /// 控件触发移动的事件(自身拥有算法,当移动多少像素后,触发事件,注意,该事件可能会频繁的触发)
        /// </summary>
        public Action ButtonHappenMoveEvent = null;
 
        #endregion
 
        #region ■ 重写彪哥的属性_____________________
 
        /// <summary>
        /// 重写Text属性
        /// </summary>
        public new string Text
        {
            //先这么弄先吧
            get { return base.Text == null ? string.Empty : base.Text; }
            set
            {
                base.Text = value == null ? string.Empty : value;
            }
        }
 
        #endregion
 
        #region ■ 初始化_____________________________
 
        /// <summary>
        /// Botton的最初原型(不建议修改和直接使用):标准字体和颜色,文字向左靠齐,14号字
        /// </summary>
        public ButtonBase()
        {
            //测试,全体按钮为14号字
            this.TextSize = 14;
 
            this.TextColor = UserCenterColor.Current.TextColor1;
            this.TextAlignment = TextAlignment.CenterLeft;
 
            //点击事件
            this.MouseUpEventHandler += ButtonBase_MouseUpEventHandler;
            //按下事件
            this.MouseDownEventHandler += ButtonBase_MouseDownEventHandler;
            //移动事件
            this.MouseMoveEventHandler += ButtonBase_MouseMoveEventHandler;
        }
 
        /// <summary>
        /// 初始化控件大小(不以平均值进行真实数值计算)
        /// </summary>
        /// <param name="i_Width">宽度</param>
        /// <param name="i_Height">高度</param>
        /// <param name="real">是否计算真实值</param>
        public void InitSize(int i_Width, int i_Height, bool real = true)
        {
            if (real == true)
            {
                i_Width = Application.GetRealWidth(i_Width);
                i_Height = Application.GetRealHeight(i_Height);
            }
            this.Height = i_Height;
            this.Width = i_Width;
        }
 
        /// <summary>
        /// 初始化控件大小(不以平均值进行真实数值计算)
        /// </summary>
        /// <param name="i_Width">宽度</param>
        /// <param name="real">是否计算真实值</param>
        public void InitSize(int i_Width, bool real = false)
        {
            if (real == true)
            {
                i_Width = Application.GetRealWidth(i_Width);
            }
 
            this.Height = HdlControlResourse.NormalControlHeight;
            this.Width = i_Width;
        }
 
        /// <summary>
        /// 初始化图标控件大小(以平均值进行真实数值计算)
        /// </summary>
        /// <param name="i_Width">宽度</param>
        /// <param name="i_Height">高度</param>
        /// <param name="real">是否计算真实值</param>
        public void InitIconSize(int i_Width, int i_Height, bool real = true)
        {
            if (real == true)
            {
                i_Width = this.GetPictrueRealSize(i_Width);
                i_Height = this.GetPictrueRealSize(i_Height);
            }
 
            this.Height = i_Height;
            this.Width = i_Width;
        }
 
        /// <summary>
        /// 初始化图片控件大小
        /// </summary>
        /// <param name="i_Width">宽度</param>
        /// <param name="i_Height">高度</param>
        /// <param name="real">是否计算真实值</param>
        public void InitPictrueSize(int i_Width, int i_Height, bool real = true)
        {
            if (real == true)
            {
                i_Width = HdlControlLogic.Current.GetPictrueRealSize(i_Width);
                i_Height = HdlControlLogic.Current.GetPictrueRealSize(i_Height);
            }
 
            this.Height = i_Height;
            this.Width = i_Width;
        }
 
        #endregion
 
        #region ■ 点击事件___________________________
 
        /// <summary>
        /// 点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonBase_MouseUpEventHandler(object sender, MouseEventArgs e)
        {
            if (ButtonClickEvent == null)
            {
                this.MouseUpEventHandler -= ButtonBase_MouseUpEventHandler;
                return;
            }
            //2020.05.14追加IsFormAdding:界面还在加载中,不能再点击
            if (CanClick == true && HdlControlResourse.IsFormAdding == false)
            {
                //Log出力
                this.WriteLog(0);
 
                try
                {
                    this.ButtonClickEvent(this, e);
                }
                catch (Exception ex)
                {
                    //出现未知错误
                    var alert = new ShowMsgControl(ShowMsgType.Error, Language.StringByID(R.MyInternationalizationString.uUnKnownError));
                    alert.Show();
                    //Log出力
                    HdlLogLogic.Current.WriteLog(ex);
                }
            }
        }
 
        #endregion
 
        #region ■ 按下事件___________________________
 
        /// <summary>
        /// 按下事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonBase_MouseDownEventHandler(object sender, MouseEventArgs e)
        {
            //记录起当前点击的坐标
            downPoint.X = (int)e.X;
            downPoint.Y = (int)e.Y;
 
            if (CanClick == false || this.ButtonDownClickEvent == null)
            {
                //不能点击
                return;
            }
 
            try
            {
                this.ButtonDownClickEvent(this, e);
            }
            catch (Exception ex)
            {
                //出现未知错误
                var alert = new ShowMsgControl(ShowMsgType.Error, Language.StringByID(R.MyInternationalizationString.uUnKnownError));
                alert.Show();
                //Log出力
                HdlLogLogic.Current.WriteLog(ex);
            }
        }
 
        #endregion
 
        #region ■ 移动事件___________________________
 
        /// <summary>
        /// 移动事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ButtonBase_MouseMoveEventHandler(object sender, MouseEventArgs e)
        {
            if (this.ButtonHappenMoveEvent == null)
            {
                this.MouseMoveEventHandler -= ButtonBase_MouseMoveEventHandler;
                return;
            }
            int value = (int)e.X - this.downPoint.X;
            if (value >= 30 || value <= -30)
            {
                //触发移动事件
                this.ButtonHappenMoveEvent();
                return;
            }
            value = (int)e.Y - this.downPoint.Y;
            if (value >= 30 || value <= -30)
            {
                //触发移动事件
                this.ButtonHappenMoveEvent();
                return;
            }
        }
 
        #endregion
 
        #region ■ 设置点击状态_______________________
 
        /// <summary>
        /// 设置点击后的状态(此方法由各自控件进行重载)
        /// </summary>
        public virtual void SetClickStatu()
        {
        }
 
        /// <summary>
        /// 设置非点击后的状态(此方法由各自控件进行重载)
        /// </summary>
        public virtual void SetNotClickStatu()
        {
        }
 
        #endregion
 
        #region ■ 一般方法___________________________
 
        /// <summary>
        /// 控件摧毁
        /// </summary>
        public override void RemoveFromParent()
        {
            ButtonClickEvent = null;
            ButtonDownClickEvent = null;
 
            if (this.Parent != null)
            {
                base.RemoveFromParent();
            }
        }
 
        /// <summary>
        /// 根据文本,计算它实际的宽度
        /// </summary>
        /// <returns></returns>
        public int GetRealWidthByText()
        {
            if (string.IsNullOrEmpty(this.Text) == true) { return Application.GetRealWidth(25); }
#if Android
            //需要增加一个误差值
            return this.GetTextWidth() + Application.GetRealWidth(12);
#endif
#if iOS
            //需要增加一个误差值
            return this.GetTextWidth() + Application.GetRealWidth(25);
#endif
        }
 
        /// <summary>
        /// 计算图片的真实高宽度
        /// </summary>
        /// <param name="i_size"></param>
        /// <returns></returns>
        public int GetPictrueRealSize(int i_size)
        {
            return HdlControlLogic.Current.GetPictrueRealSize(i_size);
        }
 
        #endregion
 
        #region ■ Log出力____________________________
 
        /// <summary>
        /// 该控件所属的界面名字
        /// </summary>
        public string formName = null;
        /// <summary>
        /// 控件名字
        /// </summary>
        private string controlName = null;
 
        /// <summary>
        /// Log出力
        /// </summary>
        private void WriteLog(int div)
        {
            if (formName == null)
            {
                formName = string.Empty;
                View myView = this.Parent;
                for (; ; )
                {
                    if (myView == null)
                    {
                        break;
                    }
                    else if (myView is CommonFormBase)
                    {
                        //这个控件所属的界面
                        formName = ((CommonFormBase)myView).FormID;
                        break;
                    }
                    myView = myView.Parent;
                }
                if (string.IsNullOrEmpty(this.Text) == false)
                {
                    //这个控件的文本
                    controlName = this.Text;
                }
                else
                {
                    //如果没有文本的话,它应该是一张图片
                    controlName = this.UnSelectedImagePath;
                }
            }
            if (div == 0)
            {
                HdlLogLogic.Current.WriteLog(1, formName + "的[" + controlName + "]按键被点击");
            }
            else
            {
                HdlLogLogic.Current.WriteLog(1, formName + "的[" + controlName + "]按键被长按");
            }
        }
 
        #endregion
    }
}