PopupHelper.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 = false, bool autoClose = true, 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. if (animation == EnumPopupAnimation.Slide)
  128. docker.Options.AnimationType = PopupToolWindowAnimation.Slide;
  129. else
  130. docker.Options.AnimationType = PopupToolWindowAnimation.Fade;
  131. PopupToolWindowAnchor anchor = PopupToolWindowAnchor.Right;
  132. switch (direct)
  133. {
  134. case EnumPopupDirection.Left:
  135. anchor = PopupToolWindowAnchor.Left;
  136. break;
  137. case EnumPopupDirection.Right:
  138. anchor = PopupToolWindowAnchor.Right;
  139. break;
  140. case EnumPopupDirection.Top:
  141. anchor = PopupToolWindowAnchor.Top;
  142. break;
  143. case EnumPopupDirection.Bottom:
  144. anchor = PopupToolWindowAnchor.Bottom;
  145. break;
  146. }
  147. docker.Options.AnchorType = anchor;
  148. docker.Controls[0].Controls.Clear();
  149. content.Dock = DockStyle.Fill;
  150. if (content is Form)
  151. content.Controls[0].Parent = docker.Controls[0];
  152. else
  153. content.Parent = docker.Controls[0];
  154. bool immediate = animation == EnumPopupAnimation.None;
  155. docker.ShowPopup(immediate);
  156. docker.Shown += (sender, e) =>
  157. {
  158. renderDone?.Invoke();
  159. };
  160. }
  161. }
  162. public static void HidePopup(Control content)
  163. {
  164. if (content.Parent == null || content.Parent.Parent == null) return;
  165. var docker = content.Parent.Parent as FlyoutPanel;
  166. docker?.HidePopup();
  167. }
  168. }
  169. }