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

时间:2011-06-24 15:37:43

标签: c# forms graphics transparency

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

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()中的填充时,它根本不可见。 问题是 - 为什么会发生这种情况?如果背景是透明的,为什么它会以这种方式显示表单?在这种情况下可以做些什么?

任何帮助表示感谢。

1 个答案:

答案 0 :(得分:1)

打开带有红色背景的无边框表单并将TransparencyKey设置为红色会不会更容易?