Do Windows Forms not support object transparency?

AverageWriter

Member
Joined
Dec 30, 2021
Messages
10
Programming Experience
Beginner
Encountered a very curious thing- I've set up some images on my form, picked a background color I don't use much, and set the form's TransparencyKey to that color, which I had assumed would make anything on that form... you know... transparent.
What it's doing instead is cutting little holes in the form itself so that whatever is behind the window (such as the desktop) shows through. Creating a control and placing it between the images and the form doesn't seem to work.
 
The TransparencyKey is working as expected. It is a property of the form. Anything that color on the form will show the windows behind the form.

 
The TransparencyKey is working as expected. It is a property of the form. Anything that color on the form will show the windows behind the form.


You're absolutely right. I had thought about creating a form that sat UNDER the first form, but layering controls doesn't make them transparent with each other.
I really have a lot to learn.
 
As suggested, what you're seeing is exactly what you should be seeing based on what you did. This is an example of why you should ALWAYS read the documentation. That goes double if you try something and it doesn't work as you expect. Microsoft spent thousands of man-hours writing documentation for a reason. Don't just ignore it and then ask others questions that that documentation answers. It won't answer every question but it will answer many, so ALWAYS consult it first. You can get to specific topics simply by using the F1 key in VS but you should also bookmark/favourite the home page in your browser. The VS Help menu will take you directly to the top-level home page but it's then a bit of rooting around to get to the class library reference home page.

So, the question you actually should be asking, and thus describing, is how to do what you actually want to do. Windows Forms does have very limited transparency support. If you're expecting a single control to show what's behind part of it then you will be sadly disappointed. All that is possible at the control level is a fake transparency where a control will effectively draw the area of its parent that it is obscuring in its background and the degree to which that is visible is determined by the Opacity of that control. Because you can't actually see through the control, you won't see any sibling controls that you place between the control and its parent.

If you want more than that then you don't want Windows Forms, which was originally released in 2002 and just can't do what you want. WPF was built from the ground up with advanced functionality like transparency in mind. That's what you'll need to use.
 
As suggested, what you're seeing is exactly what you should be seeing based on what you did. This is an example of why you should ALWAYS read the documentation. That goes double if you try something and it doesn't work as you expect. Microsoft spent thousands of man-hours writing documentation for a reason. Don't just ignore it and then ask others questions that that documentation answers. It won't answer every question but it will answer many, so ALWAYS consult it first. You can get to specific topics simply by using the F1 key in VS but you should also bookmark/favourite the home page in your browser. The VS Help menu will take you directly to the top-level home page but it's then a bit of rooting around to get to the class library reference home page.

So, the question you actually should be asking, and thus describing, is how to do what you actually want to do. Windows Forms does have very limited transparency support. If you're expecting a single control to show what's behind part of it then you will be sadly disappointed. All that is possible at the control level is a fake transparency where a control will effectively draw the area of its parent that it is obscuring in its background and the degree to which that is visible is determined by the Opacity of that control. Because you can't actually see through the control, you won't see any sibling controls that you place between the control and its parent.

If you want more than that then you don't want Windows Forms, which was originally released in 2002 and just can't do what you want. WPF was built from the ground up with advanced functionality like transparency in mind. That's what you'll need to use.

I haven't gotten to that part of the documentation yet. I'm on Chapter 5 (page 109). They haven't touched on graphics yet- we just finished arrays.
 
I haven't gotten to that part of the documentation yet. I'm on Chapter 5 (page 109).
That's not the documentation. That is your course material. The documentation is what was linked to in post #2. If you had selected the TransparencyKey property in the code editor or the Properties window and pressed F1 then you'd have been taken directly to that page. Like I said, you should also bookmark the home page in your browser so that you can search for whatever you want, whenever you want. Here is the Australian version. You can change the culture if you need something else. Like I said, the VS help menu will take you here and a few clicks from there will take you to the previous page I linked to. The VS Help menu has led to the documentation for almost two decades now and similar Help has been a thing in Windows for decades more.
 
Back
Top Bottom