LayoutControlExtension.cs 2.1 KB

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