views:

1637

answers:

2

Hello,

I woud like to create a filled rounded rectangle at run-time and assign it as content of a PictureBox (already created and hidden) in Windows Forms.

Do you have an idea how can I implement it?

thank you in advance

A: 

The easiest way to do this is to create a Bitmap on the fly using the Graphics object. The DrawEllipse method should suffice.

Then assign that Bitmap as the contents of the PictureBox object.

JaredPar
+3  A: 

This method fills a rounded rectangle on a graphics object (VB code) :

Public Sub FillRoundedRectangle(ByVal g As Drawing.Graphics, ByVal r As Rectangle, ByVal d As Integer, ByVal b As Brush)
    Dim mode As Drawing2D.SmoothingMode = g.SmoothingMode
    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed
    g.FillPie(b, r.X, r.Y, d, d, 180, 90)
    g.FillPie(b, r.X + r.Width - d, r.Y, d, d, 270, 90)
    g.FillPie(b, r.X, r.Y + r.Height - d, d, d, 90, 90)
    g.FillPie(b, r.X + r.Width - d, r.Y + r.Height - d, d, d, 0, 90)
    g.FillRectangle(b, CInt(r.X + d / 2), r.Y, r.Width - d, CInt(d / 2))
    g.FillRectangle(b, r.X, CInt(r.Y + d / 2), r.Width, CInt(r.Height - d))
    g.FillRectangle(b, CInt(r.X + d / 2), CInt(r.Y + r.Height - d / 2), CInt(r.Width - d), CInt(d / 2))
    g.SmoothingMode = mode
End Sub

To call this function, handle the paint event of the picturebox and pass the e.Graphics object as the first argument, and the picturebox's bounds as the second argument if you want the rectangle to fill your picture box completely.

The d parameter changes the corners' angle, I call it with a value of 30, you can try different values...

Also, here's some code to draw (instead of fill) a rounded rectangle:

Public Sub DrawRoundedRectangle(ByVal g As Drawing.Graphics, ByVal r As Rectangle, ByVal d As Integer, ByVal p As Pen)
    g.DrawArc(p, r.X, r.Y, d, d, 180, 90)
    g.DrawLine(p, CInt(r.X + d / 2), r.Y, CInt(r.X + r.Width - d / 2), r.Y)
    g.DrawArc(p, r.X + r.Width - d, r.Y, d, d, 270, 90)
    g.DrawLine(p, r.X, CInt(r.Y + d / 2), r.X, CInt(r.Y + r.Height - d / 2))
    g.DrawLine(p, CInt(r.X + r.Width), CInt(r.Y + d / 2), CInt(r.X + r.Width), CInt(r.Y + r.Height - d / 2))
    g.DrawLine(p, CInt(r.X + d / 2), CInt(r.Y + r.Height), CInt(r.X + r.Width - d / 2), CInt(r.Y + r.Height))
    g.DrawArc(p, r.X, r.Y + r.Height - d, d, d, 90, 90)
    g.DrawArc(p, r.X + r.Width - d, r.Y + r.Height - d, d, d, 0, 90)
End Sub
Meta-Knight
hey Meta-Knight thanks a lot for your lightning anwser! :DThe code is working very well.Just another question, how can I do if I don't want to paint it immediatly but only when the application is in a certain state? Should I use also in this case the Paint event?
marco.ragogna
You could have a boolean value in your class (let's say it's called mustPaint) that you set when you want the rectangle to be painted then you could add a condition in the Paint event:if mustPaint then [paint rounded rectangle here] end if
Meta-Knight
ok, I will try it, thanks
marco.ragogna
do you have any idea why the shape is showed only when I do for example Alt-Tab and not before?
marco.ragogna
I solved calling the PictureBox.Refresh methdo manually to trigger the Paint event
marco.ragogna