/// <summary> /// Ctor: takes ownership of actual context menu UI. /// </summary> /// <param name="ctxtMenuControl">The context menu UI.</param> public CtxtMenuForm(ICtxtMenuControl ctxtMenuControl) { this.ctxtMenuControl = ctxtMenuControl; SuspendLayout(); // Configure form - borderless etc. AutoScaleDimensions = new System.Drawing.SizeF(6.0F, 13.0F); AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; BackColor = ZenParams.WindowColor; ClientSize = new System.Drawing.Size(278, 244); FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; StartPosition = System.Windows.Forms.FormStartPosition.Manual; Name = "CtxtMenuForm"; ShowIcon = false; ShowInTaskbar = false; Text = "CtxtMenuForm"; TopMost = true; // Add UI control to children, resize to accommodate it. ctxtMenuControl.AsUserControl.Location = Point.Empty; Controls.Add(ctxtMenuControl.AsUserControl); ctxtMenuControl.AssumeSize(); // KK, done. //ResumeLayout(false); // No! that screws up size. Size = ctxtMenuControl.AsUserControl.Size; Application.AddMessageFilter(this); }
/// <summary> /// <para>Called by derived controls to display a context menu.</para> /// <para>Once shown, top form takes over ownership of provided control (will dispose it when closing context menu).</para> /// </summary> /// <param name="relLoc">Desired relative location, in calling control's coordinates.</param> /// <param name="ctxtMenuCtrl">The UI to show as a context menu.</param> protected void ShowContextMenu(Point relLoc, ICtxtMenuControl ctxtMenuCtrl) { Point absLoc = new Point(AbsRect.X + relLoc.X, AbsRect.Y + relLoc.Y); Point screenLoc = CurrentParentForm.WinForm.PointToScreen(absLoc); CurrentParentForm.ShowContextMenu(screenLoc, ctxtMenuCtrl); }
/// <summary> /// Closes the context menu control shopwn earlier (if it's still visible at all). /// </summary> internal void CloseContextMenu(ICtxtMenuControl ctxtMenuCtrl) { if (ctxtForm != null && ctxtMenuCtrl == ctxtForm.CtxtMenuControl) { ctxtForm.Close(); ctxtForm = null; } }
/// <summary> /// Shows a context menu at the desired location, or the nearest best place. /// </summary> /// <param name="screenLoc">The desired location in screen coordinates.</param> /// <param name="ctxtMenuCtrl">The context menu UI to show.</param> internal void ShowContextMenu(Point screenLoc, ICtxtMenuControl ctxtMenuCtrl) { // If we currently have a context menu on the screen, kill it. if (ctxtForm != null) { ctxtForm.Close(); ctxtForm = null; } // Create new form var ff = new CtxtMenuForm(ctxtMenuCtrl); // Calculate optimal location. // Horizontally: centered around pointer // Vertically: prefer above, with 3px in between Screen scr = getScreenForPoint(screenLoc); int x = screenLoc.X - ff.Width / 2; if (x < scr.WorkingArea.Left) { x = scr.WorkingArea.Left; } else if (x + ff.Width > scr.WorkingArea.Right) { x = scr.WorkingArea.Right - ff.Width; } int y = screenLoc.Y - ff.Height - 3; if (y < scr.WorkingArea.Top) { y = screenLoc.Y + 3; } ff.Location = new Point(x, y); // Show context menu ff.Show(form); form.Focus(); ctxtForm = ff; }
/// <summary> /// Closes the context menu control shopwn earlier (if it's still visible at all). /// </summary> protected void CloseContextMenu(ICtxtMenuControl ctxtMenuCtrl) { CurrentParentForm.CloseContextMenu(ctxtMenuCtrl); }
/// <summary> /// Shows a context menu at the desired location, or the nearest best place. /// </summary> /// <param name="screenLoc">The desired location in screen coordinates.</param> /// <param name="ctxtMenuCtrl">The context menu UI to show.</param> internal void ShowContextMenu(Point screenLoc, ICtxtMenuControl ctxtMenuCtrl) { // If we currently have a context menu on the screen, kill it. if (ctxtForm != null) { ctxtForm.Close(); ctxtForm = null; } // Create new form var ff = new CtxtMenuForm(ctxtMenuCtrl); // Calculate optimal location. // Horizontally: centered around pointer // Vertically: prefer above, with 3px in between Screen scr = getScreenForPoint(screenLoc); int x = screenLoc.X - ff.Width / 2; if (x < scr.WorkingArea.Left) x = scr.WorkingArea.Left; else if (x + ff.Width > scr.WorkingArea.Right) x = scr.WorkingArea.Right - ff.Width; int y = screenLoc.Y - ff.Height - 3; if (y < scr.WorkingArea.Top) y = screenLoc.Y + 3; ff.Location = new Point(x, y); // Show context menu ff.Show(form); form.Focus(); ctxtForm = ff; }