9

How to make semi transparent form in C# windows form application

I have tried the TransparentKey which makes it full-transparent. and tried Opacity but it effects the whole form (with controls).

I want only form part to be semi-transparent but not Controls.

Javed Akram
  • 15,024
  • 26
  • 81
  • 118

3 Answers3

9

You can use a hatch brush with a certain percentage, for example:

    using System.Drawing.Drawing2D;

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        var hb = new HatchBrush(HatchStyle.Percent50, this.TransparencyKey);

        e.Graphics.FillRectangle(hb,this.DisplayRectangle);
    }
Assaf Lavie
  • 73,079
  • 34
  • 148
  • 203
John Koerner
  • 37,428
  • 8
  • 84
  • 134
3

I found the Hatch Brush grotesque,

Instead of:

protected override void OnPaintBackground(PaintEventArgs e) {
  var hb = new HatchBrush(HatchStyle.Percent80, this.TransparencyKey);
  e.Graphics.FillRectangle(hb, this.DisplayRectangle);
}

I used:

protected override void OnPaintBackground(PaintEventArgs e) {
  var sb = new SolidBrush(Color.FromArgb(100, 100, 100, 100));
  e.Graphics.FillRectangle(sb, this.DisplayRectangle);
}
-1

There is a solution which add semi-transparency to a Control (not Form) :

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        // Apply opacity (0 to 255)
        panel1.BackColor = Color.FromArgb(25, panel1.BackColor);
    }

In visual Studio : (alpha activated only during execution)

enter image description here

Executed on Windows 7 :

enter image description here

Executed on an old WIndows 2003 Server :

enter image description here

Source : https://stackoverflow.com/a/4464161/1529139

Community
  • 1
  • 1
56ka
  • 1,463
  • 1
  • 21
  • 37
  • The form is not semi-transparent in your example..., or do I miss something? – user1027167 Aug 26 '15 at 11:09
  • Maybe the background is so much uniform and it is not enough clear but it really works with alpha channel. If you watch closely you will see the shading :) – 56ka Aug 26 '15 at 11:17
  • 2
    I have tested and have the following result: The button1 is opaque, the panel1 is semi transparent, the form1 is opaque. You can not see, what is behind the form, but the question was about a semi-transparent form. So I have the same problem, but your answere seems to be not correct... – user1027167 Aug 26 '15 at 11:22
  • I see what you mean, you're true. In facts I've answered for a `Control` but the question is about the `Form`... I will update – 56ka Aug 26 '15 at 11:27
  • 5
    Who has upvoted this question, just after I have downvoted it? This answere should be deleted, because it leads people into the wrong direction. – user1027167 Aug 27 '15 at 10:50
  • I tried this, and it thrown an `ArgumentException` with the message that the control does not support transparent background colors. – Adam L. S. Jan 28 '22 at 08:34