我可以在窗体上绘图,尤其是控件吗?

发布于 2024-11-29 20:04:02 字数 237 浏览 2 评论 0原文

我可以在表单上绘制所有控件之上吗?
我的表单上有一些控件(文本框按钮COM对象),并且我希望绘制ON它们,覆盖它们之前绘制的任何像素。
我在 C# 上使用 Windows 窗体
注意:Graphics 类在控件下绘制...

Is it possible for me to draw ABOVE all controls on a form?

I have some controls (textboxes, buttons, COM objects) on my form, and I wish to draw ON them, overriding any pixels previously drawn by them.

I am using Windows Forms on C#.

NOTE: the Graphics class draws under the controls...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月下凄凉 2024-12-06 20:04:02

我知道我在游戏中已经太晚了,并且接受的答案没有任何问题,但我发现它太复杂并且难以理解,所以我想出了一个“hack”。

1.使用以下代码创建自定义Panel

public class TransparentPanel : Panel
{
    protected override void OnPaint(PaintEventArgs e)
    {
        
    }

    protected override void OnPaintBackground(PaintEventArgs e)
    {
        
    }

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT

            return cp;
        }
    }
}

2.在 Form 上当前的每个控件上创建一个与 Form 大小完全相同的 TransparentPanel,将其置于最前面,并设置其 < code>BackColor 为 TransparentEnablefalse

3.现在,您在 OnPaint 中绘制的所有内容都将绘制在任何控件的“上方”,并且不会“阻止”任何可交互的控件,例如 TextBox

I know I'm way too late in the game, and there is nothing wrong with the accepted answer, but I found it too complicated and somehow hard to understand, so I come up with a "hack" for it.

1. Make a custom Panel with the following code.

public class TransparentPanel : Panel
{
    protected override void OnPaint(PaintEventArgs e)
    {
        
    }

    protected override void OnPaintBackground(PaintEventArgs e)
    {
        
    }

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x00000020; //WS_EX_TRANSPARENT

            return cp;
        }
    }
}

2. Create a TransparentPanel exactly the same size as the Form, over every control currently on the Form, bring it to the front, and set its BackColor to Transparent, Enable to false.

3. Now everything you draw within OnPaint will be drawn "above" any controls, and it will not "block" any interactable controls such as TextBox.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文