如何在.NET/C#的右键菜单上画图? [英] How to draw on top of the right-click menu in .NET/C#?

查看:36
本文介绍了如何在.NET/C#的右键菜单上画图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个辅助技术应用程序(使用C#),该应用程序将信息覆盖在当前打开的窗口的顶部.它检测可点击的元素,并标记它们.

I'm developing an assistive technology application (in C#) that overlays information on top of the currently open window. It detects clickable elements, and labels them.

为此,我当前正在创建一个无边界的透明窗口,并将TopMost设置为"true",并在其上绘制标签.这意味着在当前应用程序的前面总是有一个窗口悬停,我可以在其上绘制标签.

To do this, I'm currently creating a borderless, transparent window with TopMost set to "true", and drawing the labels on that. This means there is always a window hovering in front of the current application, on which I can draw the labels.

问题是,此窗口不包含右键单击菜单-仅包含其他窗口.当用户单击鼠标右键时,上下文菜单将在叠加层上方绘制.

The problem is, this window doesn't cover the right-click menu - only other windows. When the user right-clicks, the context menu is drawn above the overlay.

我需要能够在右键菜单中为元素添加标签,但是在当前的实现中,我无法在其上画图.有人知道解决方案吗?

I need to be able to label elements in the right-click menu, but I can't draw on top of it with the current implementation. Does anybody know of a solution?

这是绘制覆盖图的相关代码.我已经在表单设计器中设置了表单选项,而不是在代码显式设置中设置了表单选项,因此我不确定它会提供多少帮助.我删除了与绘图或表单本身无关的代码:

This is the relevant code for drawing the overlay. I've set the form options in the form designer, not in the code explicity, so I'm not sure how much it will help. I've removed the code not related to drawing, or 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);
        }
    }    
}

推荐答案

我发现了一些黑客解决方案.

I've found a bit of a hack solution.

通过定期将叠加窗口置于最前面,我可以将其置于Z顺序的顶部,以覆盖右键单击菜单.它不是完美的,但确实有效.这是核心概念,去除了多线程:

By periodically bringing the overlay window to the front, I can bring it to the top of the Z-order where it covers the right-click menu. It's not perfect, but it does work. This is the core concept, with multithreading stripped out:

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);
        }
    }
}

(假设"OverlayForm"是在表单设计器中设置了相关标志的表单,即始终在顶部",透明键",无边距",最大化"等)

(This assumes "OverlayForm" is a form with the relevant flags set in the form designer - i.e. "Always on Top," "Transparency Key," "Borderless," "Maximised," etc.)

我仅在Windows 8.1上进行了测试.我不知道Z顺序是否在其他版本上也是如此.

I've only tested this on Windows 8.1. I don't know if the Z-order behaves this way on other versions.

这篇关于如何在.NET/C#的右键菜单上画图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