Reputation: 392
For quite sometime now I have been trying to accomplish some sort of form which allows transparency with PNG images.
I am working in C# and I was able to find a nice project that achieved this via per pixel alpha blend and by changing the image to a bitmap and using an alpha layer to only show parts which aren't transparent, which is then updated with UpdateLayeredWindow.
Here is the project: http://www.codeproject.com/Articles/1822/Per-Pixel-Alpha-Blend-in-C
I was able to implement the code from the aforementioned project but due to the create parameters which is set the form is basically made invisible and only the image is shown. And when I remove the create parameters the effect is not applied.
But I am wanting to be able to apply this to my form background. For my form I have set the form border style to none and applied a background image.
How can I apply this technique to my form background and still be able to add controls which will not be affected?
Upvotes: 2
Views: 3271
Reputation: 8834
You're using the technique of layered windows to be able to have a background with alpha-transparency. This comes with the disadvantage of not being able to see controls on it. That's because the layered window is just an image. If you put controls on it, you will still get their events and messages, but to actually see them, you would have to draw all the controls on this bitmap and keep it up to date with the controls state. Tedious.
There's no other way to get an alpha-form though (at least not a good one I'm aware of).
If you're just caring about an alpha-transparent border of the form, and the client / inner part of the form being solid, you should think about creating a second "content" form, above the layered window which makes up the border, and move it with the layered window. Thus, you can combine the best of both worlds.
An example is seen here: http://www.codeproject.com/Articles/20758/Alpha-Blended-Windows-Forms. It also discusses typical traps when implementing such 2-form-fashion and how to avoid them.
Afaik, Office 2013 and VS 2013 do it in a similar fashion (with WPF-like powered drawing though), notice all the nice bugs their window shadow has.
Another choice, if you can choose it: Use WPF. Since the control drawing is completely rendered by your graphics card which renders it to a single graphics buffer, it can turn such buffer into the bitmap of a layered window and thus keep it up to date with the controls perfectly. I've never done that though since I'm not a WPF programmer, but I've seen this in action and bet there are some nice commenters giving links to good tutorials.
Upvotes: 4