Skip to content

Commit 9f4acfd

Browse files
authored
[Dark Mode] Drawing fixes + enhancements (#225)
* Revamped Theme and Dark Mode draw methods. With a focus on focus. - Added VisualStyleRenderer extensions - Re-organized extension classes to mirror internal .NET classes and API - More intuitive usage: use `e.DrawTheme...`, which will call `e.Draw...` when using default theme and `e.DrawDark...` when using Dark Mode. - More consistent `FocusRectangle` and Highlighting. Remaining known unhandled cases: - ListBox + CheckListBox - DropDownList ComboBox * Fix incorrectly commented out DrawText call Re-commented out default (non-themed) DrawText. * Updated per review - PreferencesDialog Languages ListBox : in most cases, draw active focus instead of inactive - SplitButton : draw Focus if Focused && ShowFocusCues - DarkControlPaint : removed
1 parent 048f62d commit 9f4acfd

File tree

18 files changed

+872
-414
lines changed

18 files changed

+872
-414
lines changed

ComicRack.Engine/Controls/ListStyles.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ private static void DrawItem(object sender, DrawListViewItemEventArgs e)
5858
{
5959
e.DrawDefault = false;
6060
e.DrawBackground();
61-
using (e.Graphics.SaveState())
61+
// ListView is a Win32 control; OS is responsible for some aspects of drawing, independent of .NET version
62+
// This includes which parts are handling in DrawItem/DrawSubItem. Replacement methods need testing on Win10 + Win11.
63+
//e.DrawThemeBackground();
64+
using (e.Graphics.SaveState())
6265
{
6366
e.Graphics.SetClip(e.Item.Bounds, CombineMode.Intersect);
6467
StyledRenderer.AlphaStyle alphaStyle = StyledRenderer.GetAlphaStyle(e.Item.Selected, hot: false, e.Item.Focused);

ComicRack.Engine/Controls/RatingControl.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ protected override void OnPaint(PaintEventArgs e)
197197
VisualStyleElement element = (Focused ? VisualStyleElement.TextBox.TextEdit.Focused : VisualStyleElement.TextBox.TextEdit.Normal);
198198
VisualStyleRenderer visualStyleRenderer = new VisualStyleRenderer(element);
199199
//visualStyleRenderer.DrawBackground(graphics, base.ClientRectangle);
200-
visualStyleRenderer.DrawThemeBackground(e, base.ClientRectangle, BackColor);
200+
visualStyleRenderer.DrawThemeBackground(graphics, base.ClientRectangle);
201201
}
202202
DrawContent(graphics);
203203
return;

ComicRack.Engine/Controls/ThumbnailControl.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using cYo.Common.ComponentModel;
1010
using cYo.Common.Drawing;
1111
using cYo.Common.Windows.Forms;
12+
using cYo.Common.Windows.Forms.Theme;
1213
using cYo.Projects.ComicRack.Engine.Drawing;
1314
using cYo.Projects.ComicRack.Engine.IO;
1415
using cYo.Projects.ComicRack.Engine.IO.Cache;
@@ -524,8 +525,9 @@ protected override void OnPaint(PaintEventArgs e)
524525
}
525526
if (base.DesignMode)
526527
{
527-
ControlPaint.DrawFocusRectangle(e.Graphics, base.ClientRectangle);
528-
}
528+
//ControlPaint.DrawFocusRectangle(e.Graphics, base.ClientRectangle);
529+
ControlPaintEx.DrawFocusRectangle(e.Graphics, base.ClientRectangle);
530+
}
529531
}
530532
}
531533
finally

