LayoutControlExtension.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. 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. System.Windows.Forms.Control findCtrl = ctrl;
  36. string GetLayoutName()
  37. {
  38. while (findCtrl.Parent != null)
  39. {
  40. findCtrl = findCtrl.Parent;
  41. if (findCtrl is System.Windows.Forms.UserControl) break;
  42. }
  43. return $"{findCtrl.Name}_{ctrl.Name}";
  44. }
  45. string name = GetLayoutName();
  46. EventHandler loadEvent = (sender, e) =>
  47. {
  48. var form = ctrl.FindForm();
  49. if (form != null)
  50. {
  51. form.VisibleChanged += (sender2, e2) =>
  52. {
  53. Directory.CreateDirectory("Layout");
  54. if (name == null) return;
  55. ctrl.SaveLayoutToXml($"Layout\\{name}.xml");
  56. };
  57. }
  58. };
  59. if (findCtrl is Form frm)
  60. {
  61. frm.Load += loadEvent;
  62. }
  63. else if (findCtrl is UserControl uCtrl)
  64. {
  65. uCtrl.Load += loadEvent;
  66. }
  67. if ( name != null && File.Exists($"Layout\\{name}.xml"))
  68. {
  69. if (Debugger.IsAttached)
  70. {
  71. File.Delete($"Layout\\{name}.xml");
  72. }
  73. else
  74. {
  75. ctrl.RestoreLayoutFromXml($"Layout\\{name}.xml");
  76. }
  77. }
  78. }
  79. }
  80. }