Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Color del borde de GroupBox

1,509 views
Skip to first unread message

jdepaz

unread,
Jul 4, 2010, 3:54:45 PM7/4/10
to
Hola lista,

Tengo el siguiente codigo para cambiar el color del borde de un
GroupBox:


public partial class GroupBox1:Form
{
public GroupBox1()
{
}

private void GroupBox1_Load(object sender, EventArgs e)
{
myGroupBox myGroupBox = new myGroupBox();
myGroupBox.Text = "CGroupBox1";
myGroupBox.BorderColor = Color.Red;
this.Controls.Add(myGroupBox);
}
}
public class myGroupBox : GroupBox
{
private Color borderColor;
public Color BorderColor
{
get { return this.borderColor; }
set { this.borderColor = value; }
}

public myGroupBox()
{
this.borderColor = Color.Black;
}
private Color backColor = Color.Transparent;

public Color ActualBackColor
{
get { return this.backColor; }
set { this.backColor = value; }
}
protected override void OnPaint(PaintEventArgs e)
{
Size tSize = TextRenderer.MeasureText(this.Text,
this.Font);

Rectangle borderRect = e.ClipRectangle;
borderRect.Y += tSize.Height / 2;
borderRect.Height -= tSize.Height / 2;
GraphicsPath gPath = CreatePath(0, borderRect.Y,(float)
(this.Width - 1), borderRect.Height - 1, 5, true, true, true, true);


e.Graphics.FillPath(new SolidBrush(ActualBackColor),
gPath);
e.Graphics.DrawPath(new Pen(this.borderColor), gPath);
borderRect.X += 6;
borderRect.Y -= 7;
e.Graphics.DrawString(this.Text, this.Font, new
SolidBrush(this.ForeColor), borderRect);
}

public GraphicsPath CreatePath(float x, float y, float width,
float height,
float radius, bool
RoundTopLeft, bool RoundTopRight, bool RoundBottomRight, bool
RoundBottomLeft)
{
float xw = x + width;
float yh = y + height;
float xwr = xw - radius;
float yhr = yh - radius;
float xr = x + radius;
float yr = y + radius;
float r2 = radius * 2;
float xwr2 = xw - r2;
float yhr2 = yh - r2;

GraphicsPath p = new GraphicsPath();
p.StartFigure();

//Top Left Corner

if (RoundTopLeft)
{
p.AddArc(x, y, r2, r2, 180, 90);
}
else
{
p.AddLine(x, yr, x, y);
p.AddLine(x, y, xr, y);

}

//Top Edge
p.AddLine(xr, y, xwr, y);

//Top Right Corner

if (RoundTopRight)
{
p.AddArc(xwr2, y, r2, r2, 270, 90);
}
else
{
p.AddLine(xwr, y, xw, y);
p.AddLine(xw, y, xw, yr);
}


//Right Edge
p.AddLine(xw, yr, xw, yhr);

//Bottom Right Corner

if (RoundBottomRight)
{
p.AddArc(xwr2, yhr2, r2, r2, 0, 90);
}
else
{
p.AddLine(xw, yhr, xw, yh);
p.AddLine(xw, yh, xwr, yh);
}


//Bottom Edge
p.AddLine(xwr, yh, xr, yh);

//Bottom Left Corner

if (RoundBottomLeft)
{
p.AddArc(x, yhr2, r2, r2, 90, 90);
}
else
{
p.AddLine(xr, yh, x, yh);
p.AddLine(x, yh, x, yhr);
}

//Left Edge
p.AddLine(x, yhr, x, yr);

p.CloseFigure();
return p;
}
}

Ahora bien,
resulta que en el diseñador, al arrastrar el puntero se
muestra como la figura que adjunto, y también
en tiempo de diseño, al hacer click con el botón
derecho del mouse, se repinta como la otra figura.


http://img94.imageshack.us/img94/4071/screenshot023ha.jpg
http://img33.imageshack.us/img33/4022/screenshot024t.jpg

Leandro Tuttini

unread,
Jul 10, 2010, 7:19:27 PM7/10/10
to

te aconsejaria que la pregunta la realice en los foros de MSDN

http://social.msdn.microsoft.com/Forums/es-ES/vcses

imagino que la idea de hacerlo la has obtenido de un ejmeplo como este
http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/cfd34dd1-b6e5-4b56-9901-0dc3d2ca5788

se que el ejmeplo esta en vb.net, pero podrias traducirlo ayudado por
estas tools

Convert C# to VB.NET
http://www.developerfusion.com/tools/convert/csharp-to-vb/
Code Converter
http://converter.telerik.com/

por ahi si tomas el ejemplo implementandolo en el OnPaint de resultado

saludos

0 new messages