LayoutControlExtension.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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, bool saveLayout = false)
  20. {
  21. foreach (var item in ctrl.Root.Items)
  22. {
  23. if (item is LayoutControlItem layItem)
  24. {
  25. layItem.AllowHtmlStringInCaption = true;
  26. }
  27. }
  28. ctrl.AllowTouchGestures = DevExpress.Utils.DefaultBoolean.False;
  29. ctrl.AllowDrop = false;
  30. ctrl.AllowCustomization = false;
  31. ctrl.OptionsFocus.AllowFocusGroups = false;
  32. ctrl.OptionsFocus.AllowFocusTabbedGroups = false;
  33. ctrl.OptionsFocus.AllowFocusControlOnLabelClick = true;
  34. // ctrl.Root.GroupBordersVisible = false;
  35. if (saveLayout)
  36. {
  37. System.Windows.Forms.Control findCtrl = ctrl;
  38. string GetLayoutName()
  39. {
  40. while (findCtrl.Parent != null)
  41. {
  42. findCtrl = findCtrl.Parent;
  43. if (findCtrl is System.Windows.Forms.UserControl) break;
  44. }
  45. return $"{findCtrl.Name}_{ctrl.Name}";
  46. }
  47. string name = GetLayoutName();
  48. EventHandler loadEvent = (sender, e) =>
  49. {
  50. var form = ctrl.FindForm();
  51. if (form != null)
  52. {
  53. form.VisibleChanged += (sender2, e2) =>
  54. {
  55. Directory.CreateDirectory("Layout");
  56. if (name == null) return;
  57. ctrl.SaveLayoutToXml($"Layout\\{name}.xml");
  58. };
  59. }
  60. };
  61. if (findCtrl is Form frm)
  62. {
  63. frm.Load += loadEvent;
  64. }
  65. else if (findCtrl is UserControl uCtrl)
  66. {
  67. uCtrl.Load += loadEvent;
  68. }
  69. if (name != null && File.Exists($"Layout\\{name}.xml"))
  70. {
  71. if (Debugger.IsAttached)
  72. {
  73. //File.Delete($"Layout\\{name}.xml");
  74. }
  75. else
  76. {
  77. ctrl.RestoreLayoutFromXml($"Layout\\{name}.xml");
  78. }
  79. }
  80. }
  81. }
  82. }
  83. }