How to make a transparent form, while keeping control fully visible?

I would like to have a form in which the form's controls are fully visible, but the form itself is invisible. If I change the shape of the Opacity , this makes the shape and controls translucent, so this does not work.

I cannot do this by setting the TransparencyKey form since I have a PictureBox form. If the image in the PictureBox contains pixels corresponding to the TransparencyKey , they are displayed as open in the form that I do not want.

+4
source share
2 answers

TransparencyKey is the only way to get this. Choose the right color. Color.Fuchsia has a long tradition of color selection starting in the early days of Win32. Put an eye on him to see his virtues.

+2
source

With the warning that I never used it, I just stumbled upon it once, thought "carefully!". and moved on ...

Take a look at System.Drawing.Drawing2D.GraphicsPath and set the Region property. I added two buttons to the main Windows forms application:

 public Form1() { InitializeComponent(); Rectangle r1 = new Rectangle(button1.Location, button1.Size); Rectangle r2 = new Rectangle(button2.Location, button2.Size); GraphicsPath gp = new GraphicsPath(); gp.AddRectangle(r1); gp.AddRectangle(r2); this.Region = new Region(gp); } 

I approximated the shape of a button with a rectangle; with this code you can see the background color of the form in the corners of the buttons. You will need to develop a path for each of your controls and add them to the path individually. You will need to consider any offset introduced by the form's title bar or border style.

Update: I did some investigation and several possible approaches to the problem:

  • Using the GraphicsPath method, set pictureBox.Visible to False if the image is not loaded.
  • When you load an image into the image window, analyze the image to get a list of all the colors in it, and then arbitrarily create one that is not. Set the BackColor and TransparencyKey form properties to match this new color, answer> .
+2
source

Source: https://habr.com/ru/post/1309164/


All Articles