开发者

C# - Transparent modal form on a window

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 instea开发者_如何学编程d, 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:

C# - Transparent modal form on a window

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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