Popup.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /*******************************************************************************
  2. * Copyright(c) 2014 DongkeSoft All rights reserved. / Confidential
  3. * 类的信息:
  4. * 1.程序名称:NativeMethods.cs
  5. * 2.功能描述:
  6. * 编辑履历:
  7. * 作者 日期 版本 修改内容
  8. * 陈晓野 2014/09/04 1.00 新建
  9. *******************************************************************************/
  10. using System;
  11. using System.Collections.Generic;
  12. using System.ComponentModel;
  13. using System.Data;
  14. using System.Drawing;
  15. using System.Text;
  16. using System.Windows.Forms;
  17. using System.Security.Permissions;
  18. using System.Runtime.InteropServices;
  19. using VS = System.Windows.Forms.VisualStyles;
  20. /*
  21. <li>Base class for custom tooltips.</li>
  22. <li>Office-2007-like tooltip class.</li>
  23. */
  24. namespace Dongke.IBOSS.PRD.Basics.BaseControls
  25. {
  26. /// <summary>
  27. /// CodeProject.com "Simple pop-up control" "http://www.codeproject.com/cs/miscctrl/simplepopup.asp".
  28. /// Represents a pop-up window.
  29. /// </summary>
  30. [ToolboxItem(false)]
  31. //[CLSCompliant(true)]
  32. public partial class Popup : ToolStripDropDown
  33. {
  34. #region " Fields & Properties "
  35. private Control content;
  36. /// <summary>
  37. /// Gets the content of the pop-up.
  38. /// </summary>
  39. public Control Content
  40. {
  41. get { return content; }
  42. }
  43. private bool fade;
  44. /// <summary>
  45. /// Gets a value indicating whether the <see cref="PopupControl.Popup"/> uses the fade effect.
  46. /// </summary>
  47. /// <value><c>true</c> if pop-up uses the fade effect; otherwise, <c>false</c>.</value>
  48. /// <remarks>To use the fade effect, the FocusOnOpen property also has to be set to <c>true</c>.</remarks>
  49. public bool UseFadeEffect
  50. {
  51. get { return fade; }
  52. set
  53. {
  54. if (fade == value) return;
  55. fade = value;
  56. }
  57. }
  58. private bool focusOnOpen = true;
  59. /// <summary>
  60. /// Gets or sets a value indicating whether to focus the content after the pop-up has been opened.
  61. /// </summary>
  62. /// <value><c>true</c> if the content should be focused after the pop-up has been opened; otherwise, <c>false</c>.</value>
  63. /// <remarks>If the FocusOnOpen property is set to <c>false</c>, then pop-up cannot use the fade effect.</remarks>
  64. public bool FocusOnOpen
  65. {
  66. get { return focusOnOpen; }
  67. set { focusOnOpen = value; }
  68. }
  69. private bool acceptAlt = true;
  70. /// <summary>
  71. /// Gets or sets a value indicating whether presing the alt key should close the pop-up.
  72. /// </summary>
  73. /// <value><c>true</c> if presing the alt key does not close the pop-up; otherwise, <c>false</c>.</value>
  74. public bool AcceptAlt
  75. {
  76. get { return acceptAlt; }
  77. set { acceptAlt = value; }
  78. }
  79. private Popup ownerPopup;
  80. private Popup childPopup;
  81. private bool _resizable;
  82. private bool resizable;
  83. /// <summary>
  84. /// Gets or sets a value indicating whether this <see cref="PopupControl.Popup" /> is resizable.
  85. /// </summary>
  86. /// <value><c>true</c> if resizable; otherwise, <c>false</c>.</value>
  87. public bool Resizable
  88. {
  89. get { return resizable && _resizable; }
  90. set { resizable = value; }
  91. }
  92. private ToolStripControlHost host;
  93. private Size minSize;
  94. /// <summary>
  95. /// Gets or sets the size that is the lower limit that <see cref="M:System.Windows.Forms.Control.GetPreferredSize(System.Drawing.Size)" /> can specify.
  96. /// </summary>
  97. /// <returns>An ordered pair of type <see cref="T:System.Drawing.Size" /> representing the width and height of a rectangle.</returns>
  98. public new Size MinimumSize
  99. {
  100. get { return minSize; }
  101. set { minSize = value; }
  102. }
  103. private Size maxSize;
  104. /// <summary>
  105. /// Gets or sets the size that is the upper limit that <see cref="M:System.Windows.Forms.Control.GetPreferredSize(System.Drawing.Size)" /> can specify.
  106. /// </summary>
  107. /// <returns>An ordered pair of type <see cref="T:System.Drawing.Size" /> representing the width and height of a rectangle.</returns>
  108. public new Size MaximumSize
  109. {
  110. get { return maxSize; }
  111. set { maxSize = value; }
  112. }
  113. /// <summary>
  114. /// Gets parameters of a new window.
  115. /// </summary>
  116. /// <returns>An object of type <see cref="T:System.Windows.Forms.CreateParams" /> used when creating a new window.</returns>
  117. protected override CreateParams CreateParams
  118. {
  119. [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
  120. get
  121. {
  122. CreateParams cp = base.CreateParams;
  123. cp.ExStyle |= NativeMethods.WS_EX_NOACTIVATE;
  124. return cp;
  125. }
  126. }
  127. #endregion
  128. #region " Constructors "
  129. /// <summary>
  130. /// Initializes a new instance of the <see cref="PopupControl.Popup"/> class.
  131. /// </summary>
  132. /// <param name="content">The content of the pop-up.</param>
  133. /// <remarks>
  134. /// Pop-up will be disposed immediately after disposion of the content control.
  135. /// </remarks>
  136. /// <exception cref="T:System.ArgumentNullException"><paramref name="content" /> is <code>null</code>.</exception>
  137. public Popup(Control content)
  138. {
  139. if (content == null)
  140. {
  141. throw new ArgumentNullException("content");
  142. }
  143. this.content = content;
  144. this.fade = SystemInformation.IsMenuAnimationEnabled && SystemInformation.IsMenuFadeEnabled;
  145. this._resizable = true;
  146. InitializeComponent();
  147. AutoSize = false;
  148. DoubleBuffered = true;
  149. ResizeRedraw = true;
  150. host = new ToolStripControlHost(content);
  151. Padding = Margin = host.Padding = host.Margin = Padding.Empty;
  152. MinimumSize = content.MinimumSize;
  153. content.MinimumSize = content.Size;
  154. MaximumSize = content.MaximumSize;
  155. content.MaximumSize = content.Size;
  156. Size = content.Size;
  157. content.Location = Point.Empty;
  158. Items.Add(host);
  159. content.Disposed += delegate(object sender, EventArgs e)
  160. {
  161. content = null;
  162. Dispose(true);
  163. };
  164. content.RegionChanged += delegate(object sender, EventArgs e)
  165. {
  166. UpdateRegion();
  167. };
  168. content.Paint += delegate(object sender, PaintEventArgs e)
  169. {
  170. PaintSizeGrip(e);
  171. };
  172. UpdateRegion();
  173. }
  174. #endregion
  175. #region " Methods "
  176. /// <summary>
  177. /// Processes a dialog box key.
  178. /// </summary>
  179. /// <param name="keyData">One of the <see cref="T:System.Windows.Forms.Keys" /> values that represents the key to process.</param>
  180. /// <returns>
  181. /// true if the key was processed by the control; otherwise, false.
  182. /// </returns>
  183. protected override bool ProcessDialogKey(Keys keyData)
  184. {
  185. if (acceptAlt && ((keyData & Keys.Alt) == Keys.Alt)) return false;
  186. return base.ProcessDialogKey(keyData);
  187. }
  188. /// <summary>
  189. /// Updates the pop-up region.
  190. /// </summary>
  191. protected void UpdateRegion()
  192. {
  193. if (this.Region != null)
  194. {
  195. this.Region.Dispose();
  196. this.Region = null;
  197. }
  198. if (content.Region != null)
  199. {
  200. this.Region = content.Region.Clone();
  201. }
  202. }
  203. /// <summary>
  204. /// Shows pop-up window below the specified control.
  205. /// </summary>
  206. /// <param name="control">The control below which the pop-up will be shown.</param>
  207. /// <remarks>
  208. /// When there is no space below the specified control, the pop-up control is shown above it.
  209. /// </remarks>
  210. /// <exception cref="T:System.ArgumentNullException"><paramref name="control"/> is <code>null</code>.</exception>
  211. public void Show(Control control)
  212. {
  213. if (control == null)
  214. {
  215. throw new ArgumentNullException("control");
  216. }
  217. SetOwnerItem(control);
  218. Show(control, control.ClientRectangle);
  219. }
  220. /// <summary>
  221. /// Shows pop-up window below the specified area of specified control.
  222. /// </summary>
  223. /// <param name="control">The control used to compute screen location of specified area.</param>
  224. /// <param name="area">The area of control below which the pop-up will be shown.</param>
  225. /// <remarks>
  226. /// When there is no space below specified area, the pop-up control is shown above it.
  227. /// </remarks>
  228. /// <exception cref="T:System.ArgumentNullException"><paramref name="control"/> is <code>null</code>.</exception>
  229. public void Show(Control control, Rectangle area)
  230. {
  231. if (control == null)
  232. {
  233. throw new ArgumentNullException("control");
  234. }
  235. SetOwnerItem(control);
  236. resizableTop = resizableRight = false;
  237. Point location = control.PointToScreen(new Point(area.Left, area.Top + area.Height));
  238. Rectangle screen = Screen.FromControl(control).WorkingArea;
  239. if (location.X + Size.Width > (screen.Left + screen.Width))
  240. {
  241. resizableRight = true;
  242. location.X = (screen.Left + screen.Width) - Size.Width;
  243. }
  244. if (location.Y + Size.Height > (screen.Top + screen.Height))
  245. {
  246. resizableTop = true;
  247. location.Y -= Size.Height + area.Height;
  248. }
  249. location = control.PointToClient(location);
  250. Show(control, location, ToolStripDropDownDirection.BelowRight);
  251. }
  252. private const int frames = 1;
  253. private const int totalduration = 0; // ML : 2007-11-05 : was 100 but caused a flicker.
  254. private const int frameduration = totalduration / frames;
  255. /// <summary>
  256. /// Adjusts the size of the owner <see cref="T:System.Windows.Forms.ToolStrip" /> to accommodate the <see cref="T:System.Windows.Forms.ToolStripDropDown" /> if the owner <see cref="T:System.Windows.Forms.ToolStrip" /> is currently displayed, or clears and resets active <see cref="T:System.Windows.Forms.ToolStripDropDown" /> child controls of the <see cref="T:System.Windows.Forms.ToolStrip" /> if the <see cref="T:System.Windows.Forms.ToolStrip" /> is not currently displayed.
  257. /// </summary>
  258. /// <param name="visible">true if the owner <see cref="T:System.Windows.Forms.ToolStrip" /> is currently displayed; otherwise, false.</param>
  259. protected override void SetVisibleCore(bool visible)
  260. {
  261. double opacity = Opacity;
  262. if (visible && fade && focusOnOpen) Opacity = 0;
  263. base.SetVisibleCore(visible);
  264. if (!visible || !fade || !focusOnOpen) return;
  265. for (int i = 1; i <= frames; i++)
  266. {
  267. if (i > 1)
  268. {
  269. System.Threading.Thread.Sleep(frameduration);
  270. }
  271. Opacity = opacity * (double)i / (double)frames;
  272. }
  273. Opacity = opacity;
  274. }
  275. private bool resizableTop;
  276. private bool resizableRight;
  277. private void SetOwnerItem(Control control)
  278. {
  279. if (control == null)
  280. {
  281. return;
  282. }
  283. if (control is Popup)
  284. {
  285. Popup popupControl = control as Popup;
  286. ownerPopup = popupControl;
  287. ownerPopup.childPopup = this;
  288. OwnerItem = popupControl.Items[0];
  289. return;
  290. }
  291. if (control.Parent != null)
  292. {
  293. SetOwnerItem(control.Parent);
  294. }
  295. }
  296. /// <summary>
  297. /// Raises the <see cref="E:System.Windows.Forms.Control.SizeChanged" /> event.
  298. /// </summary>
  299. /// <param name="e">An <see cref="T:System.EventArgs" /> that contains the event data.</param>
  300. protected override void OnSizeChanged(EventArgs e)
  301. {
  302. content.MinimumSize = Size;
  303. content.MaximumSize = Size;
  304. content.Size = Size;
  305. content.Location = Point.Empty;
  306. base.OnSizeChanged(e);
  307. }
  308. /// <summary>
  309. /// Raises the <see cref="E:System.Windows.Forms.ToolStripDropDown.Opening" /> event.
  310. /// </summary>
  311. /// <param name="e">A <see cref="T:System.ComponentModel.CancelEventArgs" /> that contains the event data.</param>
  312. protected override void OnOpening(CancelEventArgs e)
  313. {
  314. if (content.IsDisposed || content.Disposing)
  315. {
  316. e.Cancel = true;
  317. return;
  318. }
  319. UpdateRegion();
  320. base.OnOpening(e);
  321. }
  322. /// <summary>
  323. /// Raises the <see cref="E:System.Windows.Forms.ToolStripDropDown.Opened" /> event.
  324. /// </summary>
  325. /// <param name="e">An <see cref="T:System.EventArgs" /> that contains the event data.</param>
  326. protected override void OnOpened(EventArgs e)
  327. {
  328. if (ownerPopup != null)
  329. {
  330. ownerPopup._resizable = false;
  331. }
  332. if (focusOnOpen)
  333. {
  334. content.Focus();
  335. }
  336. base.OnOpened(e);
  337. }
  338. protected override void OnClosed(ToolStripDropDownClosedEventArgs e)
  339. {
  340. if (ownerPopup != null)
  341. {
  342. ownerPopup._resizable = true;
  343. }
  344. base.OnClosed(e);
  345. }
  346. public DateTime LastClosedTimeStamp = DateTime.Now;
  347. protected override void OnVisibleChanged(EventArgs e)
  348. {
  349. if (Visible == false)
  350. LastClosedTimeStamp = DateTime.Now;
  351. base.OnVisibleChanged(e);
  352. }
  353. #endregion
  354. #region " Resizing Support "
  355. /// <summary>
  356. /// Processes Windows messages.
  357. /// </summary>
  358. /// <param name="m">The Windows <see cref="T:System.Windows.Forms.Message" /> to process.</param>
  359. [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
  360. protected override void WndProc(ref Message m)
  361. {
  362. if (InternalProcessResizing(ref m, false))
  363. {
  364. return;
  365. }
  366. base.WndProc(ref m);
  367. }
  368. /// <summary>
  369. /// Processes the resizing messages.
  370. /// </summary>
  371. /// <param name="m">The message.</param>
  372. /// <returns>true, if the WndProc method from the base class shouldn't be invoked.</returns>
  373. [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
  374. public bool ProcessResizing(ref Message m)
  375. {
  376. return InternalProcessResizing(ref m, true);
  377. }
  378. [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
  379. private bool InternalProcessResizing(ref Message m, bool contentControl)
  380. {
  381. if (m.Msg == NativeMethods.WM_NCACTIVATE && m.WParam != IntPtr.Zero && childPopup != null && childPopup.Visible)
  382. {
  383. childPopup.Hide();
  384. }
  385. if (!Resizable)
  386. {
  387. return false;
  388. }
  389. if (m.Msg == NativeMethods.WM_NCHITTEST)
  390. {
  391. return OnNcHitTest(ref m, contentControl);
  392. }
  393. else if (m.Msg == NativeMethods.WM_GETMINMAXINFO)
  394. {
  395. return OnGetMinMaxInfo(ref m);
  396. }
  397. return false;
  398. }
  399. [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
  400. private bool OnGetMinMaxInfo(ref Message m)
  401. {
  402. NativeMethods.MINMAXINFO minmax = (NativeMethods.MINMAXINFO)Marshal.PtrToStructure(m.LParam, typeof(NativeMethods.MINMAXINFO));
  403. minmax.maxTrackSize = this.MaximumSize;
  404. minmax.minTrackSize = this.MinimumSize;
  405. Marshal.StructureToPtr(minmax, m.LParam, false);
  406. return true;
  407. }
  408. private bool OnNcHitTest(ref Message m, bool contentControl)
  409. {
  410. int x = NativeMethods.LOWORD(m.LParam);
  411. int y = NativeMethods.HIWORD(m.LParam);
  412. Point clientLocation = PointToClient(new Point(x, y));
  413. GripBounds gripBouns = new GripBounds(contentControl ? content.ClientRectangle : ClientRectangle);
  414. IntPtr transparent = new IntPtr(NativeMethods.HTTRANSPARENT);
  415. if (resizableTop)
  416. {
  417. if (resizableRight && gripBouns.TopLeft.Contains(clientLocation))
  418. {
  419. m.Result = contentControl ? transparent : (IntPtr)NativeMethods.HTTOPLEFT;
  420. return true;
  421. }
  422. if (!resizableRight && gripBouns.TopRight.Contains(clientLocation))
  423. {
  424. m.Result = contentControl ? transparent : (IntPtr)NativeMethods.HTTOPRIGHT;
  425. return true;
  426. }
  427. if (gripBouns.Top.Contains(clientLocation))
  428. {
  429. m.Result = contentControl ? transparent : (IntPtr)NativeMethods.HTTOP;
  430. return true;
  431. }
  432. }
  433. else
  434. {
  435. if (resizableRight && gripBouns.BottomLeft.Contains(clientLocation))
  436. {
  437. m.Result = contentControl ? transparent : (IntPtr)NativeMethods.HTBOTTOMLEFT;
  438. return true;
  439. }
  440. if (!resizableRight && gripBouns.BottomRight.Contains(clientLocation))
  441. {
  442. m.Result = contentControl ? transparent : (IntPtr)NativeMethods.HTBOTTOMRIGHT;
  443. return true;
  444. }
  445. if (gripBouns.Bottom.Contains(clientLocation))
  446. {
  447. m.Result = contentControl ? transparent : (IntPtr)NativeMethods.HTBOTTOM;
  448. return true;
  449. }
  450. }
  451. if (resizableRight && gripBouns.Left.Contains(clientLocation))
  452. {
  453. m.Result = contentControl ? transparent : (IntPtr)NativeMethods.HTLEFT;
  454. return true;
  455. }
  456. if (!resizableRight && gripBouns.Right.Contains(clientLocation))
  457. {
  458. m.Result = contentControl ? transparent : (IntPtr)NativeMethods.HTRIGHT;
  459. return true;
  460. }
  461. return false;
  462. }
  463. private VS.VisualStyleRenderer sizeGripRenderer;
  464. /// <summary>
  465. /// Paints the size grip.
  466. /// </summary>
  467. /// <param name="e">The <see cref="System.Windows.Forms.PaintEventArgs" /> instance containing the event data.</param>
  468. public void PaintSizeGrip(PaintEventArgs e)
  469. {
  470. if (e == null || e.Graphics == null || !resizable)
  471. {
  472. return;
  473. }
  474. Size clientSize = content.ClientSize;
  475. if (Application.RenderWithVisualStyles)
  476. {
  477. if (this.sizeGripRenderer == null)
  478. {
  479. this.sizeGripRenderer = new VS.VisualStyleRenderer(VS.VisualStyleElement.Status.Gripper.Normal);
  480. }
  481. this.sizeGripRenderer.DrawBackground(e.Graphics, new Rectangle(clientSize.Width - 0x10, clientSize.Height - 0x10, 0x10, 0x10));
  482. }
  483. else
  484. {
  485. ControlPaint.DrawSizeGrip(e.Graphics, content.BackColor, clientSize.Width - 0x10, clientSize.Height - 0x10, 0x10, 0x10);
  486. }
  487. }
  488. #endregion
  489. }
  490. }