semi-transparent form but opaque Controls in C#
Asked Answered
G

3

10

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.

Gingery answered 5/3, 2011 at 2:54 Comment(0)
N
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);
    }
Neurotomy answered 5/3, 2011 at 3:10 Comment(1)
Oh my. The result is disgusting. Sorry, but it truly is.Burack
N
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);
}
Navaho answered 21/11, 2017 at 9:58 Comment(3)
Perfect, your answer was the only one that served meMecklenburg
Is it for WPF cause I am not able to find the event as OnPaintBackground in windows formsMaidservant
Yes, this WPF not winforms. why use winforms when you can use wpf?Navaho
T
-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://mcmap.net/q/271559/-how-can-i-set-the-opacity-or-transparency-of-a-panel-in-winforms

Talented answered 18/8, 2015 at 14:58 Comment(6)
The form is not semi-transparent in your example..., or do I miss something?Ruelu
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 :)Talented
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...Ruelu
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 updateTalented
Who has upvoted this question, just after I have downvoted it? This answere should be deleted, because it leads people into the wrong direction.Ruelu
I tried this, and it thrown an ArgumentException with the message that the control does not support transparent background colors.Isobar

© 2022 - 2024 — McMap. All rights reserved.