Windows Develop Bookmark and Share   
 index > Windows Forms General > Specific Element Opacity
 

Specific Element Opacity

Hi

Using Visual C#, how would one go about making the background an opacity set at something below 100 (e.g. 85%) while having everything else in the form retain 100% opacity?

For my specific problem, I am building a screen saver in which I want to have the black background at 85% opacity but the image I have floating around the screen should stay at 100%. If I use the opacity property under the whole form, everything becomes more opaque. Is there anything specific way of doing this, or is there a trick about this? This isn't a huge problem, but I haven't been able to find anything on the web yet that has aided me.

Thanks in advance!

Lil_ Quaty  Sunday, March 23, 2008 1:29 AM

Hi Lil' Quaty,

Based on my investigation, you cannot make a control which insides a transparent Form in a .Net program.

However, I have an idea to figure your issue out, that is to create another form (assume Form2) to show the control, Form2 is topmost window, it has no title bar, no border, it do not show in the task bar, and contains a picture box. Please check the code snippets below.

Code Snippet

Transparent_Form_Demo

public partial class Form1 : Form

{

private Point f2_location;

private Form2 f2;

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{//start here

this.Opacity = 0.50;

this.BackColor = Color.Black;

f2 = new Form2();

f2.Show();

f2.Opacity = 1.0;

f2.ShowIcon = false;

f2.ShowInTaskbar = false;

f2.FormBorderStyle = FormBorderStyle.None;

f2_location = this.Location;

f2_location.Offset(new Point(10, 30));

f2.Location = f2_location;

}

private void Form1_Move(object sender, EventArgs e)

{//move the form2 location when form1 was moved

if (f2 != null)

{

f2_location = this.Location;

f2_location.Offset(new Point(10, 30));

f2.Location = f2_location;

f2.TopMost = true;

}

}

}

Code Snippet

Transparent_Form_Demo

public partial class Form2 : Form

{

public Form2()

{

InitializeComponent();

pictureBox1.Image = Image.FromFile("F:\\avato2r3.jpg");

pictureBox1.Location = new Point(0, 0);

pictureBox1.Size = pictureBox1.Image.Size;

//this.Size = pictureBox1.Size;

}

}

Regards,

Xun

Xun Ye  Wednesday, March 26, 2008 3:49 AM

Hi Lil' Quaty,

Based on my investigation, you cannot make a control which insides a transparent Form in a .Net program.

However, I have an idea to figure your issue out, that is to create another form (assume Form2) to show the control, Form2 is topmost window, it has no title bar, no border, it do not show in the task bar, and contains a picture box. Please check the code snippets below.

Code Snippet

Transparent_Form_Demo

public partial class Form1 : Form

{

private Point f2_location;

private Form2 f2;

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{//start here

this.Opacity = 0.50;

this.BackColor = Color.Black;

f2 = new Form2();

f2.Show();

f2.Opacity = 1.0;

f2.ShowIcon = false;

f2.ShowInTaskbar = false;

f2.FormBorderStyle = FormBorderStyle.None;

f2_location = this.Location;

f2_location.Offset(new Point(10, 30));

f2.Location = f2_location;

}

private void Form1_Move(object sender, EventArgs e)

{//move the form2 location when form1 was moved

if (f2 != null)

{

f2_location = this.Location;

f2_location.Offset(new Point(10, 30));

f2.Location = f2_location;

f2.TopMost = true;

}

}

}

Code Snippet

Transparent_Form_Demo

public partial class Form2 : Form

{

public Form2()

{

InitializeComponent();

pictureBox1.Image = Image.FromFile("F:\\avato2r3.jpg");

pictureBox1.Location = new Point(0, 0);

pictureBox1.Size = pictureBox1.Image.Size;

//this.Size = pictureBox1.Size;

}

}

Regards,

Xun

Xun Ye  Wednesday, March 26, 2008 3:49 AM
Xun

Looks good on paper, but I'll have to try to implement it. However, the whole concept looks viable, so thank you! I always appreciate getting input from others such as yourself.


Lil_ Quaty  Wednesday, March 26, 2008 5:16 AM

You can use google to search for other answers

Custom Search

More Threads

• I feel dumb, but...
• Location
• How to read XUL UI ???
• ToolTipText doesn’t appear in UserControl
• How can you view controls beneath a partially transparent control?
• Duplicate Controls to New Panel
• Issue with Custom Cursors
• Small problem (MDI form, ToolStripContainer) but very urgent
• C# Window label Text
• formating an excel column when exporting a data grid