Groups | Blog | Home
all groups > dotnet drawing api > february 2004 >

dotnet drawing api : Semitransparent areas


Armin Zingler
2/10/2004 5:16:03 PM
Hi again,

I've got another transparency problem.
Q: How can I make _parts_ of a Form _semi_transparent?

- Opacity affects the _whole_ Form.
- The TransparencyKey makes areas _completely_ transparent.

So both are not what I am looking for. The following example uses the alpha
channel:

Private Sub Form1_Load( _
ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load

setstyle(ControlStyles.AllPaintingInWmPaint, True)
setstyle(ControlStyles.ResizeRedraw, True)
setstyle(ControlStyles.UserPaint, True)
End Sub


Protected Overrides Sub OnPaintBackground( _
ByVal pevent As System.Windows.Forms.PaintEventArgs)
'MyBase.OnPaintBackground(pevent)
End Sub

Protected Overrides Sub OnPaint( _
ByVal e As System.Windows.Forms.PaintEventArgs)

Dim b As SolidBrush
Dim r1, r2 As Rectangle

b = New SolidBrush(Color.FromArgb(128, 255, 0, 0))
r1 = Me.ClientRectangle
r1.Width \= 2
r2 = r1
r2.X = r1.Width

e.Graphics.FillRectangle(Brushes.Red, r1)
e.Graphics.FillRectangle(b, r2)

b.Dispose()
End Sub

Now the right half is semitransparent so that the window beneath shines
through, but if I move the Form, the windows beneath are not painted,
so the result of the right half is not always a mixture of the window(s)
beneath and the Form content. I know this happens because my Form is
not repainted when moved. I'd like to have the same effect as when
using the Opacity property, i.e. everything is repainted to get a
mixture result. I've also played with the SupportsTransparentBackColor style
but it didn't help also.

(How) can this be solved?


Armin
Bob Powell [MVP]
2/10/2004 5:46:44 PM
The only way to do this is using p/invoke and using the UpdateLayeredWindow
method. This enables you to create a per-pixel alpha image with varying
levels of transparency and display it on screen.

The CreateParams can be modified to use the WS_EX_LAYERED style.

I'm afraid I don't have any demo code for this as yet.

--
Bob Powell [MVP]
C#, System.Drawing

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/gdiplus_faq.htm

Read my Blog at http://bobpowelldotnet.blogspot.com

[quoted text, click to view]

Armin Zingler
2/10/2004 5:51:10 PM
"Bob Powell [MVP]" <bob@_spamkiller_bobpowell.net> schrieb
[quoted text, click to view]

Thanks a lot! I'll have a look at it.


--
Armin
AddThis Social Bookmark Button