LayoutControlExtension.cs 2.4 KB

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