LayoutControlExtension.cs 2.4 KB

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