Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I know how to draw rectangles. The problem is that when I draw another rectangle, it doesn't show what's behind him ( the old rectangles ). How can I draw a 'transparent rectangle'

 

See attached files (VB.NET)

Development & Research Department @ Elven Soft
  • Leaders
Posted

I'm 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.

'I haven't 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.

[sIGPIC]e[/sIGPIC]
  • Leaders
Posted
DiverDan, not for the sake of contradicting you, but for the sake of posting accurate information, I'm 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.
[sIGPIC]e[/sIGPIC]
  • Leaders
Posted

Ok, PROKA, here is what is wrong with your display.

You are drawing one rectangle in the form's 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. :)

Iceplug, USN

One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(

  • 1 month later...
Posted

you can make a color have an alpha channel:

 

do something like this:

 

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..

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...