PopupWindowHelper.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. using System;
  2. using System.Drawing;
  3. using System.Runtime.InteropServices;
  4. using System.Windows.Forms;
  5. namespace FastReport.Controls
  6. {
  7. #region Event Argument Classes
  8. /// <summary>
  9. /// Contains event information for a <b>PopupClosed</b> event.
  10. /// </summary>
  11. internal class PopupClosedEventArgs : EventArgs
  12. {
  13. /// <summary>
  14. /// The popup form.
  15. /// </summary>
  16. private Form popup = null;
  17. /// <summary>
  18. /// Gets the popup form which is being closed.
  19. /// </summary>
  20. public Form Popup
  21. {
  22. get
  23. {
  24. return this.popup;
  25. }
  26. }
  27. /// <summary>
  28. /// Constructs a new instance of this class for the specified
  29. /// popup form.
  30. /// </summary>
  31. /// <param name="popup">Popup Form which is being closed.</param>
  32. public PopupClosedEventArgs(Form popup)
  33. {
  34. this.popup = popup;
  35. }
  36. }
  37. /// <summary>
  38. /// Arguments to a <b>PopupCancelEvent</b>. Provides a
  39. /// reference to the popup form that is to be closed and
  40. /// allows the operation to be cancelled.
  41. /// </summary>
  42. public class PopupCancelEventArgs : EventArgs
  43. {
  44. /// <summary>
  45. /// Whether to cancel the operation
  46. /// </summary>
  47. private bool cancel = false;
  48. /// <summary>
  49. /// Mouse down location
  50. /// </summary>
  51. private Point location;
  52. /// <summary>
  53. /// Popup form.
  54. /// </summary>
  55. private Form popup = null;
  56. /// <summary>
  57. /// Constructs a new instance of this class.
  58. /// </summary>
  59. /// <param name="popup">The popup form</param>
  60. /// <param name="location">The mouse location, if any, where the
  61. /// mouse event that would cancel the popup occured.</param>
  62. public PopupCancelEventArgs(Form popup, Point location)
  63. {
  64. this.popup = popup;
  65. this.location = location;
  66. this.cancel = false;
  67. }
  68. /// <summary>
  69. /// Gets the popup form
  70. /// </summary>
  71. public Form Popup
  72. {
  73. get
  74. {
  75. return this.popup;
  76. }
  77. }
  78. /// <summary>
  79. /// Gets the location that the mouse down which would cancel this
  80. /// popup occurred
  81. /// </summary>
  82. public Point CursorLocation
  83. {
  84. get
  85. {
  86. return this.location;
  87. }
  88. }
  89. /// <summary>
  90. /// Gets/sets whether to cancel closing the form. Set to
  91. /// <c>true</c> to prevent the popup from being closed.
  92. /// </summary>
  93. public bool Cancel
  94. {
  95. get
  96. {
  97. return this.cancel;
  98. }
  99. set
  100. {
  101. this.cancel = value;
  102. }
  103. }
  104. }
  105. #endregion
  106. #region Delegates
  107. /// <summary>
  108. /// Represents the method which responds to a <b>PopupClosed</b> event.
  109. /// </summary>
  110. internal delegate void PopupClosedEventHandler(object sender, PopupClosedEventArgs e);
  111. /// <summary>
  112. /// Represents the method which responds to a <b>PopupCancel</b> event.
  113. /// </summary>
  114. internal delegate void PopupCancelEventHandler(object sender, PopupCancelEventArgs e);
  115. #endregion
  116. #region PopupWindowHelper
  117. /// <summary>
  118. /// A class to assist in creating popup windows like Combo Box drop-downs and Menus.
  119. /// This class includes functionality to keep the title bar of the popup owner form
  120. /// active whilst the popup is displayed, and to automatically cancel the popup
  121. /// whenever the user clicks outside the popup window or shifts focus to another
  122. /// application.
  123. /// </summary>
  124. internal class PopupWindowHelper : NativeWindow
  125. {
  126. #region Unmanaged Code
  127. [DllImport("user32", CharSet = CharSet.Auto)]
  128. private extern static int SendMessage(IntPtr handle, int msg, int wParam, IntPtr lParam);
  129. [DllImport("user32", CharSet = CharSet.Auto)]
  130. private extern static int PostMessage(IntPtr handle, int msg, int wParam, IntPtr lParam);
  131. private const int WM_ACTIVATE = 0x006;
  132. private const int WM_ACTIVATEAPP = 0x01C;
  133. private const int WM_NCACTIVATE = 0x086;
  134. [DllImport("user32")]
  135. private extern static void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
  136. private const int KEYEVENTF_KEYUP = 0x0002;
  137. #endregion
  138. #region Member Variables
  139. /// <summary>
  140. /// Event Handler to detect when the popup window is closed
  141. /// </summary>
  142. private EventHandler popClosedHandler = null;
  143. /// <summary>
  144. /// Message filter to detect mouse clicks anywhere in the application
  145. /// whilst the popup window is being displayed.
  146. /// </summary>
  147. private PopupWindowHelperMessageFilter filter = null;
  148. /// <summary>
  149. /// The popup form that is being shown.
  150. /// </summary>
  151. private Form popup = null;
  152. /// <summary>
  153. /// The owner of the popup form that is being shown:
  154. /// </summary>
  155. private Form owner = null;
  156. /// <summary>
  157. /// Whether the popup is showing or not.
  158. /// </summary>
  159. private bool popupShowing = false;
  160. /// <summary>
  161. /// Whether the popup has been cancelled, notified by PopupCancel,
  162. /// rather than closed.
  163. /// </summary>
  164. private bool skipClose = false;
  165. #endregion
  166. /// <summary>
  167. /// Raised when the popup form is closed.
  168. /// </summary>
  169. public event PopupClosedEventHandler PopupClosed;
  170. /// <summary>
  171. /// Raised when the Popup Window is about to be cancelled. The
  172. /// <see cref="PopupCancelEventArgs.Cancel"/> property can be
  173. /// set to <c>true</c> to prevent the form from being cancelled.
  174. /// </summary>
  175. public event PopupCancelEventHandler PopupCancel;
  176. /// <summary>
  177. /// Shows the specified Form as a popup window, keeping the
  178. /// Owner's title bar active and preparing to cancel the popup
  179. /// should the user click anywhere outside the popup window.
  180. /// <para>Typical code to use this message is as follows:</para>
  181. /// <code>
  182. /// frmPopup popup = new frmPopup();
  183. /// Point location = this.PointToScreen(new Point(button1.Left, button1.Bottom));
  184. /// popupHelper.ShowPopup(this, popup, location);
  185. /// </code>
  186. /// <para>Put as much initialisation code as possible
  187. /// into the popup form's constructor, rather than the <b>System.Windows.Forms.Load</b>
  188. /// event as this will improve visual appearance.</para>
  189. /// </summary>
  190. /// <param name="owner">Main form which owns the popup</param>
  191. /// <param name="popup">Window to show as a popup</param>
  192. /// <param name="location">Location relative to the screen to show the popup at.</param>
  193. public void ShowPopup(Form owner, Form popup, Point location)
  194. {
  195. this.owner = owner;
  196. this.popup = popup;
  197. // Start checking for the popup being cancelled
  198. Application.AddMessageFilter(filter);
  199. // Set the location of the popup form:
  200. popup.StartPosition = FormStartPosition.Manual;
  201. popup.Location = location;
  202. // Make it owned by the window that's displaying it:
  203. owner.AddOwnedForm(popup);
  204. // Respond to the Closed event in case the popup
  205. // is closed by its own internal means
  206. popClosedHandler = new EventHandler(popup_Closed);
  207. popup.Closed += popClosedHandler;
  208. // Show the popup:
  209. this.popupShowing = true;
  210. popup.Show();
  211. popup.Activate();
  212. // A little bit of fun. We've shown the popup,
  213. // but because we've kept the main window's
  214. // title bar in focus the tab sequence isn't quite
  215. // right. This can be fixed by sending a tab,
  216. // but that on its own would shift focus to the
  217. // second control in the form. So send a tab,
  218. // followed by a reverse-tab.
  219. // Send a Tab command:
  220. keybd_event((byte) Keys.Tab, 0, 0, 0);
  221. keybd_event((byte) Keys.Tab, 0, KEYEVENTF_KEYUP, 0);
  222. // Send a reverse Tab command:
  223. keybd_event((byte) Keys.ShiftKey, 0, 0, 0);
  224. keybd_event((byte) Keys.Tab, 0, 0, 0);
  225. keybd_event((byte) Keys.Tab, 0, KEYEVENTF_KEYUP, 0);
  226. keybd_event((byte) Keys.ShiftKey, 0, KEYEVENTF_KEYUP, 0);
  227. // Start filtering for mouse clicks outside the popup
  228. filter.Popup = popup;
  229. }
  230. /// <summary>
  231. /// Responds to the <see cref="System.Windows.Forms.Form.Closed"/>
  232. /// event from the popup form.
  233. /// </summary>
  234. /// <param name="sender">Popup form that has been closed.</param>
  235. /// <param name="e">Not used.</param>
  236. private void popup_Closed(object sender, EventArgs e)
  237. {
  238. ClosePopup();
  239. }
  240. /// <summary>
  241. /// Subclasses the owning form's existing Window Procedure to enables the
  242. /// title bar to remain active when a popup is show, and to detect if
  243. /// the user clicks onto another application whilst the popup is visible.
  244. /// </summary>
  245. /// <param name="m">Window Procedure Message</param>
  246. protected override void WndProc(ref Message m)
  247. {
  248. base.WndProc(ref m);
  249. if (this.popupShowing)
  250. {
  251. // check for WM_ACTIVATE and WM_NCACTIVATE
  252. if (m.Msg == WM_NCACTIVATE)
  253. {
  254. // Check if the title bar will made inactive:
  255. if (((int) m.WParam) == 0)
  256. {
  257. // If so reactivate it.
  258. SendMessage(this.Handle, WM_NCACTIVATE, 1, IntPtr.Zero);
  259. // Note it's no good to try and consume this message;
  260. // if you try to do that you'll end up with windows
  261. // that don't respond.
  262. }
  263. }
  264. else if (m.Msg == WM_ACTIVATEAPP)
  265. {
  266. // Check if the application is being deactivated.
  267. if ((int)m.WParam == 0)
  268. {
  269. // It is so cancel the popup:
  270. ClosePopup();
  271. // And put the title bar into the inactive state:
  272. PostMessage(this.Handle, WM_NCACTIVATE, 0, IntPtr.Zero);
  273. }
  274. }
  275. }
  276. }
  277. /// <summary>
  278. /// Called when the popup is being hidden.
  279. /// </summary>
  280. public void ClosePopup()
  281. {
  282. if (this.popupShowing)
  283. {
  284. if (!skipClose)
  285. {
  286. // Raise event to owner
  287. OnPopupClosed(new PopupClosedEventArgs(this.popup));
  288. }
  289. skipClose = false;
  290. // Make sure the popup is closed and we've cleaned
  291. // up:
  292. this.owner.RemoveOwnedForm(this.popup);
  293. this.popupShowing = false;
  294. this.popup.Closed -= popClosedHandler;
  295. this.popClosedHandler = null;
  296. this.popup.Close();
  297. // No longer need to filter for clicks outside the
  298. // popup.
  299. Application.RemoveMessageFilter(filter);
  300. // If we did something from the popup which shifted
  301. // focus to a new form, like showing another popup
  302. // or dialog, then Windows won't know how to bring
  303. // the original owner back to the foreground, so
  304. // force it here:
  305. this.owner.Activate();
  306. // Null out references for GC
  307. this.popup = null;
  308. this.owner = null;
  309. }
  310. }
  311. /// <summary>
  312. /// Raises the <see cref="PopupClosed"/> event.
  313. /// </summary>
  314. /// <param name="e"><see cref="PopupClosedEventArgs"/> describing the
  315. /// popup form that is being closed.</param>
  316. protected virtual void OnPopupClosed(PopupClosedEventArgs e)
  317. {
  318. if (this.PopupClosed != null)
  319. {
  320. this.PopupClosed(this, e);
  321. }
  322. }
  323. private void popup_Cancel(object sender, PopupCancelEventArgs e)
  324. {
  325. OnPopupCancel(e);
  326. }
  327. /// <summary>
  328. /// Raises the <see cref="PopupCancel"/> event.
  329. /// </summary>
  330. /// <param name="e"><see cref="PopupCancelEventArgs"/> describing the
  331. /// popup form that about to be cancelled.</param>
  332. protected virtual void OnPopupCancel(PopupCancelEventArgs e)
  333. {
  334. if (this.PopupCancel != null)
  335. {
  336. this.PopupCancel(this, e);
  337. if (!e.Cancel)
  338. {
  339. skipClose = true;
  340. }
  341. }
  342. }
  343. /// <summary>
  344. /// Default constructor.
  345. /// </summary>
  346. /// <remarks>Use the <see cref="System.Windows.Forms.NativeWindow.AssignHandle"/>
  347. /// method to attach this class to the form you want to show popups from.</remarks>
  348. public PopupWindowHelper()
  349. {
  350. filter = new PopupWindowHelperMessageFilter(this);
  351. filter.PopupCancel += new PopupCancelEventHandler(popup_Cancel);
  352. }
  353. }
  354. #endregion
  355. #region PopupWindowHelperMessageFilter
  356. /// <summary>
  357. /// A Message Loop filter which detect mouse events whilst the popup form is shown
  358. /// and notifies the owning <see cref="PopupWindowHelper"/> class when a mouse
  359. /// click outside the popup occurs.
  360. /// </summary>
  361. internal class PopupWindowHelperMessageFilter : IMessageFilter
  362. {
  363. private const int WM_LBUTTONDOWN = 0x201;
  364. private const int WM_RBUTTONDOWN = 0x204;
  365. private const int WM_MBUTTONDOWN = 0x207;
  366. private const int WM_NCLBUTTONDOWN = 0x0A1;
  367. private const int WM_NCRBUTTONDOWN = 0x0A4;
  368. private const int WM_NCMBUTTONDOWN = 0x0A7;
  369. /// <summary>
  370. /// Raised when the Popup Window is about to be cancelled. The
  371. /// <see cref="PopupCancelEventArgs.Cancel"/> property can be
  372. /// set to <c>true</c> to prevent the form from being cancelled.
  373. /// </summary>
  374. public event PopupCancelEventHandler PopupCancel;
  375. /// <summary>
  376. /// The popup form
  377. /// </summary>
  378. private Form popup = null;
  379. /// <summary>
  380. /// The owning <see cref="PopupWindowHelper"/> object.
  381. /// </summary>
  382. private PopupWindowHelper owner = null;
  383. /// <summary>
  384. /// Constructs a new instance of this class and sets the owning
  385. /// object.
  386. /// </summary>
  387. /// <param name="owner">The <see cref="PopupWindowHelper"/> object
  388. /// which owns this class.</param>
  389. public PopupWindowHelperMessageFilter(PopupWindowHelper owner)
  390. {
  391. this.owner = owner;
  392. }
  393. /// <summary>
  394. /// Gets/sets the popup form which is being displayed.
  395. /// </summary>
  396. public Form Popup
  397. {
  398. get
  399. {
  400. return this.popup;
  401. }
  402. set
  403. {
  404. this.popup = value;
  405. }
  406. }
  407. /// <summary>
  408. /// Checks the message loop for mouse messages whilst the popup
  409. /// window is displayed. If one is detected the position is
  410. /// checked to see if it is outside the form, and the owner
  411. /// is notified if so.
  412. /// </summary>
  413. /// <param name="m">Windows Message about to be processed by the
  414. /// message loop</param>
  415. /// <returns><c>true</c> to filter the message, <c>false</c> otherwise.
  416. /// This implementation always returns <c>false</c>.</returns>
  417. public bool PreFilterMessage(ref Message m)
  418. {
  419. if (this.popup != null)
  420. {
  421. switch (m.Msg)
  422. {
  423. case WM_LBUTTONDOWN:
  424. case WM_RBUTTONDOWN:
  425. case WM_MBUTTONDOWN:
  426. case WM_NCLBUTTONDOWN:
  427. case WM_NCRBUTTONDOWN:
  428. case WM_NCMBUTTONDOWN:
  429. OnMouseDown();
  430. break;
  431. }
  432. }
  433. return false;
  434. }
  435. /// <summary>
  436. /// Checks the mouse location and calls the OnCancelPopup method
  437. /// if the mouse is outside the popup form.
  438. /// </summary>
  439. private void OnMouseDown()
  440. {
  441. // Get the cursor location
  442. Point cursorPos = Cursor.Position;
  443. // Check if it is within the popup form
  444. if (!popup.Bounds.Contains(cursorPos))
  445. {
  446. // If not, then call to see if it should be closed
  447. OnCancelPopup(new PopupCancelEventArgs(popup, cursorPos));
  448. }
  449. }
  450. /// <summary>
  451. /// Raises the <see cref="PopupCancel"/> event.
  452. /// </summary>
  453. /// <param name="e">The <see cref="PopupCancelEventArgs"/> associated
  454. /// with the cancel event.</param>
  455. protected virtual void OnCancelPopup(PopupCancelEventArgs e)
  456. {
  457. if (this.PopupCancel != null)
  458. {
  459. this.PopupCancel(this, e);
  460. }
  461. if (!e.Cancel)
  462. {
  463. owner.ClosePopup();
  464. // Clear reference for GC
  465. popup = null;
  466. }
  467. }
  468. }
  469. #endregion
  470. }