Respuesta: Tamaño de imagenes en nodo de un tree view Bueno me voy a responder para quien le interese...
paso 1.- Crear un control heredado de un treeView
paso 2. set la propiedad ItemHeight a la altura de la imagenes
paso 3.- escribir el siguiente codigo
private Rectangle CalcularBoundsNodo(TreeNode Nodo)
{
Rectangle rec = Nodo.Bounds;
Graphics g = this.CreateGraphics();
int TextWidth = (int)g.MeasureString(Nodo.Text, Nodo.NodeFont).Width + 26 - rec.Size.Width;
Point loc = rec.Location;
rec.Inflate(TextWidth, 0);
rec.Offset(loc.X - rec.X, 0);
g.Dispose();
return rec;
}
private GraphicsPath PathBackcolorGrupos(Rectangle Rec)
{
GraphicsPath gp = new GraphicsPath();
Rectangle r = new Rectangle(2, Rec.Y + 3, this.Width - 25, Rec.Height - 10);
int radius = 10;
gp.AddArc(r.Left, r.Top, radius, radius, 180, 90);
gp.AddArc(r.Right - radius, r.Top, radius, radius, 270, 90);
gp.AddArc(r.Right - radius, r.Bottom - radius, radius, radius, 0, 90);
gp.AddArc(r.Left, r.Bottom - radius, radius, radius, 90, 90);
gp.CloseFigure();
return gp;
}
protected override void OnDrawNode(DrawTreeNodeEventArgs e)
{
// e.DrawDefault = true;
Rectangle Rec = CalcularBoundsNodo(e.Node);
if ((e.State & TreeNodeStates.Selected) != 0)
{
// e.Graphics.FillRectangle(Brushes.Brown, Rec);
// // Color Color1 = Color.FromArgb(176, 216, 228);
// //Color Color2 = Color.FromArgb(72, 72, 172);
Color Color1 = Color.White;
Color Color2 = Color.Teal;
LinearGradientBrush brocha = new LinearGradientBrush(Rec, Color1, Color2, LinearGradientMode.Vertical);
// brocha.GammaCorrection = true;
GraphicsPath Objeto = PathBackcolorGrupos(Rec);
e.Graphics.FillPath(brocha, Objeto);
e.Graphics.DrawPath(new Pen(Brushes.Teal), Objeto);
// Retrieve the node font. If the node font has not been set,
// use the TreeView font.
Font nodeFont = e.Node.NodeFont;
if (nodeFont == null) nodeFont = this.Font;
//Dibuja imagen de collapse y expand y sea del grupo principal
if (e.Node.Parent == null)
{
Font fontExpand = new Font("marlett", 10, GraphicsUnit.Point);
Rectangle rectText1 = new Rectangle(new Point(0, Rec.Y + 8), new Size(15, 15));
if (e.Node.IsExpanded)
e.Graphics.DrawString("4", fontExpand, Brushes.Black, rectText1);
else
e.Graphics.DrawString("6", fontExpand, Brushes.Black, rectText1);
fontExpand.Dispose();
}
else
{//dibuja la presencia de la imagen
Rec.Offset(30, 0);
if (e.Node.StateImageIndex != -1)
{
Image Imagen = this.StateImageList.Images[e.Node.StateImageIndex];
Point Posimage = new Point(Rec.X - 30, Rec.Y);
e.Graphics.DrawImage(Imagen, Posimage);
}
}
// Dibuja Texto
Point loc = new Point(Rec.X, Rec.Y + 5);
Rectangle rectText = new Rectangle(loc, Rec.Size);
e.Graphics.DrawString(e.Node.Text, nodeFont, Brushes.White, rectText);
}
else
{
// e.DrawDefault = true;
Rectangle RecNodo = new Rectangle(0, Rec.Y, this.Width, Rec.Height);
e.Graphics.FillRectangle(Brushes.White, RecNodo);
//dibuja el backcolor de los grupos
#region dibuja el backcolor de los grupos
//if (e.Node.Parent == null)
//{
// // Color Color1 = Color.FromArgb(176, 216, 228);
// //Color Color2 = Color.FromArgb(72, 72, 172);
// Color Color1 = Color.White;
// Color Color2 = Color.Teal;
// LinearGradientBrush brocha = new LinearGradientBrush(Rec, Color1, Color2,LinearGradientMode.Vertical);
// // brocha.GammaCorrection = true;
// GraphicsPath Objeto = PathBackcolorGrupos(Rec);
// e.Graphics.FillPath(brocha, Objeto);
// e.Graphics.DrawPath(new Pen(Brushes.Teal), Objeto);
//}
#endregion
// Retrieve the node font. If the node font has not been set,
// use the TreeView font.
Font nodeFont = e.Node.NodeFont;
if (nodeFont == null) nodeFont = this.Font;
//Dibuja imagen de collapse y expand y sea del grupo principal
#region para poner el expand y collap con una imagen
//if (e.Node.Parent == null && imageList2.Images.Count != 0)
// if (e.Node.IsExpanded)
// {
// Image Imagen = imageList2.Images[1];
// Point Posimage = new Point(Rec.X - 11, Rec.Y + 8);
// e.Graphics.DrawImage(Imagen, Posimage);
// }
// else
// {
// Image Imagen = imageList2.Images[0];
// Point Posimage = new Point(Rec.X - 11, Rec.Y + 8);
// e.Graphics.DrawImage(Imagen, Posimage);
// }
#endregion
if (e.Node.Parent == null)
{
Font fontExpand = new Font("marlett", 10, GraphicsUnit.Point);
Rectangle rectText1 = new Rectangle(new Point(0, Rec.Y + 8), new Size(15, 15));
if (e.Node.IsExpanded)
e.Graphics.DrawString("4", fontExpand, Brushes.Black, rectText1);
else
e.Graphics.DrawString("6", fontExpand, Brushes.Black, rectText1);
fontExpand.Dispose();
}
else
{
//dibuja la presencia de la imagen
Rec.Offset(30, 0);
if (e.Node.StateImageIndex != -1)
{
Image Imagen = this.StateImageList.Images[e.Node.StateImageIndex];
Point Posimage = new Point(Rec.X - 30, Rec.Y);
e.Graphics.DrawImage(Imagen, Posimage);
}
}
// Dibuja Texto
Point loc = new Point(Rec.X, Rec.Y + 5);
Rectangle rectText = new Rectangle(loc, Rec.Size);
SolidBrush BrochaTexto = new SolidBrush(e.Node.ForeColor);
e.Graphics.DrawString(e.Node.Text, nodeFont, BrochaTexto, rectText);
}
}
nota: el ejemplo esta aplicado a un ejercicio en especifico.
Suerte |