#NewBug Fixed slow scrolling

This commit is contained in:
Markus Hofknecht 2020-03-13 14:31:31 +01:00
parent 8102a7b787
commit 1f28b5d32f
4 changed files with 43 additions and 6 deletions

View file

@ -4,6 +4,7 @@ using System.Linq;
using System.Reflection; using System.Reflection;
using System.Windows.Forms; using System.Windows.Forms;
using SystemTrayMenu.Controls; using SystemTrayMenu.Controls;
using SystemTrayMenu.Helper;
namespace SystemTrayMenu namespace SystemTrayMenu
{ {
@ -53,6 +54,9 @@ namespace SystemTrayMenu
dgvCellStyle.SelectionBackColor = MenuDefines.FileHover; dgvCellStyle.SelectionBackColor = MenuDefines.FileHover;
dgvCellStyle.SelectionForeColor = Color.Black; dgvCellStyle.SelectionForeColor = Color.Black;
this.dgv.DefaultCellStyle = dgvCellStyle; this.dgv.DefaultCellStyle = dgvCellStyle;
VScrollBar scrollBar = dgv.Controls.OfType<VScrollBar>().First();
scrollBar.MouseWheel += dgv_MouseWheel;
} }
static void SetDoubleBuffer(Control ctl, bool DoubleBuffered) static void SetDoubleBuffer(Control ctl, bool DoubleBuffered)
@ -209,7 +213,9 @@ namespace SystemTrayMenu
private void AdjustDataGridViewSize() private void AdjustDataGridViewSize()
{ {
dgv.AutoResizeColumns(); //dgv.AutoResizeColumns(); //AutoResizeColumns slow ~45ms
DataGridViewExtensions.FastAutoSizeColumns(dgv);
bool scrollbarShown = false; bool scrollbarShown = false;
foreach (var scroll in dgv.Controls.OfType<VScrollBar>()) foreach (var scroll in dgv.Controls.OfType<VScrollBar>())
{ {
@ -254,7 +260,7 @@ namespace SystemTrayMenu
} }
} }
dgv.PerformLayout(); Application.DoEvents();
MouseWheel.Invoke(); MouseWheel.Invoke();
} }

View file

@ -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<DataGridViewRow>();
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);
}
}
}
}

View file

@ -37,7 +37,7 @@ namespace SystemTrayMenu
OpenCloseState openCloseState = OpenCloseState.Default; OpenCloseState openCloseState = OpenCloseState.Default;
BackgroundWorker worker = new BackgroundWorker(); BackgroundWorker worker = new BackgroundWorker();
Screen screen = null; Screen screen = Screen.PrimaryScreen;
public SystemTrayMenu(ref bool cancelAppRun) public SystemTrayMenu(ref bool cancelAppRun)
{ {
@ -124,8 +124,6 @@ namespace SystemTrayMenu
#endregion #endregion
openCloseState = OpenCloseState.Opening; openCloseState = OpenCloseState.Opening;
screen = Screen.PrimaryScreen;
menuNotifyIcon.LoadingStart(); menuNotifyIcon.LoadingStart();
worker.RunWorkerAsync(); worker.RunWorkerAsync();
} }
@ -318,7 +316,7 @@ namespace SystemTrayMenu
widthPredecessors -= newWith; widthPredecessors -= newWith;
} }
} }
else if (Screen.PrimaryScreen.Bounds.Width < else if (screen.Bounds.Width <
widthPredecessors + menuPredecessor.Width + menu.Width) widthPredecessors + menuPredecessor.Width + menu.Width)
{ {
directionToRight = true; directionToRight = true;

View file

@ -160,6 +160,7 @@
<Compile Include="Controls\RowData.cs" /> <Compile Include="Controls\RowData.cs" />
<Compile Include="Controls\AppContextMenu.cs" /> <Compile Include="Controls\AppContextMenu.cs" />
<Compile Include="Helper\BringWindowToTop.cs" /> <Compile Include="Helper\BringWindowToTop.cs" />
<Compile Include="Helper\DataGridViewExtensions.cs" />
<Compile Include="Helper\File\IconReader.cs" /> <Compile Include="Helper\File\IconReader.cs" />
<Compile Include="Helper\File\FileIni.cs" /> <Compile Include="Helper\File\FileIni.cs" />
<Compile Include="Helper\File\FileLnk.cs" /> <Compile Include="Helper\File\FileLnk.cs" />