ningshuxia
2024-04-09 03c3b824872e688af2b42f226175fb0f6f907c1a
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
namespace ISupply.WinFrm.Client
{
    public partial class Main : DevExpress.XtraBars.FluentDesignSystem.FluentDesignForm
    {
        public Main()
        {
            InitializeComponent();
            this.IconOptions.Icon = ISupply.Icons.Resource.App;
 
            if (this.components == null)
                _app = new System.Windows.Forms.NotifyIcon();
            else
                _app = new System.Windows.Forms.NotifyIcon(this.components);
            _app.Text = "曲线管理";
            _app.Visible = true;
            _app.Click += (object sender, EventArgs e) =>
            {
                this.Visible = true;
                this.WindowState = FormWindowState.Maximized;
                _app.Visible = false;
            };
        }
 
        //icon
        readonly NotifyIcon _app = null;
 
 
        /*//禁止标题扩展
        protected override bool ExtendNavigationControlToFormTitle
        {
            get { return false; }
        }*/
 
        //开启界面双缓冲  解决窗体切换闪屏问题
        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000;
                return cp;
            }
        }
 
        /// <summary>
        /// 初始化
        /// </summary>
        internal void Initial()
        {
            var list = FuncCreater.GetFuncElements();
            if (list != null && list.Count > 0)
            {
                foreach (var element in list)
                {
                    var accEle = GetAccordionControlElement(element);
                    this.accordionControl.Elements.Add(accEle);
                }
            }
        }
 
        #region TabbedView
 
        //选中
        private void tabbedView1_DocumentSelected(object sender, DocumentEventArgs e)
        {
            if (e.Document == null)
                return;
            if (!(e.Document.Tag is WinFrm.SurfaceGuid surfaceGuid))
                return;
            foreach (var item in this.accordionControl.Elements)
            {
                if (SelectAccordionControlElement(item, surfaceGuid))
                    break;
            }
        }
        #endregion
 
        #region Page
 
        //是否存在Page
        private bool IsExistPage(WinFrm.SurfaceGuid sguid, bool isActivateDoc)
        {
            if (sguid == null)
                return false;
 
            if (this.tabbedView1.Documents != null && this.tabbedView1.Documents.Count > 0)
            {
                foreach (BaseDocument doc in this.tabbedView1.Documents)
                {
                    if (doc.Tag != null)
                    {
                        if (doc.Tag is WinFrm.SurfaceGuid)
                        {
                            if ((doc.Tag as WinFrm.SurfaceGuid).ToString() == sguid.ToString())
                            {
                                if (isActivateDoc)
                                    this.tabbedView1.Controller.Activate(doc);
                                return true;
                            }
                        }
                    }
                }
            }
 
            return false;
        }
 
        //更新数据
        private void RefreshPageData(WinFrm.SurfaceGuid sguid)
        {
            if (sguid == null)
                return;
 
            if (this.tabbedView1.Documents != null && this.tabbedView1.Documents.Count > 0)
            {
                var doc = this.tabbedView1.Documents.ToList().Find(x => x.Tag != null && x.Tag is WinFrm.SurfaceGuid && (x.Tag as WinFrm.SurfaceGuid).ToString() == sguid.ToString());
                if (doc != null)
                {
                    if (doc.Control is WinFrm.DocumentPage page)
                    {
                        page.RefreshDataSource();
                    }
                    return;
                }
            }
        }
 
        //更新PageTitle
        private void UpdatePageTitle(WinFrm.SurfaceGuid sguid, WinFrm.PageTitle title)
        {
            if (sguid == null || title == null)
                return;
 
 
            if (this.tabbedView1.Documents != null && this.tabbedView1.Documents.Count > 0)
            {
                var doc = this.tabbedView1.Documents.ToList().Find(x => x.Tag != null && x.Tag is WinFrm.SurfaceGuid && (x.Tag as WinFrm.SurfaceGuid).ToString() == sguid.ToString());
                if (doc != null)
                {
                    doc.Caption = title.Caption;
                    doc.ImageOptions.Image = title.HeaderImage;
                    doc.ImageOptions.SvgImage = title.HeaderSvgImage;
                    doc.ImageOptions.SvgImageSize = title.SvgImageSize;
                    if (doc.Control is WinFrm.DocumentPage page)
                    {
                        page.PageTitle = title;
                    }
                    return;
                }
            }
        }
 
        //创建Page
        private bool CreatePage(WinFrm.DocumentPage page, WinFrm.SurfaceGuid sguid)
        {
            if (page == null || sguid == null)
                return false;
            page.SurfaceGuid = sguid;
            page.Dock = DockStyle.Fill;
            page.IsExistPageEvent += IsExistPage;
            page.CreatePageEvent += CreatePage;
            page.RefreshPageDataEvent += RefreshPageData;
            page.UpdatePageTitleEvent += UpdatePageTitle;
            page.ClosePageEvent += ClosePage;
            page.ResetAllPagesEvent += ResetAllPages;
            page.RegistEvents();
 
            this.tabbedView1.BeginUpdate();
            var doc = this.tabbedView1.AddDocument(page);
            doc.Footer = Directory.GetCurrentDirectory();
            if (page.PageTitle != null)
            {
                doc.Caption = page.PageTitle.Caption;
                doc.ImageOptions.Image = page.PageTitle.HeaderImage;
                doc.ImageOptions.SvgImage = page.PageTitle.HeaderSvgImage;
                doc.ImageOptions.SvgImageSize = page.PageTitle.SvgImageSize;
            }
            else
            {
                doc.Caption = page.SurfaceGuid?.Function;
            }
            doc.Tag = sguid;
            this.tabbedView1.EndUpdate();
            this.tabbedView1.Controller.Activate(doc);
            this.tabbedView1.ActivateDocument(doc.Control);
 
            page.InitialDataSource();
            return true;
        }
 
 
 
 
        //关闭Page
        private void ClosePage(WinFrm.SurfaceGuid sguid)
        {
            if (this.tabbedView1.Documents != null && this.tabbedView1.Documents.Count > 0)
            {
                var doc = this.tabbedView1.Documents.ToList().Find(x => x.Tag != null && x.Tag is WinFrm.SurfaceGuid && (x.Tag as WinFrm.SurfaceGuid).ToString() == sguid.ToString());
                if (doc != null)
                {
                    var page = doc.Control as WinFrm.DocumentPage;
                    this.tabbedView1.Controller.Close(doc);//会触发正在关闭和关闭事件
                }
            }
        }
 
        //查找page
        private WinFrm.DocumentPage FindPage(WinFrm.SurfaceGuid sguid, bool isActiveDoc)
        {
            if (sguid == null)
                return default;
            var sguid_msg = sguid.ToString();
 
            if (this.tabbedView1.Documents != null && this.tabbedView1.Documents.Count > 0)
            {
                var doc = this.tabbedView1.Documents.ToList().Find(x => x.Tag != null && x.Tag.ToString() == sguid_msg);
                if (doc != null)
                {
                    if (isActiveDoc)
                        this.tabbedView1.Controller.Activate(doc);
                    if (doc.Control is WinFrm.DocumentPage page)
                    {
                        return page;
                    }
                    return null;
                }
            }
            return null;
        }
 
        //重置所有页面
        private void ResetAllPages()
        {
            this.tabbedView1.Controller.CloseAll();
        }
 
        #endregion
 
        #region AccordionControl
 
        //功能点击事件 
        private void accordionControl_ElementClick(object sender, DevExpress.XtraBars.Navigation.ElementClickEventArgs e)
        {
            if (e.Element.Tag == null)
                return;
            var ele = e.Element.Tag as FuncElement;
            if (ele.SurfaceGuid != null)
            {
                CreateFuncPage(ele);
            }
        }
 
 
        //获取模块元素
        private DevExpress.XtraBars.Navigation.AccordionControlElement GetAccordionControlElement(FuncElement ele)
        {
            var accEle = new DevExpress.XtraBars.Navigation.AccordionControlElement();
            if (ele.SurfaceGuid != null)
            {
                accEle.Name = ele.SurfaceGuid.ToString();
            }
            else
            {
                accEle.Name = ele.Id;
            }
            accEle.Text = ele.Name;
            accEle.Image = ele.Image;
            accEle.Expanded = ele.Expand;
            accEle.AllowGlyphSkinning = DevExpress.Utils.DefaultBoolean.False;
            accEle.Tag = ele;
            accEle.Style = DevExpress.XtraBars.Navigation.ElementStyle.Item;
            if (ele.FuncElements != null && ele.FuncElements.Count > 0)
            {
                foreach (var item in ele.FuncElements)
                {
                    accEle.Elements.Add(GetAccordionControlElement(item));
                }
                accEle.Style = DevExpress.XtraBars.Navigation.ElementStyle.Group;
            }
            return accEle;
        }
 
        //选择功能元素
        private bool SelectAccordionControlElement(DevExpress.XtraBars.Navigation.AccordionControlElement element, WinFrm.SurfaceGuid surfaceGuid)
        {
            if (element == null)
                return false;
            if (surfaceGuid == null)
                return false;
            if (element.Name == surfaceGuid.ToString())
            {
                this.accordionControl.SelectedElement = element;
                if (element.OwnerElement != null)
                {
                    element.OwnerElement.Expanded = true;
                    this.accordionControl.Refresh();
                }
                return true;
            }
            else
            {
                if (element.Elements != null && element.Elements.Count > 0)
                {
                    foreach (var item in element.Elements)
                    {
                        SelectAccordionControlElement(item, surfaceGuid);
                    }
                }
            }
            return false;
        }
 
 
        //创建功能页
        private void CreateFuncPage(FuncElement obj)
        {
            try
            {
                var control = (Control)Assembly.LoadFrom(obj.DllFileName).CreateInstance(obj.ControlFullName);
                if (control is WinFrm.DocumentPage page)
                {
                    if (IsExistPage(obj.SurfaceGuid, true))
                        return;
                    CreatePage(page, obj.SurfaceGuid);
                }
            }
            catch (Exception)
            {
                XtraMessageBox.Show($"{obj.Name} 配置错误!");
            }
        }
 
        #endregion
 
 
 
    }
}