LayoutControlExtension.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using DevExpress.Utils;
  2. using DevExpress.XtraEditors;
  3. using DevExpress.XtraLayout;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. using XdCxRhDW.Core;
  13. namespace ExtensionsDev
  14. {
  15. public static class LayoutControlExtension
  16. {
  17. public static void UseDefault(this LayoutControl ctrl)
  18. {
  19. ctrl.AllowTouchGestures = DevExpress.Utils.DefaultBoolean.False;
  20. ctrl.AllowDrop = false;
  21. ctrl.AllowCustomization = false;
  22. ctrl.OptionsFocus.AllowFocusGroups = false;
  23. ctrl.OptionsFocus.AllowFocusTabbedGroups = false;
  24. ctrl.OptionsFocus.AllowFocusControlOnLabelClick = true;
  25. // ctrl.Root.GroupBordersVisible = false;
  26. System.Windows.Forms.Control findCtrl = ctrl;
  27. string GetLayoutName()
  28. {
  29. while (findCtrl.Parent != null)
  30. {
  31. findCtrl = findCtrl.Parent;
  32. if (findCtrl is System.Windows.Forms.UserControl) break;
  33. }
  34. return $"{findCtrl.Name}_{ctrl.Name}";
  35. }
  36. string name = GetLayoutName();
  37. EventHandler loadEvent = (sender, e) =>
  38. {
  39. var form = ctrl.FindForm();
  40. if (form != null)
  41. {
  42. form.VisibleChanged += (sender2, e2) =>
  43. {
  44. Directory.CreateDirectory("Layout");
  45. if (name == null) return;
  46. ctrl.SaveLayoutToXml($"Layout\\{name}.xml");
  47. };
  48. }
  49. };
  50. if (findCtrl is Form frm)
  51. {
  52. frm.Load += loadEvent;
  53. }
  54. else if (findCtrl is UserControl uCtrl)
  55. {
  56. uCtrl.Load += loadEvent;
  57. }
  58. if (name != null && File.Exists($"Layout\\{name}.xml"))
  59. {
  60. ctrl.RestoreLayoutFromXml($"Layout\\{name}.xml");
  61. }
  62. }
  63. }
  64. }