2
votes

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?

1
I'm confused. Doesn't PNG offer built-in support for transparency? - 500 - Internal Server Error
Afaik, layered windows are just bitmaps drawn with alpha channel on the screen. Any controls have to be customly drawn to this bitmap. If you have solid sections in the bitmap, you're better to go with a non-layered "content" form over the layered one, moving with it. The content form can have controls like any other. Example here: codeproject.com/Articles/20758/Alpha-Blended-Windows-Forms - Ray
Yes but when using a background image for forms the transparency on PNG images are disregarded, hence the usage of transparency keys, which is ugly. - avgcoder
I know that, you have no other choice than layered windows to have an alpha channel form. But this comes with the disadvantage of controls not being usable with it. Have a look at my edit of my first comment to combine both worlds of layered windows and control-compatible windows. - Ray
Should this be tagged windows-forms? It's not clear. - heltonbiker

1 Answers

4
votes

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.