After I minimize the form, and pop it back up, the listBox (which is OwnerDrawned so I can customize the colors and behavior to my likings) starts to flicker when I hover over the items.
How to fix this ?
I've tried several solutions by now, like https://www.codeproject.com/Answers/473893/FlickeringplusinplusaplusWindowsplusFormplusapplic and https://www.codeproject.com/Answers/728560/Remove-flickering-due-to-TableLayoutPanel-Panel-2 but also, https://stackoverflow.com/a/3718648/10012792 but none seem to fix the flickering AFTER I minimized the app and pop it back up.
{
private int _MouseIndex = -1;
public Form1()
{
InitializeComponent();
ResizeRedraw = true;
DoubleBuffered = true;
}
public static void SetDoubleBuffered(System.Windows.Forms.Control c)
{
if (System.Windows.Forms.SystemInformation.TerminalServerSession)
return;
System.Reflection.PropertyInfo aProp = typeof(System.Windows.Forms.Control).GetProperty("DoubleBuffered", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance);
aProp.SetValue(c, true, null);
}
protected override CreateParams CreateParams
{
get
{
// Activate double buffering at the form level. All child controls will be double buffered as well.
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000; // Turn on WS_EX_COMPOSITED
return cp;
}
}
{
DoubleBuffered = true;
if (e.Index < 0) return;
//e.DrawBackground();
Color borderColor = Color.FromArgb(255, 42, 42, 42);
bool isItemSelected = ((e.State & DrawItemState.Selected) == DrawItemState.Selected);
using (SolidBrush bgBrush = new SolidBrush(isItemSelected ? Color.FromArgb(255, 52, 52, 52) : Color.FromArgb(255, 29, 29, 29)))
using (SolidBrush overlay = new SolidBrush(isItemSelected ? Color.FromArgb(255, 42, 42, 42) : Color.FromArgb(255, 42, 42, 42)))
using (SolidBrush itemBrush = isItemSelected ? new SolidBrush(Color.FromArgb(255, 68, 201, 94)) : new SolidBrush(Color.FromArgb(255, 243, 243, 243)))
{
string itemText = listBoxTracks.GetItemText(listBoxTracks.Items[e.Index]);
e.Graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
SizeF size = e.Graphics.MeasureString(listBoxTracks.ToString(), e.Font);
e.Graphics.FillRectangle(bgBrush, e.Bounds);
e.Graphics.DrawRectangle(new Pen(borderColor), e.Bounds);
e.Graphics.DrawString(itemText, e.Font, itemBrush, e.Bounds.Left + (e.Bounds.Width / 22 - size.Width / 44), e.Bounds.Top + (e.Bounds.Height / 2 - size.Height / 2));
if (e.Index == _MouseIndex)
{
e.Graphics.FillRectangle(overlay, e.Bounds);
e.Graphics.DrawString(itemText, e.Font, itemBrush, e.Bounds.Left + (e.Bounds.Width / 22 - size.Width / 44), e.Bounds.Top + (e.Bounds.Height / 2 - size.Height / 2));
}
}
}
{
int index = listBoxTracks.IndexFromPoint(e.Location);
if (index != _MouseIndex)
{
_MouseIndex = index;
listBoxTracks.Invalidate();
}
}
{
if (_MouseIndex > -1)
{
_MouseIndex = -1;
listBoxTracks.Invalidate();
}
}
I expect the output to be non-flickery, since the methods I used should prevent this.
I had a hard time to register it to show since internal recording using Camtasia, Nvidia's overlay nor my iPhone SE could show it afterwards in the recording. Mirillis Action! however did on 144fps, see: https://i.stack.imgur.com/Onq8C.jpg
Sourcecode here: https://mega.nz/#!kIJlAQ5S!l9hTXxYE0lcg4iNH3TUcIly3TaDCrtWlPs1xKttS7ws