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
#region
 
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using CSFrameworkV5.Common;
using CSFrameworkV5.Core;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
 
///*************************************************************************/
///*
///* 文件名    :ToolbarRegister.cs                           
///* 程序说明  : MDI主窗体上的工具条按钮注册器
///* 原创作者  :www.csframework.com 
///* 
///* Copyright 2006-2021 C/S框架网 www.csframework.com
///*
///**************************************************************************/
 
#endregion
 
namespace CSFrameworkV5.Library.CommonClass
{
    /// <summary>
    ///     DevExpress按钮注册器
    /// </summary>
    public class DevBarRegister : IToolbarRegister
    {
        private Bar _bar;
        private Form _Owner;
 
        public DevBarRegister(Form owner, Bar bar)
        {
            _Owner = owner;
            _bar = bar;
        }
 
        public IButtonInfo CreateButton(string name, string caption,
            ToolBarGroup group, Bitmap image, Size size,
            bool beginGroup, bool isLargeButton, OnButtonClick clickEvent)
        {
            return new DevBarButtonInfo(_bar.Manager, name, caption, group,
                image, beginGroup, isLargeButton,
                clickEvent);
        }
 
        public IButtonInfo CreateListButton(string name, string caption,
            ToolBarGroup group, Bitmap image, Size size,
            bool beginGroup, bool isLargeButton, OnButtonClick clickEvent,
            List<IButtonInfo> subItems)
        {
            return new DevBarListButton(_bar.Manager, name, caption, group,
                beginGroup, isLargeButton, image,
                clickEvent, subItems);
        }
 
        public IButtonInfo CreateSeperator()
        {
            //return new DevBarButtonInfo(_bar.Manager, "btn" + Guid.NewGuid().ToStringEx().Replace("-", ""), "-", null, null);
            return null;
        }
 
        public void Dispose()
        {
            //
        }
 
        public void RegisteButton(IList buttons)
        {
            _bar.BeginUpdate();
            _bar.ItemLinks.Clear();
            BarItemLink link;
            foreach (IButtonInfo bi in buttons)
            {
                var btn = bi.Button as BarItem;
                link = _bar.ItemLinks.Add(btn);
                link.BeginGroup = bi.BeginGroup;
            }
 
            _bar.EndUpdate();
        }
    }
 
    public class DevRibbonRegister : IToolbarRegister
    {
        public const string GROUP_TAG = "功能组动态创建";
        public const string GROUP_DEF = "预设功能组";
 
        private Form _Owner;
        private RibbonControl _RibbonControl;
        private RibbonPageGroup _RibbonDefaultPageGroup;
        private RibbonPage _RibbonPage;
 
        public DevRibbonRegister(Form owner, RibbonControl ribbonControl,
            RibbonPage ribbonPage,
            RibbonPageGroup defaultGroup)
        {
            _Owner = owner;
            _RibbonControl = ribbonControl;
            _RibbonPage = ribbonPage;
            _RibbonDefaultPageGroup = defaultGroup;
        }
 
        public IButtonInfo CreateButton(string name, string caption,
            ToolBarGroup group, Bitmap image, Size size,
            bool beginGroup, bool isLargeButton, OnButtonClick clickEvent)
        {
            return new DevBarButtonInfo(_RibbonControl.Manager, name, caption,
                group, image, beginGroup, isLargeButton,
                clickEvent);
        }
 
        public IButtonInfo CreateListButton(string name, string caption,
            ToolBarGroup group, Bitmap image, Size size,
            bool beginGroup, bool isLargeButton, OnButtonClick clickEvent,
            List<IButtonInfo> subItems)
        {
            return new DevBarListButton(_RibbonControl.Manager, name, caption,
                group, beginGroup, isLargeButton, image,
                clickEvent, subItems);
        }
 
        public IButtonInfo CreateSeperator()
        {
            return null;
        }
 
        public void Dispose()
        {
            //
        }
 
        public void RegisteButton(IList buttons)
        {
            RibbonPageGroup tmpGroup;
            BarItem btn;
 
            //清空动态创建的功能组按钮
            ClearRegistedButtons();
 
            foreach (IButtonInfo bi in buttons)
            {
                btn = bi.Button as BarItem;
                btn.RibbonStyle = bi.IsLargeButton
                    ? RibbonItemStyles.Large
                    : RibbonItemStyles.Default;
 
                //未指定分组名称,加到预设的分组
                if (string.IsNullOrEmpty(bi.GroupName))
                {
                    if (_RibbonDefaultPageGroup == null)
                        CreateDefaultPageGroup();
 
                    _RibbonDefaultPageGroup.ItemLinks.Add(btn);
                }
                else
                {
                    //根据按钮GroupName属性,将按钮添加到相应的PageGroup
                    tmpGroup = _RibbonControl.GetGroupByName(bi.GroupName);
                    if (tmpGroup != null)
                    {
                        tmpGroup.ItemLinks.Add(btn);
                    }
                    else
                    {
                        var group = new RibbonPageGroup(bi.GroupName);
                        group.ShowCaptionButton = false;
                        group.AllowMinimize = false;
                        group.Tag = GROUP_TAG;
                        group.Name = bi.GroupName;
                        group.ItemLinks.Add(btn);
                        var G = (ToolBarGroup)Enum.Parse(typeof(ToolBarGroup),
                            bi.GroupName);
                        var index = GetGroupIndex(_RibbonPage, G);
                        _RibbonPage.Groups.Insert(index, group);
                    }
                }
            }
        }
 
        /// <summary>
        ///     清空动态创建的功能组按钮
        /// </summary>
        private void ClearRegistedButtons()
        {
            if (_RibbonDefaultPageGroup != null)
                _RibbonDefaultPageGroup.ItemLinks.Clear();
 
            var list = new List<RibbonPageGroup>();
            foreach (RibbonPageGroup p in _RibbonPage.Groups)
                if (ConvertEx.ToString(p.Tag) == GROUP_TAG)
                    list.Add(p);
 
            for (var i = 0; i <= list.Count - 1; i++)
                _RibbonPage.Groups.Remove(list[i]);
        }
 
        private void CreateDefaultPageGroup()
        {
            _RibbonDefaultPageGroup = new RibbonPageGroup(GROUP_DEF);
            _RibbonDefaultPageGroup.ShowCaptionButton = false;
            _RibbonDefaultPageGroup.AllowMinimize = false;
            _RibbonDefaultPageGroup.Tag = GROUP_TAG;
            _RibbonDefaultPageGroup.Name = GROUP_DEF;
            _RibbonDefaultPageGroup.Text = GROUP_DEF;
            _RibbonPage.Groups.Add(_RibbonDefaultPageGroup);
        }
 
        private int GetGroupEnumID(string groupName)
        {
            ToolBarGroup G;
            if (Enum.TryParse(groupName, out G)) return (int)G;
 
            return 0;
        }
 
        private int GetGroupIndex(RibbonPage ribbonPage, ToolBarGroup group)
        {
            var groupIndex = (int)group; //当前组的固定序号
 
            var maxID = -1;
            int tmp;
            foreach (RibbonPageGroup G in ribbonPage.Groups)
            {
                tmp = GetGroupEnumID(G.Name);
                if (tmp < groupIndex)
                    if (tmp > maxID)
                        maxID = tmp;
            }
 
            return maxID + 1;
        }
    }
}