PopupWindow.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. using System.Windows.Forms;
  2. using System.Drawing;
  3. using FastReport.Forms;
  4. namespace FastReport.Controls
  5. {
  6. /// <summary>
  7. /// Represents a popup form.
  8. /// </summary>
  9. /// <remarks>
  10. /// Use this form if you want to show some controls in non-modal borderless form that
  11. /// behaves like other standard popup controls such as context menu. This form does not
  12. /// move a focus from the parent form.
  13. /// </remarks>
  14. public class PopupWindow : BaseForm
  15. {
  16. private Form ownerForm;
  17. private PopupWindowHelper popupHelper;
  18. /// <summary>
  19. /// Shows the form.
  20. /// </summary>
  21. /// <param name="ctl">The control which location is used as a reference for <b>pt</b> parameter.</param>
  22. /// <param name="pt">The location relative to the <b>ctl</b> control.</param>
  23. public void Show(Control ctl, Point pt)
  24. {
  25. Show(ctl.PointToScreen(pt));
  26. }
  27. /// <summary>
  28. /// Shows the form.
  29. /// </summary>
  30. /// <param name="ctl">The control which location is used as a reference for <b>x</b>, <b>y</b> parameters.</param>
  31. /// <param name="x">The x position relative to the <b>ctl</b> control.</param>
  32. /// <param name="y">The y position relative to the <b>ctl</b> control.</param>
  33. public void Show(Control ctl, int x, int y)
  34. {
  35. Show(ctl, new Point(x, y));
  36. }
  37. /// <summary>
  38. /// Shows the form.
  39. /// </summary>
  40. /// <param name="pt">The absolute screen location.</param>
  41. public void Show(Point pt)
  42. {
  43. Rectangle area = Screen.GetWorkingArea(pt);
  44. if (pt.X + Width > area.Right)
  45. pt.X = area.Right - Width;
  46. if (pt.Y + Height > area.Bottom)
  47. pt.Y = area.Bottom - Height;
  48. popupHelper.ShowPopup(ownerForm, this, pt);
  49. }
  50. /// <summary>
  51. /// Initializes a new instance of the <see cref="PopupWindow"/> class with default settings.
  52. /// </summary>
  53. /// <param name="ownerForm">The main form that owns this popup form.</param>
  54. public PopupWindow(Form ownerForm) : base()
  55. {
  56. this.ownerForm = ownerForm;
  57. popupHelper = new PopupWindowHelper();
  58. popupHelper.AssignHandle(ownerForm.Handle);
  59. popupHelper.PopupCancel += PopupCancel;
  60. FormBorderStyle = FormBorderStyle.FixedToolWindow;
  61. ControlBox = false;
  62. StartPosition = FormStartPosition.Manual;
  63. ShowInTaskbar = false;
  64. }
  65. /// <summary>
  66. /// Handler which allows to prevent canceling of popup window
  67. /// </summary>
  68. /// <param name="sender">Popup helper</param>
  69. /// <param name="e">Event arguments</param>
  70. protected virtual void PopupCancel(object sender, PopupCancelEventArgs e)
  71. {
  72. }
  73. }
  74. }