C# - 窗口上的透明模式窗体

发布于 2024-11-16 19:31:02 字数 1504 浏览 1 评论 0原文

我想在我的应用程序中使用完全透明的模态表单,并且能够用部分透明的图像填充它;为此,我曾经从表单中删除所有可见元素并获得下面的代码。

class WinScreenshotWindow : Form
{
    public WinScreenshotWindow()
    {
        // Create from without erasing background with a color
        // Going not to use transparent form instead, it will produce context menu bugs in textboxes for child form
        this.SuspendLayout();
        this.MaximizeBox = false;
        this.MinimizeBox = false;
        this.ShowIcon = false;
        this.ShowInTaskbar = false;
        this.FormBorderStyle = FormBorderStyle.None;
        this.StartPosition = FormStartPosition.Manual;
        this.ControlBox = false;
        this.Visible = false;
        this.Size = new Size(100, 100);
        this.Location = new Point(200, 200);
        this.ResumeLayout();
    }

    protected override void OnPaintBackground(PaintEventArgs e)
    {
        // Erase Background Windows message:
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        Rectangle clientRect = e.ClipRectangle;
        e.Graphics.FillRectangle(Brushes.Transparent, clientRect);
    }
}

    static void Main()
    {
        Form form = new Form();
        form.Size = new Size(400, 400);
        form.Show();

        var ww = new WinScreenshotWindow();
        ww.ShowDialog(form);
    }

但结果很奇怪:

Bug

当我删除 OnPaint() 中的填充时,它根本不可见。 问题是——为什么会发生这种情况?如果背景是透明的,为什么它会以这种方式显示表格?在这种情况下可以做什么?

任何帮助表示赞赏。

I want to use fully transparent Modal form in my application, with being able to fill it with partially-transparent image; For this, I used to remove all visible elements from the form and got the code below.

class WinScreenshotWindow : Form
{
    public WinScreenshotWindow()
    {
        // Create from without erasing background with a color
        // Going not to use transparent form instead, it will produce context menu bugs in textboxes for child form
        this.SuspendLayout();
        this.MaximizeBox = false;
        this.MinimizeBox = false;
        this.ShowIcon = false;
        this.ShowInTaskbar = false;
        this.FormBorderStyle = FormBorderStyle.None;
        this.StartPosition = FormStartPosition.Manual;
        this.ControlBox = false;
        this.Visible = false;
        this.Size = new Size(100, 100);
        this.Location = new Point(200, 200);
        this.ResumeLayout();
    }

    protected override void OnPaintBackground(PaintEventArgs e)
    {
        // Erase Background Windows message:
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        Rectangle clientRect = e.ClipRectangle;
        e.Graphics.FillRectangle(Brushes.Transparent, clientRect);
    }
}

    static void Main()
    {
        Form form = new Form();
        form.Size = new Size(400, 400);
        form.Show();

        var ww = new WinScreenshotWindow();
        ww.ShowDialog(form);
    }

But the result is something strange:

Bug

When I remove filling in OnPaint(), it is not visible at all.
The question is - why does this happen? If the background is transparent why do it shows the form in such way? And what can be done in this situation?

Any help appreciated.

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

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

发布评论

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

评论(1

就像说晚安 2024-11-23 19:31:02

打开具有红色背景色的无边框表单并设置 TransparencyKey = red 不是更容易吗?

Wouldn't it be easier to open a borderless form with a red backcolor and set the TransparencyKey = red?

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