RibbonPageHelper.cs 8.1 KB

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