Groupbox com borda colorida
-
segunda-feira, 25 de janeiro de 2010 23:54Pessoal foi dificil, mas eu achei...
Achei essa classe que cria um groupbox, e nela vc pode alterar a cor de suas bordas.
Porem tem um detalhe, as bordas do groupbox não são arredondas. Como eu faço para que as bordas sejam arredondadas ?
public class myGroupBox : GroupBox
{
private Color borderColor;
public Color BorderColor
{
get { return this.borderColor; }
set { this.borderColor = value; }
}
public myGroupBox()
{
this.borderColor = Color.Black;
}
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;
ControlPaint.DrawBorder(e.Graphics, borderRect, this.borderColor, ButtonBorderStyle.Solid);
Rectangle textRect = e.ClipRectangle;
textRect.X += 6;
textRect.Width = tSize.Width;
textRect.Height = tSize.Height;
e.Graphics.FillRectangle(new SolidBrush(this.BackColor), textRect);
e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), textRect);
}
}
Respostas
-
quinta-feira, 28 de janeiro de 2010 13:17
Olá!
Henrique, otimizei o código e coloquei mais duas propriedades. No total, o controle ficou com 3 propriedades que são:
> borderColor = Cor da borda
> textColor = Cor do texto do título do groupbox
> radius = Define a curvatura da borda
Teste por favor e veja se é isto que precisa.
Segue o código completo:
public class myGroupBox:GroupBox { private Color borderColor; private float radius; private Color textColor; public Color BorderColor { get { return this.borderColor; } set { this.borderColor = value; } } public float Radius { get { return this.radius; } set { this.radius = value; } } public Color TextColor { get { return this.textColor; } set { this.textColor = value; } } public myGroupBox() { this.borderColor = Color.Black; this.textColor = Color.Black; this.radius = 8; } protected override void OnPaint(PaintEventArgs e) { GraphicsPath gp = new GraphicsPath(); gp.AddArc(e.ClipRectangle.X + e.ClipRectangle.Width - 2 - (radius * 2), e.ClipRectangle.Y + 8, radius * 2, radius * 2, 270, 90); gp.AddArc(e.ClipRectangle.X + e.ClipRectangle.Width - 2 - (radius * 2), e.ClipRectangle.Y + e.ClipRectangle.Height - 2 - (radius * 2), radius * 2, radius * 2, 0, 90); gp.AddArc(e.ClipRectangle.X, e.ClipRectangle.Y + e.ClipRectangle.Height - 2 - (radius * 2), radius * 2, radius * 2, 90, 90); gp.AddArc(e.ClipRectangle.X, e.ClipRectangle.Y + 8, radius * 2, radius * 2, 180, 90); gp.CloseFigure(); e.Graphics.DrawPath(new Pen(borderColor), gp); Size tSize = TextRenderer.MeasureText(this.Text, this.Font); Rectangle textRect = e.ClipRectangle; textRect.X += 15; textRect.Width = tSize.Width; textRect.Height = tSize.Height; e.Graphics.FillRectangle(new SolidBrush(this.BackColor), textRect); e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(textColor), textRect); } } }
Daniel Henrique http://danielcordeiro.eti.br- Marcado como Resposta Henrique Alonso sexta-feira, 29 de janeiro de 2010 01:50
Todas as Respostas
-
terça-feira, 26 de janeiro de 2010 01:32
Olá!
Tente o seguinte:public void DrawRoundRect(Graphics g, Pen p, float x, float y, float width, float height, float radius) { GraphicsPath gp = new GraphicsPath(); gp.AddLine(x + radius, y, x + width - (radius * 2), y); // Linha gp.AddArc(x + width - (radius * 2), y, radius * 2, radius * 2, 270, 90); // Canto gp.AddLine(x + width, y + radius, x + width, y + height - (radius * 2)); // Linha gp.AddArc(x + width - (radius * 2), y + height - (radius * 2), radius * 2, radius * 2, 0, 90); // Canto gp.AddLine(x + width - (radius * 2), y + height, x + radius, y + height); // Linha gp.AddArc(x, y + height - (radius * 2), radius * 2, radius * 2, 90, 90); // Canto gp.AddLine(x, y + height - (radius * 2), x, y + radius); // Linha gp.AddArc(x, y, radius * 2, radius * 2, 180, 90); // Canto gp.CloseFigure(); g.DrawPath(p, gp); gp.Dispose(); }No evento Paint do form:
private void Form1_Paint(object sender, PaintEventArgs e) { Pen pn = new Pen(Color.Blue); DrawRoundRect(e.Graphics, pn, 10, 10, 50, 90, 5); }
Daniel Henrique http://danielcordeiro.eti.br- Sugerido como Resposta Danimar Ribeiro terça-feira, 26 de janeiro de 2010 02:33
- Não Sugerido como Resposta Henrique Alonso terça-feira, 26 de janeiro de 2010 21:25
-
terça-feira, 26 de janeiro de 2010 20:53eu coloco o seu código meu, ou eu substituo o seu pelo meu ?
-
quarta-feira, 27 de janeiro de 2010 14:53Olá!
O código que coloquei cria um groupbox com a borda arredondada. Você pode usar só o código que coloquei ou implementar o seu código com o meu código.
Daniel Henrique http://danielcordeiro.eti.br -
quarta-feira, 27 de janeiro de 2010 17:40eu até testei, porem ñ teria como fazer dele igual ao que eu passei, pois oq eu passei aparece um ícone na toolbox, é + fácil de mexer, pois é td feito de maneira visual. Se desse para alterar a borda no cód que eu passei seria muito fácil p\ usar
entendeu +/- o meu prob. ?
Agora se implementar o seu com esse que eu tenho seria ótimo, porem eu ainda ñ tenho mta experiencia, então eu ñ sei fazer, como poderia ser feito isso ? -
quinta-feira, 28 de janeiro de 2010 13:17
Olá!
Henrique, otimizei o código e coloquei mais duas propriedades. No total, o controle ficou com 3 propriedades que são:
> borderColor = Cor da borda
> textColor = Cor do texto do título do groupbox
> radius = Define a curvatura da borda
Teste por favor e veja se é isto que precisa.
Segue o código completo:
public class myGroupBox:GroupBox { private Color borderColor; private float radius; private Color textColor; public Color BorderColor { get { return this.borderColor; } set { this.borderColor = value; } } public float Radius { get { return this.radius; } set { this.radius = value; } } public Color TextColor { get { return this.textColor; } set { this.textColor = value; } } public myGroupBox() { this.borderColor = Color.Black; this.textColor = Color.Black; this.radius = 8; } protected override void OnPaint(PaintEventArgs e) { GraphicsPath gp = new GraphicsPath(); gp.AddArc(e.ClipRectangle.X + e.ClipRectangle.Width - 2 - (radius * 2), e.ClipRectangle.Y + 8, radius * 2, radius * 2, 270, 90); gp.AddArc(e.ClipRectangle.X + e.ClipRectangle.Width - 2 - (radius * 2), e.ClipRectangle.Y + e.ClipRectangle.Height - 2 - (radius * 2), radius * 2, radius * 2, 0, 90); gp.AddArc(e.ClipRectangle.X, e.ClipRectangle.Y + e.ClipRectangle.Height - 2 - (radius * 2), radius * 2, radius * 2, 90, 90); gp.AddArc(e.ClipRectangle.X, e.ClipRectangle.Y + 8, radius * 2, radius * 2, 180, 90); gp.CloseFigure(); e.Graphics.DrawPath(new Pen(borderColor), gp); Size tSize = TextRenderer.MeasureText(this.Text, this.Font); Rectangle textRect = e.ClipRectangle; textRect.X += 15; textRect.Width = tSize.Width; textRect.Height = tSize.Height; e.Graphics.FillRectangle(new SolidBrush(this.BackColor), textRect); e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(textColor), textRect); } } }
Daniel Henrique http://danielcordeiro.eti.br- Marcado como Resposta Henrique Alonso sexta-feira, 29 de janeiro de 2010 01:50
-
sexta-feira, 29 de janeiro de 2010 01:51nossa ficou perfeito !!!!
qro mto aprender a fazer estes tipos de classe !
muito obrigado !!! -
sexta-feira, 29 de janeiro de 2010 02:24Tentei fazer um negócio com essa classe, porem ñ deu mto certo.
Eu tentei usar ela para alterar a borda do TextBox, para aquela bordinha em 3D dele ficar de outra cor, ñ deu tentei mudar a borda simples dele e tambem não deu.
Como ficaria uma classe para mudar a borda do TextBox, para ela ficar em 3D ou simples de outra cor ?
E como ficaria uma classe para fazer um TextBox igual ao do Java, onde é possivel colocar o nome do TextBox em cima dele, dessa forma ñ é necessário usar label, ficaria como um GroupBox mas TextBox ? -
quarta-feira, 10 de fevereiro de 2010 02:29estou com um problema nessa classe
percebi o seguinte:
Em um forma eu tenho alguns GroupBox desses novos ai, porem eu abro uma janela em cima destes groupbox, e quando eu fecho a janela que foi aberta em cima, fica a marca dela, os desenhos dos groupbox estão deixando marcas na tela, e restros
como arrumar isso ? -
quarta-feira, 10 de fevereiro de 2010 21:03Olá!
Desculpe a demora, mas isso está acontecendo em tempo de execução ou design?
Daniel Henrique http://danielcordeiro.eti.br -
quarta-feira, 10 de fevereiro de 2010 21:20
em execução e design

