Javed Akram
Javed Akram

Reputation: 15344

semi-transparent form but opaque Controls in C#

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.

Upvotes: 10

Views: 18733

Answers (3)

user2342062
user2342062

Reputation:

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);
}

Upvotes: 5

56ka
56ka

Reputation: 1565

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

Upvotes: 0

John Koerner
John Koerner

Reputation: 38079

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);
    }

Upvotes: 9

Related Questions