Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -654,12 +654,23 @@ protected override void OnVisualStylesModeChanged(EventArgs e)
ResetComboAdapter();
ResetHeightCache();

bool recreateSystemHandle = FlatStyle == FlatStyle.System
&& IsHandleCreated;

// Crossing the modern/classic boundary is reported as VisualStylesModeChangeImpact.Recreate
// (see GetVisualStylesModeChangeImpact), so the base recreates the handle here. That is the
// only clean way to unwind the modern native-window state (the WM_NCCALCSIZE client
// expansion and the per-handle modern baseline); a fresh classic handle then behaves exactly
// as before, and a fresh modern handle captures a clean baseline.
// FlatStyle.System stays native, but an existing native handle still needs a rebuild when
// visual-style mode changes to avoid stale geometry in designer/runtime transitions.
base.OnVisualStylesModeChanged(e);

if (recreateSystemHandle && IsHandleCreated)
{
RecreateHandle();
}

ApplyModernComboLayout();
RefreshModernDropDownCornerPreference();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,15 +530,26 @@ public partial FlatStyle FlatStyle
return;
}

bool usedModernMetrics = UsesModernComboAdapter;
bool previousUsesModernMetrics = UsesModernComboAdapter;
_flatStyle = value;
ResetComboAdapter();

if (usedModernMetrics != UsesModernComboAdapter)
bool currentUsesModernMetrics = UsesModernComboAdapter;
bool modernMetricsChanged = previousUsesModernMetrics != currentUsesModernMetrics;

if (modernMetricsChanged)
{
ResetHeightCache();
CommonProperties.xClearPreferredSizeCache(this);
ApplyModernComboLayout();

if (IsHandleCreated)
{
RecreateHandle();
}
else
{
ApplyModernComboLayout();
}

LayoutTransaction.DoLayout(
this,
Expand All @@ -553,8 +564,11 @@ public partial FlatStyle FlatStyle
PropertyNames.FlatStyle);
}
}
else
{
ApplyModernComboLayout();
}

ApplyModernComboLayout();
RefreshModernDropDownCornerPreference();
Invalidate();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,64 @@ public void ComboBox_ModernVisualStyles_SystemModeChangeDoesNotRequestLayout()
Assert.False(control.IsHandleCreated);
}

[WinFormsFact]
public void ComboBox_ModernVisualStyles_FlatStyleTransitionToSystem_RecreatesHandle()
{
using SystemVisualSettingsTestScope settingsScope = new(
clientAreaAnimationEnabled: false,
highContrastEnabled: false);
using Panel parent = new();
using VisualStylesComboBox control = new()
{
FlatStyle = FlatStyle.Standard,
VisualStylesMode = VisualStylesMode.Net11
};
parent.Controls.Add(control);
parent.CreateControl();
control.CreateControl();

int handleCreatedCallCount = 0;
int handleDestroyedCallCount = 0;
control.HandleCreated += (sender, e) => handleCreatedCallCount++;
control.HandleDestroyed += (sender, e) => handleDestroyedCallCount++;

control.FlatStyle = FlatStyle.System;

Assert.True(control.IsHandleCreated);
Assert.Equal(1, handleDestroyedCallCount);
Assert.Equal(1, handleCreatedCallCount);
Assert.IsType<FlatComboAdapter>(control.CreateAdapter());
}

[WinFormsFact]
public void ComboBox_ModernVisualStyles_SystemModeBoundaryChangeWithHandle_RecreatesHandle()
{
using SystemVisualSettingsTestScope settingsScope = new(
clientAreaAnimationEnabled: false,
highContrastEnabled: false);
using Panel parent = new();
using VisualStylesComboBox control = new()
{
FlatStyle = FlatStyle.System,
VisualStylesMode = VisualStylesMode.Classic
};
parent.Controls.Add(control);
parent.CreateControl();
control.CreateControl();

int handleCreatedCallCount = 0;
int handleDestroyedCallCount = 0;
control.HandleCreated += (sender, e) => handleCreatedCallCount++;
control.HandleDestroyed += (sender, e) => handleDestroyedCallCount++;

control.VisualStylesMode = VisualStylesMode.Net11;

Assert.True(control.IsHandleCreated);
Assert.Equal(1, handleDestroyedCallCount);
Assert.Equal(1, handleCreatedCallCount);
Assert.IsType<FlatComboAdapter>(control.CreateAdapter());
}

[WinFormsFact]
public void ComboBox_ModernVisualStyles_ModeChangeRemeasuresAutoSizeRow()
{
Expand Down