From bd4cbef68cde53e2378970df278f111ad49e1739 Mon Sep 17 00:00:00 2001 From: Tomas Linhart Date: Sun, 4 Sep 2011 17:13:07 +0200 Subject: [PATCH 1/2] Fix memory leak with drawing to bitmap. --- .../CodeQuality/Src/Controls/MatrixControl.cs | 22 ++++++++++--------- .../CodeQuality/Src/Utility/Helper.cs | 9 ++++++-- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/src/AddIns/Analysis/CodeQuality/Src/Controls/MatrixControl.cs b/src/AddIns/Analysis/CodeQuality/Src/Controls/MatrixControl.cs index c44e28d6a2..ef48f837c5 100644 --- a/src/AddIns/Analysis/CodeQuality/Src/Controls/MatrixControl.cs +++ b/src/AddIns/Analysis/CodeQuality/Src/Controls/MatrixControl.cs @@ -11,6 +11,8 @@ using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Media; using System.Windows.Media.Imaging; +using System.Windows.Interop; +using PointF = System.Drawing.PointF; namespace ICSharpCode.CodeQualityAnalysis.Controls { @@ -138,6 +140,8 @@ namespace ICSharpCode.CodeQualityAnalysis.Controls var maxHeight = ((int) viewport.Height / CellHeight) + 1; // how many cells we will draw + // sometimes happens when half of cell is hidden in scroll so text isnt drawn + // so lets drawn one more cell var cellsHorizontally = maxWidth > matrixWidth ? matrixWidth : maxWidth + 1; var cellsVertically = maxHeight > matrixHeight ? matrixHeight : maxHeight + 1; @@ -155,11 +159,6 @@ namespace ICSharpCode.CodeQualityAnalysis.Controls backgroundColor.Freeze(); drawingContext.DrawRectangle(backgroundColor, null, background); - // sometimes happens when half of cell is hidden in scroll so text isnt drawn - // so lets drawn one more cell -// cellsHorizontally = maxWidth > matrixWidth ? matrixWidth : maxWidth + 1; -// cellsVertically = maxHeight > matrixHeight ? matrixHeight : maxHeight + 1; - var currentXLine = (currentCell.X - scaledOffsetX) * CellWidth - offsetDiffX; var currentYLine = (currentCell.Y - scaledOffsetY) * CellHeight - offsetDiffY; @@ -264,16 +263,19 @@ namespace ICSharpCode.CodeQualityAnalysis.Controls var spanWidth = (CellWidth - size.Width) / 2; var spanHeight = (CellHeight - size.Height) / 2; - g.DrawString(text, fontOjb, System.Drawing.Brushes.Black, new System.Drawing.PointF(spanWidth, spanHeight)); + g.DrawString(text, fontOjb, System.Drawing.Brushes.Black, new PointF(spanWidth, spanHeight)); g.Dispose(); - var img = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bmp.GetHbitmap(), - IntPtr.Zero, - Int32Rect.Empty, - BitmapSizeOptions.FromWidthAndHeight(bmp.Width, bmp.Height)); + var bitmap = bmp.GetHbitmap(); + var img = Imaging.CreateBitmapSourceFromHBitmap(bitmap, + IntPtr.Zero, + Int32Rect.Empty, + BitmapSizeOptions.FromWidthAndHeight(bmp.Width, bmp.Height)); img.Freeze(); imgs.Add(text, img); + Helper.DeleteObject(bitmap); + return img; } diff --git a/src/AddIns/Analysis/CodeQuality/Src/Utility/Helper.cs b/src/AddIns/Analysis/CodeQuality/Src/Utility/Helper.cs index 48b9e67bd2..25d0bbc478 100644 --- a/src/AddIns/Analysis/CodeQuality/Src/Utility/Helper.cs +++ b/src/AddIns/Analysis/CodeQuality/Src/Utility/Helper.cs @@ -4,15 +4,20 @@ using System; using System.Windows; using System.Windows.Media; +using ICSharpCode.TreeView; +using System.Runtime.InteropServices; -namespace ICSharpCode.CodeQualityAnalysis +namespace ICSharpCode.CodeQualityAnalysis.Utility { /// /// Description of Helper. /// public static class Helper { - public static void FillTree(ICSharpCode.TreeView.SharpTreeView tree,Module module) + [DllImport("gdi32.dll")] + public static extern bool DeleteObject(IntPtr hObject); + + public static void FillTree(SharpTreeView tree, Module module) { var root = CreateTreeItem(module); tree.Root = root; From b10233ebabb229c27a2d4581f9fa5c8c97884596 Mon Sep 17 00:00:00 2001 From: Tomas Linhart Date: Sun, 4 Sep 2011 17:53:56 +0200 Subject: [PATCH 2/2] Add HighlightLine method to matrix control. --- .../CodeQuality/Src/Controls/MatrixControl.cs | 52 ++++++++++++------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/src/AddIns/Analysis/CodeQuality/Src/Controls/MatrixControl.cs b/src/AddIns/Analysis/CodeQuality/Src/Controls/MatrixControl.cs index ef48f837c5..807e9539ab 100644 --- a/src/AddIns/Analysis/CodeQuality/Src/Controls/MatrixControl.cs +++ b/src/AddIns/Analysis/CodeQuality/Src/Controls/MatrixControl.cs @@ -75,9 +75,9 @@ namespace ICSharpCode.CodeQualityAnalysis.Controls HoveredCell = new HoveredCell(); } - public void SetVisibleItems(HeaderType tree, ICollection visibleItems) + public void SetVisibleItems(HeaderType type, ICollection visibleItems) { - matrix.SetVisibleItems(tree, visibleItems); + matrix.SetVisibleItems(type, visibleItems); matrixHeight = matrix.HeaderRows.Count; matrixWidth = matrix.HeaderColumns.Count; @@ -94,17 +94,27 @@ namespace ICSharpCode.CodeQualityAnalysis.Controls } if (changedCoords) { - HoveredCell.RowIndex = currentCell.Y; - HoveredCell.ColumnIndex = currentCell.X; - HoveredCell.Value = matrix[HoveredCell.RowIndex, HoveredCell.ColumnIndex]; - if (HoveredCellChanged != null) - HoveredCellChanged(this, new HoveredCellEventArgs(HoveredCell)); + SetHoveredCell(); } if (matrixHeight >= 0 && matrixWidth >= 0) InvalidateVisual(); } + public void HighlightLine(HeaderType type, INode node) + { + var items = type == HeaderType.Columns ? matrix.HeaderColumns : matrix.HeaderRows; + for (int i = 0; i < items.Count; i++) { + if (node.Equals(items[i])) { + currentCell = type == HeaderType.Columns ? + new Coords(i, currentCell.Y) : + new Coords(currentCell.X, i); + + SetHoveredCell(); + } + } + } + protected override void OnMouseMove(System.Windows.Input.MouseEventArgs e) { base.OnMouseMove(e); @@ -122,15 +132,19 @@ namespace ICSharpCode.CodeQualityAnalysis.Controls currentCell.Y != HoveredCell.ColumnIndex) { InvalidateVisual(); - - HoveredCell.RowIndex = currentCell.Y; - HoveredCell.ColumnIndex = currentCell.X; - HoveredCell.Value = matrix[HoveredCell.RowIndex, HoveredCell.ColumnIndex]; - if (HoveredCellChanged != null) - HoveredCellChanged(this, new HoveredCellEventArgs(HoveredCell)); + SetHoveredCell(); } } + protected void SetHoveredCell() + { + HoveredCell.RowIndex = currentCell.Y; + HoveredCell.ColumnIndex = currentCell.X; + HoveredCell.Value = matrix[HoveredCell.RowIndex, HoveredCell.ColumnIndex]; + if (HoveredCellChanged != null) + HoveredCellChanged(this, new HoveredCellEventArgs(HoveredCell)); + } + protected override void OnRender(DrawingContext drawingContext) { base.OnRender(drawingContext); @@ -167,9 +181,9 @@ namespace ICSharpCode.CodeQualityAnalysis.Controls // hover y line var rect = new Rect(0, - currentYLine, - CellWidth * cellsHorizontally, - CellHeight); + currentYLine, + CellWidth * cellsHorizontally, + CellHeight); var brush = new SolidColorBrush(Colors.GreenYellow); brush.Freeze(); @@ -203,7 +217,7 @@ namespace ICSharpCode.CodeQualityAnalysis.Controls int rowIndex = j; int columnIndex = i; var value = matrix[rowIndex, columnIndex]; - + if (Colorizer != null) { var rect = new Rect( i * CellWidth - offsetDiffX, @@ -216,7 +230,7 @@ namespace ICSharpCode.CodeQualityAnalysis.Controls // ((i * CellWidth - offsetDiffX) == currentXLine)) // brush = Brushes.Pink; //Colorizer.GetColorBrushMixedWith(Colors.GreenYellow, value); // else - brush = Colorizer.GetColorBrush(value); + brush = Colorizer.GetColorBrush(value); drawingContext.DrawRectangle(brush, null, rect); } @@ -247,7 +261,7 @@ namespace ICSharpCode.CodeQualityAnalysis.Controls } public ImageSource CreateText(string text) - { + { if (imgs.ContainsKey(text)) return imgs[text];