using DevExpress.XtraBars; using DevExpress.XtraBars.Ribbon; using System; using System.Collections.Generic; using System.Drawing; using System.Globalization; using System.IO; using System.Linq; using System.Reflection; using System.Resources; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using ProjectBase.Data.Logs; using SIMDP.BLL; using SIMDP.Model; using SIMDP.DAL.DALSQL; using SIMDP.Util; using ProjectBase.Data.BaseDAL; namespace SIMDP.RibbonPageHelper { /// /// 动态创建RibbonPage和其下面的按钮项目辅助类 /// public class RibbonPageHelper { //BlRoleRight bl = new BlRoleRight(); private RibbonControl control; public MainForm mainForm; public RibbonPageHelper(MainForm mainForm, ref RibbonControl control) { this.mainForm = mainForm; this.control = control; } public void AddPages() { #region 初始化数据 string condition = null; //BlMenuInfo menu = new BlMenuInfo(); List moFirstList = null; // 一级标题 condition = string.Format(" menu_winform_type = '1' AND menu_delete = 0"); moFirstList = BLLFactory.Instance.Find(condition); #endregion int i = 0; foreach (MoMenuInfo firstInfo in moFirstList) { //如果没有菜单的权限,则跳过 if (!BLLFactory.Instance.Function(SysEnvironment.CurrentLoginID,firstInfo.ID)) continue; //添加页面(一级菜单) RibbonPage page = new DevExpress.XtraBars.Ribbon.RibbonPage(); page.Text = firstInfo.Name; page.Name = firstInfo.ID; this.control.Pages.Insert(i++, page); List moSecondList = null;// 二级标题 condition = string.Format(" menu_winform_type = '2' AND menu_parent_id = '{0}' AND menu_delete = 0", firstInfo.ID); moSecondList = BLLFactory.Instance.Find(condition); if (moSecondList.Count == 0) continue; foreach (MoMenuInfo secondInfo in moSecondList) { //如果没有菜单的权限,则跳过 if (!BLLFactory.Instance.Function(SysEnvironment.CurrentLoginID, secondInfo.ID)) continue; //添加RibbonPageGroup(二级菜单) if (!secondInfo.PID.Equals(firstInfo.ID)) continue; RibbonPageGroup group = new RibbonPageGroup(); group.Text = secondInfo.Name; group.Name = secondInfo.ID; page.Groups.Add(group); List moThirdList = null; // 三级标题 condition = string.Format(" menu_winform_type = '3' AND menu_parent_id = '{0}' AND menu_delete = 0", secondInfo.ID); moThirdList = BLLFactory.Instance.Find(condition); if (moThirdList.Count == 0) continue; foreach (MoMenuInfo thirdInfo in moThirdList) { //如果没有菜单的权限,则跳过 if (!BLLFactory.Instance.Function(SysEnvironment.CurrentLoginID, thirdInfo.ID)) continue; //添加功能按钮(三级菜单) if (!thirdInfo.PID.Equals(secondInfo.ID)) continue; BarButtonItem button = new BarButtonItem(); button.PaintStyle = BarItemPaintStyle.CaptionGlyph; button.LargeGlyph = LoadIcon(thirdInfo.Icon); button.Glyph = LoadIcon(thirdInfo.Icon); button.Name = thirdInfo.ID; button.Caption = thirdInfo.Name; button.Tag = thirdInfo.Url; button.ItemClick += (sender, e) => { if (button.Tag != null && !string.IsNullOrEmpty(button.Tag.ToString())) { Type type = Type.GetType(button.Tag.ToString()); LoadMdiForm(mainForm, type); } //else //{ // //MessageDxUtil.ShowTips(button.Caption); //} }; group.ItemLinks.Add(button); } } } } /// /// 加载图标,如果加载不成功,那么使用默认图标 /// /// /// private Image LoadIcon(string iconPath) { string filePath = Application.StartupPath + "\\Images\\100.ico"; Icon icon = Icon.ExtractAssociatedIcon(filePath); Image result = icon.ToBitmap(); try { if (!string.IsNullOrEmpty(iconPath)) { string path = Application.StartupPath + iconPath; if (File.Exists(path)) { result = Image.FromFile(path); } } } catch { LogHelper.log.Error(string.Format("无法识别图标地址:{0},请确保该文件存在!", iconPath)); } return result; } /// /// 加载插件窗体 /// private void LoadPlugInForm(string typeName) { try { string[] itemArray = typeName.Split(new char[] { ',', ';' }); string type = itemArray[0].Trim(); string filePath = itemArray[1].Trim();//必须是相对路径 //判断是否配置了显示模式,默认窗体为Show非模式显示 string showDialog = (itemArray.Length > 2) ? itemArray[2].ToLower() : ""; bool isShowDialog = (showDialog == "1") || (showDialog == "dialog"); string dllFullPath = Path.Combine(Application.StartupPath, filePath); Assembly tempAssembly = System.Reflection.Assembly.LoadFrom(dllFullPath); if (tempAssembly != null) { Type objType = tempAssembly.GetType(type); if (objType != null) { //LoadMdiForm(this.mainForm, objType, isShowDialog); } } } catch (Exception ex) { LogHelper.log.Error(string.Format("加载模块【{0}】失败,请检查书写是否正确。", typeName), ex); } } /// /// 唯一加载某个类型的窗体,如果存在则显示,否则创建。 /// /// 主窗体对象 /// 待显示的窗体类型 /// public static Form LoadMdiForm(Form mainDialog, Type formType) { bool bFound = false; Form tableForm = null; foreach (Form form in mainDialog.MdiChildren) { if (form.GetType() == formType) { bFound = true; tableForm = form; break; } } if (!bFound) { tableForm = (Form)Activator.CreateInstance(formType); tableForm.MdiParent = mainDialog; tableForm.Show(); } //tableForm.Dock = DockStyle.Fill; //tableForm.WindowState = FormWindowState.Maximized; tableForm.BringToFront(); tableForm.Activate(); return tableForm; } } }