C#-窗口上的透明模态形式

C#-窗口上的透明模态形式,c#,forms,graphics,transparency,C#,Forms,Graphics,Transparency,我想在我的应用程序中使用完全透明的模态形式,能够用部分透明的图像填充它;为此,我从表单中删除所有可见元素,并获得下面的代码 class WinScreenshotWindow : Form { public WinScreenshotWindow() { // Create from without erasing background with a color // Going not to use transparent form instea

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

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);
    }
但结果有些奇怪:

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


非常感谢您的帮助。

打开一个红色背景的无边框窗体并将透明键设置为红色不是更容易吗?

不绘制背景会给您带来随机像素。使用透明键。这很酷,但我需要使用一种颜色作为透明键。TransparencyKey=颜色。透明度不起作用。如果我有一个带有透明键颜色的图像怎么办?@Zelzer如果颜色与你的键匹配,你会冒在图像上打洞的风险。这里的建议是试着选择一种很少使用的颜色,比如青柠绿或粉红色(我想,根据图片的不同,这种颜色很少见)。