0

I have a layered window (using SetLayeredWindowAttributes). If I set the alpha on the whole window to 128 and draw black rectangles on it, it works as expected (black rectangles that show through at about 50% opacity).

If, however, I set the aplha on the whole window (when I call SetLayeredWindowAttributes to 255) and then draw black rectangles with a brush that is set to 50%, they black rectangles are purely solid, no translucency whatsoever.

I was under the impression that once I had a layered window in this fashion that I could draw into it with varying levels of per-pixel alpha, so that these two would draw two levels of opacity:

User32.SetLayeredWindowAttributes(this.Handle, (uint) TransparentColorKey.ToArgb(), 255, User32.LWA_ALPHA | User32.LWA_COLORKEY);

e.Graphics.FillRectangle( new SolidBrush(Color.FromArgb(200, 40, 40, 40)), myRect);
e.Graphics.FillRectangle( new SolidBrush(Color.FromArgb( 25, 40, 40, 40)), myRect);

BUT both those rectangles have the same level of opacity (I'd expect one at 200, one at 25). I have control of the opacity ONLY for the entire window as a whole (the 255 in the SetLayeredWindowAttributes).

What am I missing here so that I can draw different elements, even just rectangles, at varying levels of alpha translucency?

BTW, I want SetLayeredWindowAttributes (rather than UpdateLayeredWindow) as I'm doing active drawing into it with device contexts. The latter approach requires a bitmap drawn up and provided in one shot. Thanks! Dave

Dave
  • 1,521
  • 17
  • 31

1 Answers1

1

I don't know where you're getting the idea that SetLayeredWindowAttributes should allow you to draw alpha data directly to your window. From the MSDN documentation it only seems to allow constant alpha or color keying. The only way to get per-pixel alpha is with UpdateLayeredWindow.

Esme Povirk
  • 3,004
  • 16
  • 24
  • That's pretty much what I arrived at as well, so switched to UpdateLayeredWindow, and it works. Unfortunate though. – Dave Jan 10 '13 at 21:06