Skip to content

Commit 68e7712

Browse files
Fix disabled ComboBox BackColor issue.
Fix disabled RichTextBox BackColor issue.
1 parent e8ec828 commit 68e7712

File tree

3 files changed

+97
-99
lines changed

3 files changed

+97
-99
lines changed
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +0,0 @@
1-
override System.Windows.Forms.ComboBox.OnEnabledChanged(System.EventArgs! e) -> void
2-
override System.Windows.Forms.RichTextBox.OnEnabledChanged(System.EventArgs! e) -> void

src/System.Windows.Forms/System/Windows/Forms/Controls/ComboBox/ComboBox.cs

Lines changed: 58 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1857,11 +1857,8 @@ protected override void Dispose(bool disposing)
18571857
_autoCompleteCustomSource.CollectionChanged -= OnAutoCompleteCustomSourceChanged;
18581858
}
18591859

1860-
if (_stringSource is not null)
1861-
{
1862-
_stringSource.ReleaseAutoComplete();
1863-
_stringSource = null;
1864-
}
1860+
_stringSource?.ReleaseAutoComplete();
1861+
_stringSource = null;
18651862
}
18661863

18671864
base.Dispose(disposing);
@@ -2400,11 +2397,8 @@ protected override void OnHandleDestroyed(EventArgs e)
24002397
_selectedIndex = SelectedIndex;
24012398
}
24022399

2403-
if (_stringSource is not null)
2404-
{
2405-
_stringSource.ReleaseAutoComplete();
2406-
_stringSource = null;
2407-
}
2400+
_stringSource?.ReleaseAutoComplete();
2401+
_stringSource = null;
24082402

24092403
base.OnHandleDestroyed(e);
24102404
}
@@ -3058,23 +3052,14 @@ protected override void RefreshItem(int index)
30583052
/// </summary>
30593053
private void ReleaseChildWindow()
30603054
{
3061-
if (_childEdit is not null)
3062-
{
3063-
_childEdit.ReleaseHandle();
3064-
_childEdit = null;
3065-
}
3055+
_childEdit?.ReleaseHandle();
3056+
_childEdit = null;
30663057

3067-
if (_childListBox is not null)
3068-
{
3069-
_childListBox.ReleaseHandle();
3070-
_childListBox = null;
3071-
}
3058+
_childListBox?.ReleaseHandle();
3059+
_childListBox = null;
30723060

3073-
if (_childDropDown is not null)
3074-
{
3075-
_childDropDown.ReleaseHandle();
3076-
_childDropDown = null;
3077-
}
3061+
_childDropDown?.ReleaseHandle();
3062+
_childDropDown = null;
30783063
}
30793064

30803065
internal override void ReleaseUiaProvider(HWND handle)
@@ -3692,13 +3677,11 @@ protected override unsafe void WndProc(ref Message m)
36923677

36933678
break;
36943679

3695-
case PInvokeCore.WM_CTLCOLOREDIT:
3696-
// Only handle if the ComboBox is disabled
3697-
#pragma warning disable WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
3698-
if (IsHandleCreated
3699-
&& !Enabled
3700-
&& Application.IsDarkModeEnabled
3701-
&& DarkModeRequestState is true)
3680+
#pragma warning disable WFO5001
3681+
case PInvokeCore.WM_CTLCOLORSTATIC:
3682+
3683+
HWND hwndChild = (HWND)m.LParamInternal;
3684+
if (hwndChild == _childEdit?.HWND && Application.IsDarkModeEnabled)
37023685
{
37033686
PInvokeCore.SetBkColor(
37043687
(HDC)m.WParamInternal,
@@ -3709,14 +3692,31 @@ protected override unsafe void WndProc(ref Message m)
37093692
ColorTranslator.ToWin32(Color.FromArgb(180, 180, 180)));
37103693

37113694
m.ResultInternal = (LRESULT)s_darkEditBrush;
3695+
return;
37123696
}
3713-
else
3697+
3698+
// Additional handling for Simple style listbox when disabled
3699+
if (DropDownStyle == ComboBoxStyle.Simple && Application.IsDarkModeEnabled && !Enabled)
37143700
{
3715-
m.ResultInternal = (LRESULT)(nint)InitializeDCForWmCtlColor((HDC)(nint)m.WParamInternal, m.MsgInternal);
3701+
if (hwndChild == _childListBox?.HWND)
3702+
{
3703+
PInvokeCore.SetBkColor(
3704+
(HDC)m.WParamInternal,
3705+
ColorTranslator.ToWin32(Color.FromArgb(64, 64, 64)));
3706+
3707+
PInvokeCore.SetTextColor(
3708+
(HDC)m.WParamInternal,
3709+
ColorTranslator.ToWin32(Color.FromArgb(180, 180, 180)));
3710+
3711+
m.ResultInternal = (LRESULT)s_darkEditBrush;
3712+
return;
3713+
}
37163714
}
3717-
#pragma warning restore WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
3715+
#pragma warning restore WFO5001
37183716

