Draw Transparent Rectangle

PROKA

Well-known member
Joined
Sep 3, 2003
Messages
249
Location
Bucharest
I know how to draw rectangles. The problem is that when I draw another rectangle, it doesnt show whats behind him ( the old rectangles ). How can I draw a transparent rectangle

See attached files (VB.NET)
 
Im not sure exactly what you are looking for but you can fill rectangles with semi-transparent colors by using the Color.FromARGB function, specifying a value from 0 to 255 for opacity, red, green and blue values and then call Graphics.FillRectangle using a SolidBrush created from your ARGB color.
[VB]
I havent tested this code
Dim MyBrush As New SolidBrush(Color.FromARGB(128, 0, 0, 255))
MyGraphics.FillRectangle(MyBrush, New Rectangle(0, 0, 100, 100))
It should draw a 50% opaque blue rectangle 100 px by 100 px at 0,0.
[/VB]
 
As a note, alpha channel "semi-transparent" colors as suggested by marble eater are only supported from Windows 2000 and up.
 
DiverDan, not for the sake of contradicting you, but for the sake of posting accurate information, Im going to point out that although "Layered Windows" (used for semi-transparent forms) is only supported on Windows 2000/Xp and up, other semi-transparency using Windows Forms is a feature of the GDI+ library included with the .Net Framework and is fully supported in any .Net application. Try using the Graphics object and controls with semi-transparent BackColors on the Win9x/NT platforms and you will still see the nifty semi-transparent effects.
 
Well, that project doesnt compile for me and I dont have the time to figure out the fix. What version of VB was that done in? Im using the most recent beta of VB.NET 2005 EE.
 
Ok, PROKA, here is what is wrong with your display.
You are drawing one rectangle in the forms Paint event. This in itself will only guarantee that one rectangle will be visible on your form.

When you create a new rectangle in MouseUp, you invalidate the region where you place the rectangle, so, even though:
you may not have a rectangle in this region and
you can successfully place it so that the old rectangle is not invalidated

The fact remains that you are only drawing one rectangle in the Paint event.

So, when the rectangles that you have drawn overlap, the one you are drawing now is the one that will dominate. Furthermore, the region of the rectangle is invalidated, therefore giving it an "opaque center".

I suggest that you create your own graphics object.
Dim GFX As Graphics

In Form_Load or somewhere on the form that runs first:
GFX = Me.CreateGraphics()

Now, instead of invalidating your display, which clears out a hole on the display,
just draw the rectangle.
GFX.DrawRectangle(Pens.Gray, RcDraw)

This solution will cause your rectangles to disappear when the form disappears or is obscured. If you want to keep the rectangles persistent, then keep an array of rectangles, go back to this solution you have already, with the Invalidate and the drawing in the Paint and, in the Pait event, use e.Graphics.DrawRectangles to draw a bunch of rectangles. :)
 
you can make a color have an alpha channel:

do something like this:

Code:
e.Graphics.FillRectangle(color.fromargb(127.5,255,255,255), New Rectangle(0, 0, 100, 100))

this will generate a filled rectangle that is 50% transparent.

the pig..
 
A similar reply has already been posted. Also, this thread has had no new posts for over a month and should be considered inactive.

I hope that I am not out of line when I recommend refreshing yourself on the Posting Guidelines.
 
Back
Top