PopupHelper.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. FlyoutPanel docker;
  72. if (!dic.ContainsKey(content))
  73. {
  74. docker = new FlyoutPanel();
  75. if (showCloseButton)
  76. {
  77. docker.OptionsButtonPanel.ShowButtonPanel = true;
  78. ButtonImageOptions options = new ButtonImageOptions();
  79. options.SvgImage = SvgHelper.CreateClose();
  80. options.SvgImageSize = new System.Drawing.Size(16, 16);
  81. var btnClose = new PeekFormButton("", true, options, ButtonStyle.PushButton, "", -1, true, null, true, false, true, null, -1, false);
  82. btnClose.Click += (sender, e) =>
  83. {
  84. docker.Tag = "btnClose";
  85. docker?.HidePopup();
  86. };
  87. docker.OptionsButtonPanel.Buttons.Add(btnClose);
  88. docker.OptionsButtonPanel.ButtonPanelContentAlignment = ContentAlignment.MiddleLeft;
  89. docker.OptionsButtonPanel.ButtonPanelHeight = 24;
  90. }
  91. var panel = new FlyoutPanelControl(docker) { Dock = DockStyle.Fill };
  92. docker.Controls.Add(panel);
  93. dic.Add(content, docker);
  94. }
  95. else
  96. docker = dic[content];
  97. if (docker.IsPopupOpen)
  98. docker.HidePopup();
  99. else
  100. {
  101. if (owner != null)
  102. {
  103. docker.OwnerControl = owner;
  104. }
  105. if (docker.OwnerControl == null)
  106. {
  107. var parent = content.Parent;
  108. while (!(parent is Form))
  109. {
  110. parent = parent.Parent;
  111. }
  112. docker.OwnerControl = parent;
  113. }
  114. if (size > 0)
  115. docker.Width = docker.Height = size;
  116. else
  117. {
  118. docker.Width = owner.Width;
  119. docker.Height = owner.Height;
  120. owner.SizeChanged += (sender, e) =>
  121. {
  122. if (docker.Parent != null)
  123. docker.Parent.Size = new Size(owner.Width, owner.Height);
  124. };
  125. }
  126. docker.Options.CloseOnOuterClick = autoClose;
  127. docker.Options.CloseOnHidingOwner = autoClose;
  128. if (animation == EnumPopupAnimation.Slide)
  129. docker.Options.AnimationType = PopupToolWindowAnimation.Slide;
  130. else
  131. docker.Options.AnimationType = PopupToolWindowAnimation.Fade;
  132. PopupToolWindowAnchor anchor = PopupToolWindowAnchor.Right;
  133. switch (direct)
  134. {
  135. case EnumPopupDirection.Left:
  136. anchor = PopupToolWindowAnchor.Left;
  137. break;
  138. case EnumPopupDirection.Right:
  139. anchor = PopupToolWindowAnchor.Right;
  140. break;
  141. case EnumPopupDirection.Top:
  142. anchor = PopupToolWindowAnchor.Top;
  143. break;
  144. case EnumPopupDirection.Bottom:
  145. anchor = PopupToolWindowAnchor.Bottom;
  146. break;
  147. }
  148. docker.Options.AnchorType = anchor;
  149. docker.Controls[0].Controls.Clear();
  150. content.Dock = DockStyle.Fill;
  151. if (content is Form)
  152. content.Controls[0].Parent = docker.Controls[0];
  153. else
  154. content.Parent = docker.Controls[0];
  155. bool immediate = animation == EnumPopupAnimation.None;
  156. docker.ShowPopup(immediate);
  157. docker.Shown += (sender, e) =>
  158. {
  159. renderDone?.Invoke();
  160. };
  161. }
  162. }
  163. public static void HidePopup(Control content)
  164. {
  165. if (content.Parent == null || content.Parent.Parent == null) return;
  166. var docker = content.Parent.Parent as FlyoutPanel;
  167. docker?.HidePopup();
  168. }
  169. }
  170. }