How to draw a top right mouse button menu in .NET / C #?

I am developing an assistive technology application (in C #) that overlays information on top of the current open window. It detects and marks interactive elements.

To do this, I am currently creating a transparent transparent window without a frame with TopMost set to "true" and drawing labels. This means that before the current application there is always a window on which I can draw labels.

The problem is that this window does not cover the right-click menu - only other windows. When the user right-clicks, a context menu is drawn above the overlay.

I need to be able to mark items in the context menu, but I cannot draw on top of it with the current implementation. Does anyone know of a solution?

Edit: this is the appropriate code for drawing an overlay. I set the form parameters in the form designer, not in the code explication, so I'm not sure how much this will help. I deleted the code that is not related to the drawing, or to the form itself:

public partial class OverlayForm : Form { public OverlayForm() { } protected override void OnPaint(PaintEventArgs eventArgs) { base.OnPaint(eventArgs); Graphics graphics = eventArgs.Graphics; Brush brush = new SolidBrush(this.labelColor); foreach (ClickableElement element in this.elements) { Region currentRegion = element.region; graphics.FillRegion(brush, currentRegion); } } } 
+5
source share
2 answers

I found a bit of a hacked solution.

By periodically rotating the overlay window forward, I can bring it to the top of the Z-order, where it closes the right-click menu of the mouse. It is not perfect, but it really works. This is a basic concept with a multi-threaded section:

 public class OverlayManager { OverlayForm overlay; public OverlayManager() { this.overlay = new OverlayForm(); this.overlay.Show(); this.RepeatedlyBringToFront(); } private void RepeatedlyBringToFront() { while (true) { this.overlay.BringToFront(); Thread.Sleep(50); } } } 

(It is assumed that "OverlayForm" is a form with the corresponding flags set in the form designer, that is, "Always on top", "Transparency key", "Borderless", "Maximized", etc.).

I tested this only on Windows 8.1. I do not know if the Z-order leads to other versions.

0
source

Add a context menu to your form and then assign it in the control properties in ContextMenuStrip.

 ContextMenu cm = new ContextMenu(); cm.MenuItems.Add("Item 1"); cm.MenuItems.Add("Item 2"); pictureBox1.ContextMenu = cm; 
0
source

All Articles