private long populateTree(TreeMapNode root) { DirectoryInfo d = new DirectoryInfo(root.getLabel()); try { long size = 0L; foreach (DirectoryInfo dir in d.GetDirectories()) { TreeMapNode node = new TreeMapNode(dir.FullName); long weight = populateTree(node); node.setWeight(weight); node.setValue(new DefaultValue(weight)); root.add(node); size += weight; } foreach (FileInfo file in d.GetFiles()) { size += file.Length; TreeMapNode node = new TreeMapNode(file.FullName, file.Length, new DefaultValue(file.Length)); root.add(node); } return(size); } catch { return(0); } }
private void paint(TreeMapNode n, Graphics g, int level) { if (n.IsLeaf) { RectangleF rect = new RectangleF((float)n.getX(), (float)n.getY(), (float)n.getWidth(), (float)n.getHeight()); if (rect.Width > 0 && rect.Height > 0) { gp.Reset(); gp.AddEllipse(rect.Left - rect.Width / 4, rect.Top - rect.Height / 4, rect.Width + 2 * rect.Width / 4, rect.Height + 2 * rect.Height / 4); PathGradientBrush pgb = new PathGradientBrush(gp); pgb.CenterColor = Color.White; string ext = Path.GetExtension(n.getLabel()).TrimStart('.'); if (extensions.ContainsKey(ext.ToLower())) { pgb.SurroundColors = new Color[] { (Color)extensions[ext.ToLower()] }; } else { Color c = Color.FromKnownColor((KnownColor)((int)KnownColor.ForestGreen + extensions.Count)); extensions[ext.ToLower()] = c; pgb.SurroundColors = new Color[] { c }; } pgb.CenterPoint = new PointF(rect.Left + rect.Width / 4, rect.Top + rect.Height / 2); g.FillRectangle(pgb, rect); pgb.Dispose(); string label = Path.GetFileName(n.getLabel()); SizeF labelSize = g.MeasureString(label, f, (int)rect.Width); if (rect.Width > 1 && rect.Height > 1 && File.Exists(n.getLabel())) { if (n.Icon == null) { n.Icon = Icon.ExtractAssociatedIcon(n.getLabel()); } if (rect.Width >= n.Icon.Width && rect.Height >= n.Icon.Height + labelSize.Height) { Rectangle iconRect = new Rectangle((int)rect.Left + (int)rect.Width / 2 - n.Icon.Width / 2, (int)rect.Top + (int)rect.Height / 2 - n.Icon.Height / 2 - (int)labelSize.Height / 2, n.Icon.Width, n.Icon.Height); if (clipBoard == n && cutClipboard) { ImageAttributes ia = new ImageAttributes(); ia.SetGamma(0.3F); g.DrawImage(n.Icon.ToBitmap(), iconRect, 0, 0, n.Icon.Width, n.Icon.Height, GraphicsUnit.Pixel, ia); } else { g.DrawIconUnstretched(n.Icon, iconRect); } Region clip = g.Clip; g.Clip = new Region(rect); g.DrawString(label, f, Brushes.Black, new RectangleF(rect.Left + rect.Width / 2 - labelSize.Width / 2, rect.Top + rect.Height / 2 - labelSize.Height / 2 + iconRect.Height / 2, labelSize.Width, labelSize.Height)); g.Clip = clip; } else { double ratio = Math.Min(rect.Width / n.Icon.Width, rect.Height / n.Icon.Height); if (clipBoard == n && cutClipboard) { ImageAttributes ia = new ImageAttributes(); ia.SetGamma(0.3F); g.DrawImage(n.Icon.ToBitmap(), new Rectangle((int)Math.Round(rect.Left + rect.Width / 2 - n.Icon.Width * ratio / 2), (int)Math.Round(rect.Top + rect.Height / 2 - n.Icon.Height * ratio / 2), (int)Math.Round(n.Icon.Width * ratio), (int)Math.Round(n.Icon.Height * ratio)), 0, 0, n.Icon.Width, n.Icon.Height, GraphicsUnit.Pixel, ia); } else { g.DrawIcon(n.Icon, new Rectangle((int)Math.Round(rect.Left + rect.Width / 2 - n.Icon.Width * ratio / 2), (int)Math.Round(rect.Top + rect.Height / 2 - n.Icon.Height * ratio / 2), (int)Math.Round(n.Icon.Width * ratio), (int)Math.Round(n.Icon.Height * ratio))); } } } } } else if (level > 1) { RectangleF rect = new RectangleF((float)n.getX(), (float)n.getY(), (float)n.getWidth(), (float)n.getHeight()); if (rect.Width > 0 && rect.Height > 0) { gp.Reset(); gp.AddEllipse(rect.Left - rect.Width / 4, rect.Top - rect.Height / 4, rect.Width + 2 * rect.Width / 4, rect.Height + 2 * rect.Height / 4); PathGradientBrush pgb = new PathGradientBrush(gp); pgb.CenterColor = Color.White; pgb.SurroundColors = new Color[] { Color.YellowGreen }; pgb.CenterPoint = new PointF(rect.Left + rect.Width / 4, rect.Top + rect.Height / 2); g.FillRectangle(pgb, rect); pgb.Dispose(); string label = Path.GetFileName(n.getLabel()); SizeF labelSize = g.MeasureString(label, f); Region clip = g.Clip; g.Clip = new Region(rect); g.DrawString(label, f, Brushes.Black, new RectangleF(rect.Left + rect.Width / 2 - labelSize.Width / 2, rect.Top + rect.Height / 2 - labelSize.Height / 2, labelSize.Width, labelSize.Height)); g.Clip = clip; } } if (depthLevel == 0 || level < depthLevel) { foreach (TreeMapNode child in n.Nodes) { paint(child, g, level + 1); } } }