ComicRack/Dialogs/PreferencesDialog.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,8 @@ private void lbLanguages_DrawItem(object sender, DrawItemEventArgs e)
539539
return;
540540
}
541541
TRInfo tRInfo = (TRInfo)lbLanguages.Items[e.Index];
542-
e.DrawBackground();
542+
//e.DrawBackground();
543+
e.DrawThemeBackground(focused: ActiveForm == this);
543544
using (Brush brush = new SolidBrush((tRInfo.CompletionPercent > 95f) ? ForeColor : Color.Red))
544545
{
545546
Rectangle bounds = e.Bounds;
@@ -569,10 +570,14 @@ private void lbLanguages_DrawItem(object sender, DrawItemEventArgs e)
569570
}
570571
}
571572
}
572-
if ((e.State & DrawItemState.Focus) != 0)
573-
{
574-
ControlPaintEx.DrawFocusRectangle(e.Graphics, e.Bounds);
575-
}
573+
//if ((e.State & DrawItemState.Focus) != 0)
574+
//{
575+
// ControlPaint.DrawFocusRectangle(e.Graphics, e.Bounds);
576+
//}
577+
578+
// Default theme : ControlPaint.DrawFocusRectangle() -> e.DrawFocusRectangle()
579+
// e.DrawFocusRectangle() considers DrawItemState.NoFocusRect + DrawItemState.Focus
580+
e.DrawThemeFocusRectangle(focused: ActiveForm == this);
576581
}
577582

578583
private void btBackupDatabase_Click(object sender, EventArgs e)

ComicRack/Dialogs/TasksDialog.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,10 @@ private void lvTasks_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
309309
Rectangle bounds = e.Bounds;
310310
bounds.Width = Math.Min(bounds.Width, bounds.Width * progressState.ProgressPercentage / 100);
311311
bounds.Height--;
312-
e.DrawBackground();
313-
e.Graphics.DrawStyledRectangle(bounds, StyledRenderer.AlphaStyle.Hot, Color.Green, StyledRenderer.Default.Frame(0, 1));
312+
//e.DrawBackground();
313+
e.DrawThemeBackground();
314+
// TODO : tweak so that this is more visible in Dark Mode
315+
e.Graphics.DrawStyledRectangle(bounds, StyledRenderer.AlphaStyle.Hot, Color.Green, StyledRenderer.Default.Frame(0, 1));
314316
ListViewItem.ListViewSubItem listViewSubItem = e.Item.SubItems[1];
315317
using (StringFormat format = new StringFormat
316318
{

ComicRack/Views/ComicBrowserControl.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3252,13 +3252,13 @@ private void toolTip_Draw(object sender, DrawToolTipEventArgs e)
32523252
{
32533253
VisualStyleRenderer visualStyleRenderer = new VisualStyleRenderer(normal);
32543254
//visualStyleRenderer.DrawBackground(e.Graphics, e.Bounds);
3255-
visualStyleRenderer.DrawThemeBackground(e);
3255+
visualStyleRenderer.DrawThemeBackground(e.Graphics, e.Bounds);
32563256
}
32573257
else
32583258
{
3259-
e.DrawBackground();
3259+
//e.DrawBackground();
32603260
//e.DrawBorder();
3261-
//e.DrawThemeBackground();
3261+
e.DrawThemeBackground();
32623262
e.DrawThemeBorder();
32633263
}
32643264
Rectangle bounds = e.Bounds;

cYo.Common.Windows/Forms/CheckedListBoxEx.cs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,13 @@ protected override void OnDrawItem(DrawItemEventArgs e)
3939
}
4040
if (!customDrawing)
4141
{
42+
// This doesn't appear to be used. Will need theme handling if that changes.
4243
base.OnDrawItem(e);
4344
return;
4445
}
45-
using (Brush brush = new SolidBrush(BackColor))
46-
{
47-
e.Graphics.FillRectangle(brush, e.Bounds);
48-
}
49-
CheckState itemCheckState = GetItemCheckState(e.Index);
46+
// Provide control focus state to use alongside item state
47+
e.DrawThemeBackground(focused: this.Focused);
48+
CheckState itemCheckState = GetItemCheckState(e.Index);
5049
Size size;
5150
if (Application.RenderWithVisualStyles)
5251
{
@@ -62,16 +61,15 @@ protected override void OnDrawItem(DrawItemEventArgs e)
6261
Rectangle rectangle = new Rectangle(e.Bounds.X + 1, e.Bounds.Y + (e.Bounds.Height - size.Height) / 2, size.Width, size.Height);
6362
ControlPaint.DrawCheckBox(e.Graphics, rectangle, state2);
6463
}
65-
Rectangle rectangle2 = new Rectangle(e.Bounds.X + size.Width + 2, e.Bounds.Y, e.Bounds.Width - (size.Width + 2), e.Bounds.Height);
66-
using (Brush brush2 = new SolidBrush(e.BackColor))
67-
{
68-
e.Graphics.FillRectangle(brush2, rectangle2);
69-
}
70-
OnDrawItemText(new DrawItemEventArgs(e.Graphics, e.Font, rectangle2, e.Index, e.State, e.ForeColor, e.BackColor));
71-
if ((e.State & DrawItemState.Focus) != 0 && (e.State & DrawItemState.NoFocusRect) == 0)
72-
{
73-
ControlPaintEx.DrawFocusRectangle(e.Graphics, rectangle2);
74-
}
64+
Rectangle textRectangle = new Rectangle(e.Bounds.X + size.Width + 2, e.Bounds.Y, e.Bounds.Width - (size.Width + 2), e.Bounds.Height);
65+
OnDrawItemText(new DrawItemEventArgs(e.Graphics, e.Font, textRectangle, e.Index, e.State, e.ForeColor, e.BackColor)); // BackColor is unused
66+
// This doesn't accurately draw FocusRectangle in Dark Mode (and probably Light mode)
67+
//if ((e.State & DrawItemState.Focus) != 0 && (e.State & DrawItemState.NoFocusRect) == 0)
68+
//{
69+
// ControlPaint.DrawFocusRectangle(e.Graphics, textRectangle);
70+
//}
71+
// Use extension method instead, providing control focus state to use alongside item state
72+
e.DrawThemeFocusRectangle(textRectangle, focused: this.Focused);
7573
}
7674

