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
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#region
 
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using CSFrameworkV5.Business;
using CSFrameworkV5.Common;
using CSFrameworkV5.Core;
using CSFrameworkV5.Language;
using CSFrameworkV5.Library.CommonClass;
using CSFrameworkV5.Library.Entry;
using DevExpress.XtraBars;
using DevExpress.XtraEditors;
 
#endregion
 
namespace CSFrameworkV5.Library
{
    /// <summary>
    ///     所有MDI子窗体基类.
    /// </summary>
    public partial class frmBaseChild : frmBase, IMdiChildForm,
        IPurviewControllable, ISystemButtons
    {
        /// <summary>
        ///     打开当前窗体传入的预设参数, 界面需要根据此参数做特殊处理
        /// </summary>
        protected object _CurrentParameter;
 
        public frmBaseChild()
        {
            InitializeComponent();
        }
 
        /// <summary>
        ///     系统是否只保留一个子窗体实例
        /// </summary>
        public bool AllowMultiInstatnce
        {
            get => _AllowMultiInstatnce;
            set => _AllowMultiInstatnce = value;
        }
 
        /// <summary>
        ///     给窗体设置参数,SetParameter方法在在Form.Load事件之前运行的, 因此需要在Form.Load事件内处理传入的参数。
        ///     参考:MdiTools.OpenChildForm(IMdiForm mdi, Type formType, ToolStripMenuItem sender, object param)方法
        /// </summary>
        /// <param name="param"></param>
        public virtual void SetParameter(object param)
        {
            _CurrentParameter = param;
        }
 
        /// <summary>
        ///     清空容器内输入框.
        /// </summary>
        public void ClearContainerEditorText(Control container)
        {
            for (var i = 0; i < container.Controls.Count; i++)
                if (container.Controls[i] is TextEdit)
                    ((TextEdit)container.Controls[i]).EditValue = null;
                else if (container.Controls[i] is TextBoxBase)
                    ((TextBoxBase)container.Controls[i]).Clear();
        }
 
        //当子窗体获得焦点时在主界面注册本窗体的按钮。
        //通过Form Activated事件可以看到主窗体的ToolBar状态变化。
        private void frmBaseChild_Activated(object sender, EventArgs e)
        {
            ToolbarRegister.RegisteButton(Buttons.ToList());
 
            NotifyObserver(); //通过其它观察者
 
            //显示系统操作页面(数据操作按钮页面) //new 20200202
            (MdiParent as frmMain1).ShowDataOperatePage();
        }
 
        private void frmBaseChild_FormClosed(object sender,
            FormClosedEventArgs e)
        {
            NotifyObserver(); //当关闭窗体,要通知所有子窗体观察者
        }
 
        private void frmBaseChild_FormClosing(object sender,
            FormClosingEventArgs e)
        {
            IsClosing = true; //正在关闭状态
        }
 
        //通知观察者进行更新
        private void NotifyObserver()
        {
            foreach (IObserver o in _observers)
                if (o != null)
                    o.Notify();
        }
 
        #region 成员变量定义
 
        /// <summary>
        ///     父窗体的Toolbar组件
        /// </summary>
        protected IToolbarRegister _ToolbarRegister;
 
        /// <summary>
        ///     初始化子窗体的按钮数组
        /// </summary>
        protected IButtonList _buttons = new ButtonList();
 
        /// <summary>
        ///     子窗体的观察者
        /// </summary>
        protected IList _observers = new ArrayList();
 
        /// <summary>
        ///     子窗体的系统按钮
        /// </summary>
        protected IButtonInfo[] _SystemButtons = null;
 
        /// <summary>
        ///     窗体是否正在关闭状态
        /// </summary>
        protected bool _IsClosing;
 
        /// <summary>
        ///     否只保留一个子窗体实例
        /// </summary>
        protected bool _AllowMultiInstatnce;
 
        /// <summary>
        ///     窗体的可用权限
        /// </summary>
        protected int _FormAuthorities;
 
        /// <summary>
        ///     打开窗体的菜单名
        /// </summary>
        protected string _FormMenuName = "";
 
        #endregion
 
        #region IPurviewControllable 接口实现
 
        /// <summary>
        ///     窗体可用权限.为2^n方:1,2,4,8,16.....n^2
        ///     打开窗体时从数据库获取权限值保存在该变量
        /// </summary>
        public int FormAuthorities
        {
            get => _FormAuthorities;
            set => _FormAuthorities = value;
        }
 
        /// <summary>
        ///     打开窗体的菜单名
        /// </summary>
        public string FormMenuName
        {
            get => _FormMenuName;
            set => _FormMenuName = value;
        }
 
        /// <summary>
        ///     派生类通过重写该虚方法自定义每个按钮可用状态
        /// </summary>
        public virtual bool ButtonAuthorized(int authorityValue)
        {
            if (Loginer.CurrentUser.IsAdmin()) return true;
 
            return (authorityValue & FormAuthorities) == authorityValue;
        }
 
        /// <summary>
        ///     检查当前用户是否拥有本窗体的某个权限
        /// </summary>
        /// <param name="authorityValue">需要检查的权限值,ButtonAuthority类定义功能点</param>
        /// <returns></returns>
        public bool HasPurview(int value)
        {
            return (value & _FormAuthorities) == value;
        }
 
        #endregion
 
        #region IMdiChildForm 接口实现
 
        /// <summary>
        ///     主窗体的Toolbar按钮注册器
        /// </summary>
        public IToolbarRegister ToolbarRegister
        {
            get => _ToolbarRegister;
            set => _ToolbarRegister = value;
        }
 
        public virtual void RegisterToolBar(IToolbarRegister toolBarRegister)
        {
            //this.Buttons是当前窗体的按钮数组。
            toolBarRegister.RegisteButton(Buttons.ToList());
        }
 
        /// <summary>
        ///     当前窗体是否正在关闭状态
        /// </summary>
        public bool IsClosing
        {
            get => _IsClosing;
            set => _IsClosing = value;
        }
 
        /// <summary>
        ///     注册子窗体观察者
        /// </summary>
        public void RegisterObserver(IObserver[] observers)
        {
            foreach (var o in observers) _observers.Add(o);
        }
 
        /// <summary>
        ///     子窗体的按钮数组
        /// </summary>
        public IButtonList Buttons => _buttons;
 
        /// <summary>
        ///     模板方法.初始化本窗体的按钮.
        /// </summary>
        public virtual void InitButtons()
        {
            //关闭按钮
            _buttons.AddButton(ToolbarRegister.CreateButton("btnClose",
                LanLib.Get("关闭"), ToolBarGroup.关闭窗体,
                Globals.LoadBitmap("32_Exit.png"), new Size(57, 28), true, true,
                DoClose));
 
            UpdateButtonCaption();
        }
 
        /// <summary>
        ///     系统按钮列表。注:子窗体享用系统按钮,如帮助/关闭窗体常用功能。
        /// </summary>
        public virtual IButtonInfo[] GetSystemButtons()
        {
            return _SystemButtons;
        }
 
        #region 处理快捷方式按钮
 
        /// <summary>
        ///     更新快捷方式按钮的标题
        /// </summary>
        public virtual void UpdateButtonCaption()
        {
            if (string.IsNullOrEmpty(FormButtonName))
            {
                var btn =
                    (BarItem)(MdiParent as IMdiForm).GetToolBarButton(
                        ButtonNameList.ribbon_btnShortCut);
                if (btn != null) btn.Caption = LanLib.Get("设为常用");
 
                return;
            }
 
            var FormFullName = GetType().FullName;
            var MenuName = FormMenuName;
            BarItem button = null;
 
            if (IsExistsShortCutButton(FormFullName, MenuName))
            {
                _ShortCutButtonCaption = LanLib.Get("取消常用");
                button =
                    (BarItem)(MdiParent as IMdiForm).GetToolBarButton(
                        ButtonNameList.ribbon_btnShortCut);
                if (button != null) button.Caption = LanLib.Get("取消常用");
            }
            else
            {
                _ShortCutButtonCaption = LanLib.Get("设为常用");
                button =
                    (BarItem)(MdiParent as IMdiForm).GetToolBarButton(
                        ButtonNameList.ribbon_btnShortCut);
                if (button != null) button.Caption = LanLib.Get("设为常用");
            }
 
            if (string.IsNullOrEmpty(MenuName) ||
                string.IsNullOrEmpty(FormFullName))
            {
                button =
                    (BarItem)(MdiParent as IMdiForm).GetToolBarButton(
                        ButtonNameList.ribbon_btnShortCut);
                if (button != null) button.Enabled = false;
            }
        }
 
        private static bool IsExistsShortCutButton(string FormFullName,
            string MenuName)
        {
            var exists =
                new bllUserCustomAction().Exist(FormFullName, MenuName);
            return exists;
        }
 
        private bool _ButtonNameReady = false;
        private string _FormButtonName;
 
        /// <summary>
        ///     获取当前窗体对应的按钮
        /// </summary>
        public string FormButtonName
        {
            get
            {
                if (!_ButtonNameReady && string.IsNullOrEmpty(_FormButtonName))
                {
                    var FormFullName = GetType().FullName;
                    var MenuName = FormMenuName;
                    _FormButtonName =
                        (MdiTools.MainForm as IMdiForm)
                        .GetModuleMainFormButtonNameByMenuName(FormFullName,
                            MenuName);
                }
 
                return _FormButtonName;
            }
        }
 
        protected string _ShortCutButtonCaption = "";
 
        /// <summary>
        ///     设为/取消 常用功能
        /// </summary>
        /// <param name="sender"></param>
        public virtual void DoCreateShortCut()
        {
            var txt = _ShortCutButtonCaption;
            var FormFullName = GetType().FullName;
            var MenuName = FormMenuName;
 
            var Success = false;
            if (txt == LanLib.Get("设为常用"))
            {
                if (!Msg.AskQuestion("是否要建立 <" + Text + "> 窗体的快捷方式?")) return;
 
                //数据库添加
                Success = new bllUserCustomAction().InsertData(FormFullName,
                    MenuName, FormButtonName);
            }
 
            if (txt == LanLib.Get("取消常用"))
            {
                if (!Msg.AskQuestion("是否要取消 <" + Text + "> 窗体的快捷方式?")) return;
 
                //数据库删除
                Success =
                    new bllUserCustomAction().Delete(FormFullName, MenuName);
            }
 
            if (Success)
            {
                (MdiTools.MainForm as IMdiForm).UpdateModuleButtonLocal(
                    Globals.DEF_CUSTOM_MODULE_MAIN);
                UpdateButtonCaption();
                if (Success) Msg.ShowInformation(LanLib.Get("設置快捷按钮操作成功!"));
            }
        }
 
        #endregion
 
        public virtual void DoHelp(IButtonInfo button) //打开帮助
        {
            DoHelp();
        }
 
        public virtual void DoHelp()
        {
            HelpDoc.HelpAllFile(HelpDoc.CSHelp);
            //Msg.ShowInformation("已打开帮助文档,如派生的窗体需要打开指定帮助文件,请重写DoHelp方法。");
        }
 
        public virtual void DoClose(IButtonInfo sender)
        {
            Close();
        }
 
        public virtual void DoSetLanguageCHS(IButtonInfo sender)
        {
            if (LanLib.Current != LanguageType.CHS)
            {
                LanLib.Current = LanguageType.CHS;
                SetLanguageAllForm();
            }
        }
 
        public virtual void DoSetLanguageCHT(IButtonInfo sender)
        {
            if (LanLib.Current != LanguageType.CHT)
            {
                LanLib.Current = LanguageType.CHT;
                SetLanguageAllForm();
            }
        }
 
        public virtual void DoSetLanguageENG(IButtonInfo sender)
        {
            if (LanLib.Current != LanguageType.ENG)
            {
                LanLib.Current = LanguageType.ENG;
                SetLanguageAllForm();
            }
        }
 
        private void SetLanguageAllForm()
        {
            //设置主窗体的语言
            (MdiTools.MainForm as ILanguageSupport).SetLanguage();
 
            foreach (Form form in Application.OpenForms)
                if (form is ILanguageSupport && form != MdiTools.MainForm)
                    (form as ILanguageSupport).SetLanguage();
        }
 
        public override void SetLanguage()
        {
            base.SetLanguage();
            SetDynimcButtonLanguage();
 
            Buttons.GetButtonByName("btnHelp").Caption = LanLib.Get("帮助");
            Buttons.GetButtonByName("btnClose").Caption = LanLib.Get("关闭");
            Buttons.GetButtonByName("btnLanguage").Caption = LanLib.Get("选择语言");
 
            UpdateButtonCaption();
        }
 
        private IDictionary<string, string> DefaultButtonCaption =
            new Dictionary<string, string>();
 
        private void SetDynimcButtonLanguage()
        {
            foreach (var btn in _buttons.ToArray())
            {
                if (LanLib.Current == LanLib.DefautLanguage)
                    if (!DefaultButtonCaption.ContainsKey(btn.Name))
                        DefaultButtonCaption.Add(btn.Name, btn.Caption);
 
                //默认标题作为ObjectID,获取对应的语言
                if (DefaultButtonCaption.ContainsKey(btn.Name))
                    btn.Caption = LanLib.Get(DefaultButtonCaption[btn.Name]);
            }
        }
 
        #endregion
    }
}