Friday, October 22, 2004


I've been playing a bit with non-rectangular Windows forms (using a background image and transparency key), and have discovered some strange things.

The TransparencyKey property doesn't work
Well, this is a known issue. The TransparencyKey property doesn't work on systems where you've got your screen colour depth set to more than 24 bit. Bit of an oversight in my book, but there you go.

Quick google search cured that, though. The problem can be solved by putting the following lines of code in your form.initialize method:
Dim Img As Bitmap = Me.BackgroundImage
Img.MakeTransparent(Img.GetPixel(1, 1))
Me.BackgroundImage = Img
Me.TransparencyKey = Img.GetPixel(1, 1)

That works, but seems to have a bit of a strange side-effect.

If I add a control to the form, a ListBox, say, and set its BackColor to Black, that control becomes transparent too. Like properly transparent. I mean it looks like there's a hole in the form. Any other colour's fine, it's just black makes a hole in the form.

It only seems to happen on the form with the transparency key stuff going on. On standard forms, it doesn't happen. Black controls look just as you'd expect. Black.

It's kind of disconcerting, seeing a selected item (blue bar and everything) floating seemingly in space (well, floating with a bunch of code behind it.

If anyone can figure out why this is happening, let me know. I'm really quite interested.

Anyway, that's what I learnt this evening.

PS - Feel free not to tell me the answer if you do know. I just needed to remind myself of an interesting thing I need to read up about.

No comments: