From 1f28b5d32f94e0b9157f8176c6fb36aa06b9583e Mon Sep 17 00:00:00 2001 From: Markus Hofknecht Date: Fri, 13 Mar 2020 14:31:31 +0100 Subject: [PATCH] #NewBug Fixed slow scrolling --- Controls/Menu.cs | 10 ++++++++-- Helper/DataGridViewExtensions.cs | 32 ++++++++++++++++++++++++++++++++ SystemTrayMenu.cs | 6 ++---- SystemTrayMenu.csproj | 1 + 4 files changed, 43 insertions(+), 6 deletions(-) create mode 100644 Helper/DataGridViewExtensions.cs diff --git a/Controls/Menu.cs b/Controls/Menu.cs index 9eec362..4114ba1 100644 --- a/Controls/Menu.cs +++ b/Controls/Menu.cs @@ -4,6 +4,7 @@ using System.Linq; using System.Reflection; using System.Windows.Forms; using SystemTrayMenu.Controls; +using SystemTrayMenu.Helper; namespace SystemTrayMenu { @@ -53,6 +54,9 @@ namespace SystemTrayMenu dgvCellStyle.SelectionBackColor = MenuDefines.FileHover; dgvCellStyle.SelectionForeColor = Color.Black; this.dgv.DefaultCellStyle = dgvCellStyle; + + VScrollBar scrollBar = dgv.Controls.OfType().First(); + scrollBar.MouseWheel += dgv_MouseWheel; } static void SetDoubleBuffer(Control ctl, bool DoubleBuffered) @@ -209,7 +213,9 @@ namespace SystemTrayMenu private void AdjustDataGridViewSize() { - dgv.AutoResizeColumns(); + //dgv.AutoResizeColumns(); //AutoResizeColumns slow ~45ms + DataGridViewExtensions.FastAutoSizeColumns(dgv); + bool scrollbarShown = false; foreach (var scroll in dgv.Controls.OfType()) { @@ -254,7 +260,7 @@ namespace SystemTrayMenu } } - dgv.PerformLayout(); + Application.DoEvents(); MouseWheel.Invoke(); } diff --git a/Helper/DataGridViewExtensions.cs b/Helper/DataGridViewExtensions.cs new file mode 100644 index 0000000..c83f637 --- /dev/null +++ b/Helper/DataGridViewExtensions.cs @@ -0,0 +1,32 @@ +using System.Data; +using System.Linq; +using System.Windows.Forms; + +namespace SystemTrayMenu.Helper +{ + public static class DataGridViewExtensions + { + public static void FastAutoSizeColumns(this DataGridView dgv) + { + var rows = dgv.Rows + .Cast(); + using (var gfx = dgv.CreateGraphics()) + { + int i = 1; + gfx.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; + float widthMax = dgv.Columns[i].HeaderCell.Size.Width; + foreach (DataGridViewRow row in rows) + { + var checkWidth = gfx.MeasureString( + row.Cells[i].Value.ToString() + "_", + dgv.RowTemplate.DefaultCellStyle.Font).Width; + if (checkWidth > widthMax) + { + widthMax = checkWidth; + } + } + dgv.Columns[i].Width = (int)(widthMax + 0.5); + } + } + } +} diff --git a/SystemTrayMenu.cs b/SystemTrayMenu.cs index 485907d..0dd2a0e 100644 --- a/SystemTrayMenu.cs +++ b/SystemTrayMenu.cs @@ -37,7 +37,7 @@ namespace SystemTrayMenu OpenCloseState openCloseState = OpenCloseState.Default; BackgroundWorker worker = new BackgroundWorker(); - Screen screen = null; + Screen screen = Screen.PrimaryScreen; public SystemTrayMenu(ref bool cancelAppRun) { @@ -124,8 +124,6 @@ namespace SystemTrayMenu #endregion openCloseState = OpenCloseState.Opening; - screen = Screen.PrimaryScreen; - menuNotifyIcon.LoadingStart(); worker.RunWorkerAsync(); } @@ -318,7 +316,7 @@ namespace SystemTrayMenu widthPredecessors -= newWith; } } - else if (Screen.PrimaryScreen.Bounds.Width < + else if (screen.Bounds.Width < widthPredecessors + menuPredecessor.Width + menu.Width) { directionToRight = true; diff --git a/SystemTrayMenu.csproj b/SystemTrayMenu.csproj index 5f8ee72..d8746c1 100644 --- a/SystemTrayMenu.csproj +++ b/SystemTrayMenu.csproj @@ -160,6 +160,7 @@ +