Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make the TreeView respect the DoubleBuffered property #7403

Merged
merged 6 commits into from Jul 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 28 additions & 1 deletion src/System.Windows.Forms/src/System/Windows/Forms/TreeView.cs
Expand Up @@ -76,6 +76,7 @@ public partial class TreeView : Control
private const int TREEVIEWSTATE_lastControlValidated = 0x00004000;
private const int TREEVIEWSTATE_stopResizeWindowMsgs = 0x00008000;
private const int TREEVIEWSTATE_ignoreSelects = 0x00010000;
private const int TREEVIEWSTATE_doubleBufferedPropertySet = 0x00020000;

// PERF: take all the bools and put them into a state variable
private Collections.Specialized.BitVector32 treeViewState; // see TREEVIEWSTATE_ consts above
Expand Down Expand Up @@ -436,7 +437,15 @@ protected override Size DefaultSize
protected override bool DoubleBuffered
{
get => base.DoubleBuffered;
set => base.DoubleBuffered = value;
set
{
if (DoubleBuffered != value)
{
base.DoubleBuffered = value;
treeViewState[TREEVIEWSTATE_doubleBufferedPropertySet] = true;
UpdateTreeViewExtendedStyles();
0xC0000054 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

/// <summary>
Expand Down Expand Up @@ -1959,6 +1968,9 @@ protected override void OnHandleCreated(EventArgs e)

base.OnHandleCreated(e);

// The TreeView extended styles are independent of the window extended styles.
UpdateTreeViewExtendedStyles();

int version = (int)User32.SendMessageW(this, (User32.WM)CCM.GETVERSION);
if (version < 5)
{
Expand Down Expand Up @@ -2730,6 +2742,21 @@ internal override void UpdateStylesCore()
}
}

private void UpdateTreeViewExtendedStyles()
{
if (!IsHandleCreated)
{
return;
}

// Only set the TVS_EX_DOUBLEBUFFER style if the DoubleBuffered property setter has been executed.
// This stops the style from being removed for any derived classes that set it using P/Invoke.
Comment on lines +2752 to +2753
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! 👍

if (treeViewState[TREEVIEWSTATE_doubleBufferedPropertySet])
{
User32.SendMessageW(this, (User32.WM)TVM.SETEXTENDEDSTYLE, (nint)TVS_EX.DOUBLEBUFFER, (nint)(DoubleBuffered ? TVS_EX.DOUBLEBUFFER : 0));
}
}

/// <remarks>
/// Setting the TVS.CHECKBOXES style clears the checked state
/// </remarks>
Expand Down