37193717
break;
3718+
3719+
case PInvokeCore.WM_CTLCOLOREDIT:
37203720
case PInvokeCore.WM_CTLCOLORLISTBOX:
37213721
m.ResultInternal = (LRESULT)(nint)InitializeDCForWmCtlColor((HDC)(nint)m.WParamInternal, m.MsgInternal);
37223722
break;
@@ -3811,6 +3811,29 @@ protected override unsafe void WndProc(ref Message m)
38113811
using Graphics g = Graphics.FromHdcInternal((IntPtr)dc);
38123812
FlatComboBoxAdapter.DrawFlatCombo(this, g);
38133813

3814+
#pragma warning disable WFO5001
3815+
// Special handling for disabled DropDownList in dark mode
3816+
if (Application.IsDarkModeEnabled && !Enabled && DropDownStyle == ComboBoxStyle.DropDownList)
3817+
{
3818+
// The text area for DropDownList (excluding the dropdown button)
3819+
Rectangle textBounds = ClientRectangle;
3820+
textBounds.Width -= SystemInformation.VerticalScrollBarWidth;
3821+
3822+
// Fill the background
3823+
using var bgBrush = new SolidBrush(Color.FromArgb(64, 64, 64));
3824+
g.FillRectangle(bgBrush, textBounds);
3825+
3826+
// Draw the text
3827+
TextRenderer.DrawText(
3828+
g,
3829+
Text,
3830+
Font,
3831+
textBounds,
3832+
Color.FromArgb(180, 180, 180),
3833+
TextFormatFlags.Left | TextFormatFlags.VerticalCenter | TextFormatFlags.EndEllipsis);
3834+
}
3835+
#pragma warning restore WFO5001
3836+
38143837
return;
38153838
}
38163839

src/System.Windows.Forms/System/Windows/Forms/Controls/RichTextBox/RichTextBox.cs

Lines changed: 39 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,14 +1320,9 @@ public override string Text
13201320

13211321
if (!IsHandleCreated && _textRtf is null)
13221322
{
1323-
if (_textPlain is not null)
1324-
{
1325-
return _textPlain;
1326-
}
1327-
else
1328-
{
1329-
return base.Text;
1330-
}
1323+
return _textPlain is not null
1324+
? _textPlain
1325+
: base.Text;
13311326
}
13321327
else
13331328
{
@@ -1457,14 +1452,10 @@ public unsafe float ZoomFactor
14571452
int numerator = 0;
14581453
int denominator = 0;
14591454
PInvokeCore.SendMessage(this, PInvokeCore.EM_GETZOOM, (WPARAM)(&numerator), ref denominator);
1460-
if ((numerator != 0) && (denominator != 0))
1461-
{
1462-
_zoomMultiplier = numerator / ((float)denominator);
1463-
}
1464-
else
1465-
{
1466-
_zoomMultiplier = 1.0f;
1467-
}
1455+
1456+
_zoomMultiplier = (numerator != 0) && (denominator != 0)
1457+
? numerator / ((float)denominator)
1458+
: 1.0f;
14681459

14691460
return _zoomMultiplier;
14701461
}
@@ -2412,24 +2403,6 @@ protected override void OnGotFocus(EventArgs e)
24122403
}
24132404
}
24142405

