PopupHelper.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using DevExpress.Utils;
  2. using DevExpress.Utils.Win;
  3. using DevExpress.XtraBars.Docking2010;
  4. using DevExpress.XtraEditors;
  5. using DevExpress.XtraEditors.ButtonsPanelControl;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Drawing;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using System.Windows.Forms;
  13. namespace DxHelper
  14. {
  15. /// <summary>
  16. /// Popup窗体弹出方向
  17. /// </summary>
  18. public enum EnumPopupDirection
  19. {
  20. /// <summary>
  21. /// 左侧弹出
  22. /// </summary>
  23. Left,
  24. /// <summary>
  25. /// 右侧弹出
  26. /// </summary>
  27. Right,
  28. /// <summary>
  29. /// 顶部弹出
  30. /// </summary>
  31. Top,
  32. /// <summary>
  33. /// 底部弹出
  34. /// </summary>
  35. Bottom,
  36. }
  37. /// <summary>
  38. /// Popup窗体弹出动画
  39. /// </summary>
  40. public enum EnumPopupAnimation
  41. {
  42. /// <summary>
  43. /// 无动画,立即显示或隐藏
  44. /// </summary>
  45. None,
  46. /// <summary>
  47. /// 颜色淡入淡出
  48. /// </summary>
  49. Fade,
  50. /// <summary>
  51. /// 抽屉滑入滑出
  52. /// </summary>
  53. Slide
  54. }
  55. public static class PopupHelper
  56. {
  57. private static Dictionary<Control, FlyoutPanel> dic = new Dictionary<Control, FlyoutPanel>();
  58. /// <summary>
  59. /// 从popup窗体中弹出content
  60. /// </summary>
  61. /// <param name="content">要显示的control,content必须是一个容器控件,如Panel、Form等</param>
  62. /// <param name="owner">popup窗体的父容器</param>
  63. /// <param name="size">popup窗体的大小,为0时将使用owner的大小</param>
  64. /// <param name="direct">窗体弹出位置</param>
  65. /// <param name="animation">弹出动画</param>
  66. /// <param name="showCloseButton">是否显示关闭按钮</param>
  67. /// <param name="autoClose">点击窗体外部是否自动关闭popup窗体</param>
  68. /// <param name="renderDone">渲染完成后的回调函数</param>
  69. public static void ShowPopup(Control content, Control owner, int size = 0, EnumPopupDirection direct = EnumPopupDirection.Right, EnumPopupAnimation animation = EnumPopupAnimation.Slide, bool showCloseButton = true, bool autoClose = false, Action renderDone = null)
  70. {
  71. foreach (var item in dic.Values)
  72. {
  73. item.HidePopup();
  74. }
  75. FlyoutPanel docker;
  76. if (!dic.ContainsKey(content))
  77. {
  78. docker = new FlyoutPanel();
  79. if (showCloseButton)
  80. {
  81. docker.OptionsButtonPanel.ShowButtonPanel = true;
  82. ButtonImageOptions options = new ButtonImageOptions();
  83. options.SvgImage = SvgHelper.CreateClose();
  84. options.SvgImageSize = new System.Drawing.Size(16, 16);
  85. var btnClose = new PeekFormButton("", true, options, ButtonStyle.PushButton, "", -1, true, null, true, false, true, null, -1, false);
  86. btnClose.Click += (sender, e) =>
  87. {
  88. docker.Tag = "btnClose";
  89. docker?.HidePopup();
  90. };
  91. docker.OptionsButtonPanel.Buttons.Add(btnClose);
  92. docker.OptionsButtonPanel.ButtonPanelContentAlignment = ContentAlignment.MiddleLeft;
  93. docker.OptionsButtonPanel.ButtonPanelHeight = 24;
  94. }
  95. var panel = new FlyoutPanelControl(docker) { Dock = DockStyle.Fill };
  96. docker.Controls.Add(panel);
  97. dic.Add(content, docker);
  98. }
  99. else
  100. docker = dic[content];
  101. if (docker.IsPopupOpen)
  102. docker.HidePopup();
  103. else
  104. {
  105. if (owner != null)
  106. {
  107. docker.OwnerControl = owner;
  108. }
  109. if (docker.OwnerControl == null)
  110. {
  111. var parent = content.Parent;
  112. while (!(parent is Form))
  113. {
  114. parent = parent.Parent;
  115. }
  116. docker.OwnerControl = parent;
  117. }
  118. if (size > 0)
  119. docker.Width = docker.Height = size;
  120. else
  121. {
  122. docker.Width = owner.Width;
  123. docker.Height = owner.Height;
  124. owner.SizeChanged += (sender, e) =>
  125. {
  126. if (docker.Parent != null)
  127. docker.Parent.Size = new Size(owner.Width, owner.Height);
  128. };
  129. }
  130. docker.Options.CloseOnOuterClick = autoClose;
  131. docker.Options.CloseOnHidingOwner = autoClose;
  132. if (animation == EnumPopupAnimation.Slide)
  133. docker.Options.AnimationType = PopupToolWindowAnimation.Slide;
  134. else
  135. docker.Options.AnimationType = PopupToolWindowAnimation.Fade;
  136. PopupToolWindowAnchor anchor = PopupToolWindowAnchor.Right;
  137. switch (direct)
  138. {
  139. case EnumPopupDirection.Left:
  140. anchor = PopupToolWindowAnchor.Left;
  141. break;
  142. case EnumPopupDirection.Right:
  143. anchor = PopupToolWindowAnchor.Right;
  144. break;
  145. case EnumPopupDirection.Top:
  146. anchor = PopupToolWindowAnchor.Top;
  147. break;
  148. case EnumPopupDirection.Bottom:
  149. anchor = PopupToolWindowAnchor.Bottom;
  150. break;
  151. }
  152. docker.Options.AnchorType = anchor;
  153. docker.Controls[0].Controls.Clear();
  154. content.Dock = DockStyle.Fill;
  155. if (content is Form)
  156. content.Controls[0].Parent = docker.Controls[0];
  157. else
  158. content.Parent = docker.Controls[0];
  159. bool immediate = animation == EnumPopupAnimation.None;
  160. docker.ShowPopup(immediate);
  161. docker.Shown += (sender, e) =>
  162. {
  163. renderDone?.Invoke();
  164. };
  165. }
  166. }
  167. public static void HidePopup(Control content)
  168. {
  169. if (content.Parent == null || content.Parent.Parent == null) return;
  170. var docker = content.Parent.Parent as FlyoutPanel;
  171. docker?.HidePopup();
  172. }
  173. }
  174. }