Link to home
Start Free TrialLog in
Avatar of jonorossi
jonorossi

asked on

Transparent Windows Forms Controls

You probably thought this is one of those questions that would be easy to answer with a no, you cannot make them. However, I know that it is not easy to do but I was trying to think of a way to get them to work. Is there a way to capture the region behind the control and create an Image so that it can be draw onto the background of the control that is sitting on top? All suggestions will be appreciated.
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Have you considered hosting the control on a borderless form that floats over the main form?  Then you can set the opacity of the floating form and the image of the form below will show thru.  Of course you then need to handle main form movements so that the floating window will be moved as wel...
Avatar of jonorossi
jonorossi

ASKER

Well that isn't really practical with several controls and will have problems because once you click on the main form the borderless transparent form is going to loose focus and drop behind the main form, it would also be noticeable because the main form titlebar would show that it doesn't have focus. Thanks for your suggestion.
You didn't specify that you needed user interaction with the control.

Give a little more detail on what you are trying to accomplish...  =)
>Give a little more detail on what you are trying to accomplish...  =)

Im trying to make "Transparent Windows Forms Controls" ;)

Do do you if there are any methods in GDI+ that allow you to capture a region of another control? That wouldn't really work well because how would you know what control is under the one you want to make transparent and also what if there is too or three controls.
Sorry...as far as I know there isn't any way to accomplish what you want without "layering" the controls in windows as I described above.

Maybe someone will come along and educate us both on how this can be done!
Are you making your own controls or trying to modify existing ones?  For example are you trying to make your own textbox?
I was looking at using the standard Windows Forms control but if it can be achieved by making a new label, picturebox, etc; then I'd like to hear it.
I intend to close this question because no answer has come out of it other than Idle_Mind saying it cannot be done. It would have been nice for RobertRFreeman to reply because he might have had a solution.
Take a look at Owner Drawn (UserPaint) controls:

http://addressof.com/blog/articles/282.aspx

or

http://www.dotnetrix.co.uk/tabcontrols.html

By default if you don't paint over something it will still be visible beneath.
Thanks for that Robert; the first article is pretty good however I cannot see it working with every Windows forms control and it would also take heaps too much time to create every single control. I couldn't work out why you pointed me to the second link.

At the moment i am setting the parent of each control to a picturebox. I have a panel control sitting over the top of a picturebox and set its parent so it becomes transaparent. Then when i drop controls onto the panel their background autoselects transaparent so labels all look transparent. To make transparent controls all you have to do is to set the parent to the control behind then reset the location because it wont look the same in the designer (reason for using panels)

I really wonder why Microsoft choose to do windows forms controls like this because so many apps need transparency.

Thanks for your time, if there isn't anything else to add I'll award the points
The second link has a section called: A Completely OwnerDraw TabControl.
It is a similar example of doing Owner Drawing for a tab control.

I checked a little more and it seems like the transparancy in all cases only copies the background of the parent control, it isn't true transparency since it will overwrite any text or other controls on the parent (between the parent background and the transparent-like control).

To get true transparency, you would need to override the paint commands and save the background rectangle before drawing over it.  Then you would also need to recapture the rectangle each time the control moves/resizes or the parent redraws.
I agree that Microsoft should do a redesign and include this as standard.
> "save the background rectangle before drawing over it."

This is what i was asking for in my question post. How do I do this?
I just found this in a project, I haven't tried it but I'll post it because this might be one way to get it to work:

this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
"ControlStyles.SupportsTransparentBackColor" works for some controls but it only makes the part of the control where the BackColor is displayed transparent.  Furthermore, it isn't true transparency.  Only the portion directly under the control that is on the PARENT form is displayed in the transparent region.  If you overlap controls they do not properly display the control underneath but only the PARENT control that would be directly underneath...
So "ControlStyles.SupportsTransparentBackColor" is the same as setting the backcolor to transparent?
Essentially...yes:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformscontrolstylesclasstopic.asp

    "If the SupportsTransparentBackColor bit is set to true, and the BackColor is set to a color whose alpha component is less than 255, OnPaintBackground will simulate transparency by asking its parent control to paint the background. This is not true transparency."

    "Note   If there is another control between the control and its parent, the current control will not show the control in the middle."
Thanks for that Idle_Mind, just trying to explore all possibilities. I don't really think there is a good way to do this but I'll wait to see what Robert's reply is.
ASKER CERTIFIED SOLUTION
Avatar of RobertRFreeman
RobertRFreeman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks but I cannot get it working. When I move it around in the designer I can see the control below from when i first clicked it (it doesn't update) but once i drop it back on the form in a position it just shows black; it is also black at runtime.
I tested using VS 2005 and win XP (themes enabled).

You can look up more articles on this by doing a search on OptimizedDoubleBuffer
And WS_EX_TRANSPARENT.  If you tweak enough of these windows api settings and override/handle enough you may be able to create an acceptable transparency.  If not, you will need to create your own controls from scratch or purchase third party control package.

Here are some other transparent control articles:
http://www.codeguru.com/cpp/controls/listbox/colorlistboxes/article.php/c10413/
http://www.codeproject.com/cs/miscctrl/alphablendtextbox.asp
Thanks for that Robert, I also tested it using VS 2005 and WinXP w/themes enabled. I'll have to have a better look into all the Windows API window settings when i get some more time on my hands. For now I'm using going to use the parent property of the control and set the location.

Thanks for all your help.
Regards, Jono