2415-
protected override void OnEnabledChanged(EventArgs e)
2416-
{
2417-
base.OnEnabledChanged(e);
2418-
HandleDarkModeDisabledBackground();
2419-
}
2420-
2421-
private void HandleDarkModeDisabledBackground()
2422-
{
2423-
#pragma warning disable WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
2424-
if (Application.IsDarkModeEnabled
2425-
&& !Enabled
2426-
&& DarkModeRequestState is true)
2427-
{
2428-
Invalidate();
2429-
}
2430-
#pragma warning restore WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
2431-
}
2432-
24332406
protected override void OnHandleCreated(EventArgs e)
24342407
{
24352408
// base.OnHandleCreated is called somewhere in the middle of this
@@ -2476,8 +2449,6 @@ protected override void OnHandleCreated(EventArgs e)
24762449
// base sets the Text property. It's important to do this *after* setting EM_AUTOUrlDETECT.
24772450
base.OnHandleCreated(e);
24782451

2479-
HandleDarkModeDisabledBackground();
2480-
24812452
// For some reason, we need to set the OleCallback before setting the RTF property.
24822453
UpdateOleCallback();
24832454

@@ -2706,14 +2677,9 @@ private void SendZoomFactor(float zoom)
27062677
PInvokeCore.SendMessage(this, PInvokeCore.EM_SETZOOM, (WPARAM)numerator, (LPARAM)denominator);
27072678
}
27082679

2709-
if (numerator != 0)
2710-
{
2711-
_zoomMultiplier = numerator / ((float)denominator);
2712-
}
2713-
else
2714-
{
2715-
_zoomMultiplier = 1.0f;
2716-
}
2680+
_zoomMultiplier = numerator != 0
2681+
? numerator / ((float)denominator)
2682+
: 1.0f;
27172683
}
27182684

27192685
private unsafe bool SetCharFormat(CFM_MASK mask, CFE_EFFECTS effect, RichTextBoxSelectionAttribute charFormat)
@@ -2907,14 +2873,9 @@ private void StreamIn(Stream data, uint flags)
29072873
// set up structure to do stream operation
29082874
EDITSTREAM es = default;
29092875

2910-
if ((flags & PInvoke.SF_UNICODE) != 0)
2911-
{
2912-
cookieVal = INPUT | UNICODE;
2913-
}
2914-
else
2915-
{
2916-
cookieVal = INPUT | ANSI;
2917-
}
2876+
cookieVal = (flags & PInvoke.SF_UNICODE) != 0
2877+
? INPUT | UNICODE
2878+
: INPUT | ANSI;
29182879

29192880
if ((flags & PInvoke.SF_RTF) != 0)
29202881
{
@@ -3461,26 +3422,42 @@ private void WmSetFont(ref Message m)
34613422
InternalSetForeColor(ForeColor);
34623423
}
34633424

3464-
protected override void WndProc(ref Message m)
3425+
protected override unsafe void WndProc(ref Message m)
34653426
{
34663427
switch (m.MsgInternal)
34673428
{
34683429
#pragma warning disable WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
34693430
case PInvokeCore.WM_PAINT:
3470-
if (IsHandleCreated && !Enabled)
3431+
if (Handle == m.HWND
3432+
&& !Enabled
3433+
&& Application.IsDarkModeEnabled)
34713434
{
3472-
if (Application.IsDarkModeEnabled && DarkModeRequestState is true)
3473-
{
3474-
// If the control is in dark mode, we need to paint the background
3475-
// with the dark mode color.
3476-
using Graphics g = Graphics.FromHwnd(Handle);
3477-
g.Clear(SystemColors.Control);
3478-
}
3435+
base.WndProc(ref m);
3436+
3437+
// If the control is disabled, we don't want to let the RTF control
3438+
// paint anything else. We will paint the background and the text
3439+
// ourselves, so we don't want the RTF control to paint the background
3440+
// and the text in the foreground color.
3441+
using Graphics g = Graphics.FromHwndInternal(Handle);
3442+
3443+
// Paint the background
3444+
g.FillRectangle(SystemBrushes.ControlDark, ClientRectangle);
3445+
3446+
// Paint the text
3447+
TextRenderer.DrawText(
3448+
g,
3449+
Text,
3450+
Font,
3451+
ClientRectangle,
3452+
SystemColors.GrayText,
3453+
TextFormatFlags.Left | TextFormatFlags.VerticalCenter);
3454+
3455+
return;
34793456
}
34803457

34813458
base.WndProc(ref m);
3482-
break;
34833459

3460+
break;
34843461
#pragma warning restore WFO5001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
34853462

34863463
case MessageId.WM_REFLECT_NOTIFY:

0 commit comments

Comments
 (0)