RibbonPageHelper.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using DevExpress.XtraBars;
  2. using DevExpress.XtraBars.Ribbon;
  3. using ProjectBase.Data.BaseDAL;
  4. using ProjectBase.Data.Logs;
  5. using SIASUN.Autopilot.BLL;
  6. using SIASUN.Autopilot.Model;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Drawing;
  10. using System.IO;
  11. using System.Linq;
  12. using System.Reflection;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15. using System.Windows.Forms;
  16. namespace SIASUN.Autopilot.View.RibbonPageHelper
  17. {
  18. /// <summary>
  19. /// 动态创建RibbonPage和其下面的按钮项目辅助类
  20. /// </summary>
  21. public class RibbonPageHelper
  22. {
  23. //BlRoleRight bl = new BlRoleRight();
  24. private RibbonControl control;
  25. public FormMain mainForm;
  26. public RibbonPageHelper(FormMain mainForm, ref RibbonControl control)
  27. {
  28. this.mainForm = mainForm;
  29. this.control = control;
  30. }
  31. public void AddPages()
  32. {
  33. #region 初始化数据
  34. string condition = null;
  35. //BlMenuInfo menu = new BlMenuInfo();
  36. List<MoMenuInfo> moFirstList = null; // 一级标题
  37. condition = string.Format(" menu_winform_type = '1' AND menu_delete = 0");
  38. moFirstList = BLLFactory<BlMenuInfo>.Instance.Find(condition);
  39. #endregion
  40. int i = 0;
  41. foreach (MoMenuInfo firstInfo in moFirstList)
  42. {
  43. //如果没有菜单的权限,则跳过
  44. //if (!BLLFactory<BlRoleRight>.Instance.Function(SysEnvironment.CurrentLoginID, firstInfo.ID)) continue;
  45. //添加页面(一级菜单)
  46. RibbonPage page = new DevExpress.XtraBars.Ribbon.RibbonPage();
  47. page.Text = firstInfo.Name;
  48. page.Name = firstInfo.ID;
  49. this.control.Pages.Insert(i++, page);
  50. List<MoMenuInfo> moSecondList = null;// 二级标题
  51. condition = string.Format(" menu_winform_type = '2' AND menu_parent_id = '{0}' AND menu_delete = 0", firstInfo.ID);
  52. moSecondList = BLLFactory<BlMenuInfo>.Instance.Find(condition);
  53. if (moSecondList.Count == 0) continue;
  54. foreach (MoMenuInfo secondInfo in moSecondList)
  55. {
  56. //如果没有菜单的权限,则跳过
  57. //if (!BLLFactory<BlRoleRight>.Instance.Function(SysEnvironment.CurrentLoginID, secondInfo.ID)) continue;
  58. //添加RibbonPageGroup(二级菜单)
  59. if (!secondInfo.PID.Equals(firstInfo.ID))
  60. continue;
  61. RibbonPageGroup group = new RibbonPageGroup();
  62. group.Text = secondInfo.Name;
  63. group.Name = secondInfo.ID;
  64. page.Groups.Add(group);
  65. List<MoMenuInfo> moThirdList = null; // 三级标题
  66. condition = string.Format(" menu_winform_type = '3' AND menu_parent_id = '{0}' AND menu_delete = 0", secondInfo.ID);
  67. moThirdList = BLLFactory<BlMenuInfo>.Instance.Find(condition);
  68. if (moThirdList.Count == 0) continue;
  69. foreach (MoMenuInfo thirdInfo in moThirdList)
  70. {
  71. //如果没有菜单的权限,则跳过
  72. //if (!BLLFactory<BlRoleRight>.Instance.Function(SysEnvironment.CurrentLoginID, thirdInfo.ID)) continue;
  73. //添加功能按钮(三级菜单)
  74. if (!thirdInfo.PID.Equals(secondInfo.ID))
  75. continue;
  76. BarButtonItem button = new BarButtonItem();
  77. button.PaintStyle = BarItemPaintStyle.CaptionGlyph;
  78. button.LargeGlyph = LoadIcon(thirdInfo.Icon);
  79. button.Glyph = LoadIcon(thirdInfo.Icon);
  80. button.Name = thirdInfo.ID;
  81. button.Caption = thirdInfo.Name;
  82. button.Tag = thirdInfo.Url;
  83. button.ItemClick += (sender, e) =>
  84. {
  85. if (button.Tag != null && !string.IsNullOrEmpty(button.Tag.ToString()))
  86. {
  87. Type type = Type.GetType(button.Tag.ToString());
  88. LoadMdiForm(mainForm, type);
  89. }
  90. //else
  91. //{
  92. // //MessageDxUtil.ShowTips(button.Caption);
  93. //}
  94. };
  95. group.ItemLinks.Add(button);
  96. }
  97. }
  98. }
  99. }
  100. /// <summary>
  101. /// 加载图标,如果加载不成功,那么使用默认图标
  102. /// </summary>
  103. /// <param name="iconPath"></param>
  104. /// <returns></returns>
  105. private Image LoadIcon(string iconPath)
  106. {
  107. string filePath = Application.StartupPath + "\\Images\\100.ico";
  108. Icon icon = Icon.ExtractAssociatedIcon(filePath);
  109. Image result = icon.ToBitmap();
  110. try
  111. {
  112. if (!string.IsNullOrEmpty(iconPath))
  113. {
  114. string path = Application.StartupPath + iconPath;
  115. if (File.Exists(path))
  116. {
  117. result = Image.FromFile(path);
  118. }
  119. }
  120. }
  121. catch
  122. {
  123. LogHelper.log.Error(string.Format("无法识别图标地址:{0},请确保该文件存在!", iconPath));
  124. }
  125. return result;
  126. }
  127. /// <summary>
  128. /// 加载插件窗体
  129. /// </summary>
  130. private void LoadPlugInForm(string typeName)
  131. {
  132. try
  133. {
  134. string[] itemArray = typeName.Split(new char[] { ',', ';' });
  135. string type = itemArray[0].Trim();
  136. string filePath = itemArray[1].Trim();//必须是相对路径
  137. //判断是否配置了显示模式,默认窗体为Show非模式显示
  138. string showDialog = (itemArray.Length > 2) ? itemArray[2].ToLower() : "";
  139. bool isShowDialog = (showDialog == "1") || (showDialog == "dialog");
  140. string dllFullPath = Path.Combine(Application.StartupPath, filePath);
  141. Assembly tempAssembly = System.Reflection.Assembly.LoadFrom(dllFullPath);
  142. if (tempAssembly != null)
  143. {
  144. Type objType = tempAssembly.GetType(type);
  145. if (objType != null)
  146. {
  147. //LoadMdiForm(this.mainForm, objType, isShowDialog);
  148. }
  149. }
  150. }
  151. catch (Exception ex)
  152. {
  153. LogHelper.log.Error(string.Format("加载模块【{0}】失败,请检查书写是否正确。", typeName), ex);
  154. }
  155. }
  156. /// <summary>
  157. /// 唯一加载某个类型的窗体,如果存在则显示,否则创建。
  158. /// </summary>
  159. /// <param name="mainDialog">主窗体对象</param>
  160. /// <param name="formType">待显示的窗体类型</param>
  161. /// <returns></returns>
  162. public static Form LoadMdiForm(Form mainDialog, Type formType)
  163. {
  164. bool bFound = false;
  165. Form tableForm = null;
  166. foreach (Form form in mainDialog.MdiChildren)
  167. {
  168. if (form.GetType() == formType)
  169. {
  170. bFound = true;
  171. tableForm = form;
  172. break;
  173. }
  174. }
  175. if (!bFound)
  176. {
  177. tableForm = (Form)Activator.CreateInstance(formType);
  178. tableForm.MdiParent = mainDialog;
  179. tableForm.Show();
  180. }
  181. //tableForm.Dock = DockStyle.Fill;
  182. //tableForm.WindowState = FormWindowState.Maximized;
  183. tableForm.BringToFront();
  184. tableForm.Activate();
  185. return tableForm;
  186. }
  187. }
  188. }