7775
protected override void OnMouseDown(MouseEventArgs e)

cYo.Common.Windows/Forms/CollapsibleGroupBox.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ protected override void OnPaintBackground(PaintEventArgs e)
256256
{
257257
VisualStyleRenderer visualStyleRenderer = new VisualStyleRenderer(VisualStyleElement.Tab.Body.Normal);
258258
//visualStyleRenderer.DrawBackground(e.Graphics, base.ClientRectangle);
259-
visualStyleRenderer.DrawThemeBackground(e, base.ClientRectangle, BackColor);
259+
visualStyleRenderer.DrawThemeBackground(e.Graphics, base.ClientRectangle);
260260
}
261261
}
262262

cYo.Common.Windows/Forms/ComboBoxSkinner.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,11 @@ private void comboBox_DrawItem(object sender, DrawItemEventArgs e)
288288
bool flag = (e.State & DrawItemState.ComboBoxEdit) != 0;
289289
bool flag2 = comboBoxItem != null && comboBoxItem.IsSeparator && !flag && e.Index > 0;
290290

291-
e.DrawThemeBackground();
292-
//e.DrawFocusRectangle();
293-
//e.DrawThemeBackground();
294-
e.DrawThemeFocusRectangle(); // override SelectedText highlighting
291+
//e.DrawBackground();
292+
//e.DrawFocusRectangle();
293+
// Provide control focus state to use alongside item state
294+
e.DrawThemeBackground(focused: comboBox.Focused);
295+
e.DrawThemeFocusRectangle(focused: comboBox.Focused);
295296

296297
using (Brush brush = new SolidBrush(e.ForeColor))
297298
{

cYo.Common.Windows/Forms/SimpleColorPicker.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Drawing;
44
using System.Windows.Forms;
55
using cYo.Common.Text;
6+
using cYo.Common.Windows.Forms.Theme;
67

78
namespace cYo.Common.Windows.Forms
89
{
@@ -56,8 +57,11 @@ protected override void OnDrawItem(DrawItemEventArgs e)
5657
{
5758
base.OnDrawItem(e);
5859
Graphics graphics = e.Graphics;
59-
e.DrawBackground();
60-
using (StringFormat format = new StringFormat
60+
//e.DrawBackground();
61+
e.DrawThemeBackground();
62+
// does not draw a FocusRectangle in light mode, maintaining current functionality
63+
e.DrawThemeFocusRectangle();
64+
using (StringFormat format = new StringFormat
6165
{
6266
Alignment = StringAlignment.Near,
6367
LineAlignment = StringAlignment.Center

0 commit comments

Comments
 (0)