Fix visual styles exception

This commit is contained in:
pointfeev 2024-07-22 03:04:50 -04:00
parent 80b684e95b
commit 25f626aa29

View file

@ -73,6 +73,7 @@ internal sealed class CustomTreeView : TreeView
TreeNode node = e.Node; TreeNode node = e.Node;
if (node is not { IsVisible: true }) if (node is not { IsVisible: true })
return; return;
bool highlighted = (e.State & TreeNodeStates.Selected) == TreeNodeStates.Selected && Focused; bool highlighted = (e.State & TreeNodeStates.Selected) == TreeNodeStates.Selected && Focused;
Graphics graphics = e.Graphics; Graphics graphics = e.Graphics;
backBrush ??= new(BackColor); backBrush ??= new(BackColor);
@ -80,14 +81,17 @@ internal sealed class CustomTreeView : TreeView
Brush brush = highlighted ? SystemBrushes.Highlight : backBrush; Brush brush = highlighted ? SystemBrushes.Highlight : backBrush;
Rectangle bounds = node.Bounds; Rectangle bounds = node.Bounds;
Rectangle selectionBounds = bounds; Rectangle selectionBounds = bounds;
Form form = FindForm(); Form form = FindForm();
if (form is not SelectForm and not SelectDialogForm) if (form is not SelectForm and not SelectDialogForm)
return; return;
string id = node.Name; string id = node.Name;
Platform platform = (node.Tag as Platform?).GetValueOrDefault(Platform.None); Platform platform = (node.Tag as Platform?).GetValueOrDefault(Platform.None);
DLCType dlcType = (node.Tag as DLCType?).GetValueOrDefault(DLCType.None); DLCType dlcType = (node.Tag as DLCType?).GetValueOrDefault(DLCType.None);
if (string.IsNullOrWhiteSpace(id) || platform is Platform.None && dlcType is DLCType.None) if (string.IsNullOrWhiteSpace(id) || platform is Platform.None && dlcType is DLCType.None)
return; return;
Color color = highlighted Color color = highlighted
? C1 ? C1
: Enabled : Enabled
@ -99,8 +103,7 @@ internal sealed class CustomTreeView : TreeView
SelectionDLC dlc = SelectionDLC.FromId(dlcType, node.Parent?.Name, id); SelectionDLC dlc = SelectionDLC.FromId(dlcType, node.Parent?.Name, id);
text = dlc?.Selection != null ? dlc.Selection.Platform.ToString() : dlcType.ToString(); text = dlc?.Selection != null ? dlc.Selection.Platform.ToString() : dlcType.ToString();
} }
else else text = platform.ToString();
text = platform.ToString();
Size size = TextRenderer.MeasureText(graphics, text, font); Size size = TextRenderer.MeasureText(graphics, text, font);
bounds = bounds with { X = bounds.X + bounds.Width, Width = size.Width }; bounds = bounds with { X = bounds.X + bounds.Width, Width = size.Width };
@ -108,6 +111,7 @@ internal sealed class CustomTreeView : TreeView
graphics.FillRectangle(brush, bounds); graphics.FillRectangle(brush, bounds);
Point point = new(bounds.Location.X - 1, bounds.Location.Y + 1); Point point = new(bounds.Location.X - 1, bounds.Location.Y + 1);
TextRenderer.DrawText(graphics, text, font, point, color, TextFormatFlags.Default); TextRenderer.DrawText(graphics, text, font, point, color, TextFormatFlags.Default);
if (platform is not Platform.Paradox) if (platform is not Platform.Paradox)
{ {
color = highlighted color = highlighted
@ -126,7 +130,7 @@ internal sealed class CustomTreeView : TreeView
TextRenderer.DrawText(graphics, text, font, point, color, TextFormatFlags.Default); TextRenderer.DrawText(graphics, text, font, point, color, TextFormatFlags.Default);
} }
if (form is SelectForm && ComboBoxRenderer.IsSupported) if (form is SelectForm)
{ {
Selection selection = Selection.FromId(platform, id); Selection selection = Selection.FromId(platform, id);
if (selection is not null && selection.CanUseProxy) if (selection is not null && selection.CanUseProxy)
@ -150,6 +154,7 @@ internal sealed class CustomTreeView : TreeView
graphics.FillRectangle(backBrush, bounds); graphics.FillRectangle(backBrush, bounds);
point = new(bounds.Left, bounds.Top + bounds.Height / 2 - size.Height / 2 - 1); point = new(bounds.Left, bounds.Top + bounds.Height / 2 - size.Height / 2 - 1);
CheckBoxRenderer.DrawCheckBox(graphics, point, checkBoxState); CheckBoxRenderer.DrawCheckBox(graphics, point, checkBoxState);
text = ProxyToggleString; text = ProxyToggleString;
size = TextRenderer.MeasureText(graphics, text, font); size = TextRenderer.MeasureText(graphics, text, font);
int left = 1; int left = 1;
@ -159,12 +164,16 @@ internal sealed class CustomTreeView : TreeView
graphics.FillRectangle(backBrush, bounds); graphics.FillRectangle(backBrush, bounds);
point = new(bounds.Location.X - 1 + left, bounds.Location.Y + 1); point = new(bounds.Location.X - 1 + left, bounds.Location.Y + 1);
TextRenderer.DrawText(graphics, text, font, point, Enabled ? C7 : C8, TextFormatFlags.Default); TextRenderer.DrawText(graphics, text, font, point, Enabled ? C7 : C8, TextFormatFlags.Default);
this.checkBoxBounds[selection] = RectangleToClient(checkBoxBounds); this.checkBoxBounds[selection] = RectangleToClient(checkBoxBounds);
if (selection.UseProxy) if (selection.UseProxy)
{ {
comboBoxFont ??= new(font.FontFamily, 6, font.Style, font.Unit, font.GdiCharSet, comboBoxFont ??= new(font.FontFamily, 6, font.Style, font.Unit, font.GdiCharSet,
font.GdiVerticalFont); font.GdiVerticalFont);
ComboBoxState comboBoxState = Enabled ? ComboBoxState.Normal : ComboBoxState.Disabled; ComboBoxState comboBoxState = Enabled ? ComboBoxState.Normal : ComboBoxState.Disabled;
ButtonState buttonState = Enabled ? ButtonState.Normal : ButtonState.Inactive;
text = (selection.Proxy ?? Selection.DefaultProxy) + ".dll"; text = (selection.Proxy ?? Selection.DefaultProxy) + ".dll";
size = TextRenderer.MeasureText(graphics, text, comboBoxFont) + new Size(6, 0); size = TextRenderer.MeasureText(graphics, text, comboBoxFont) + new Size(6, 0);
const int padding = 2; const int padding = 2;
@ -173,7 +182,18 @@ internal sealed class CustomTreeView : TreeView
selectionBounds.Size + bounds.Size with { Height = 0 }); selectionBounds.Size + bounds.Size with { Height = 0 });
Rectangle comboBoxBounds = bounds; Rectangle comboBoxBounds = bounds;
graphics.FillRectangle(backBrush, bounds); graphics.FillRectangle(backBrush, bounds);
if (ComboBoxRenderer.IsSupported)
ComboBoxRenderer.DrawTextBox(graphics, bounds, text, comboBoxFont, comboBoxState); ComboBoxRenderer.DrawTextBox(graphics, bounds, text, comboBoxFont, comboBoxState);
else
{
graphics.FillRectangle(SystemBrushes.ControlText, bounds);
ControlPaint.DrawButton(graphics, bounds, buttonState);
point = new(bounds.Location.X + 3 + bounds.Width / 2 - size.Width / 2,
bounds.Location.Y + bounds.Height / 2 - size.Height / 2);
TextRenderer.DrawText(graphics, text, comboBoxFont, point,
Enabled ? SystemColors.ControlText : SystemColors.GrayText, TextFormatFlags.Default);
}
size = new(14, 0); size = new(14, 0);
left = -1; left = -1;
bounds = bounds with { X = bounds.X + bounds.Width + left, Width = size.Width }; bounds = bounds with { X = bounds.X + bounds.Width + left, Width = size.Width };
@ -181,7 +201,11 @@ internal sealed class CustomTreeView : TreeView
selectionBounds.Size + new Size(bounds.Size.Width + left, 0)); selectionBounds.Size + new Size(bounds.Size.Width + left, 0));
comboBoxBounds = new(comboBoxBounds.Location, comboBoxBounds = new(comboBoxBounds.Location,
comboBoxBounds.Size + new Size(bounds.Size.Width + left, 0)); comboBoxBounds.Size + new Size(bounds.Size.Width + left, 0));
if (ComboBoxRenderer.IsSupported)
ComboBoxRenderer.DrawDropDownButton(graphics, bounds, comboBoxState); ComboBoxRenderer.DrawDropDownButton(graphics, bounds, comboBoxState);
else
ControlPaint.DrawComboButton(graphics, bounds, buttonState);
this.comboBoxBounds[selection] = RectangleToClient(comboBoxBounds); this.comboBoxBounds[selection] = RectangleToClient(comboBoxBounds);
} }
else else
@ -211,6 +235,7 @@ internal sealed class CustomTreeView : TreeView
if (e.Button is not MouseButtons.Left || !ComboBoxRenderer.IsSupported) if (e.Button is not MouseButtons.Left || !ComboBoxRenderer.IsSupported)
return; return;
if (comboBoxBounds.Count > 0 && selectForm is not null) if (comboBoxBounds.Count > 0 && selectForm is not null)
foreach (KeyValuePair<Selection, Rectangle> pair in comboBoxBounds) foreach (KeyValuePair<Selection, Rectangle> pair in comboBoxBounds)
if (!Selection.All.ContainsKey(pair.Key) || !pair.Key.CanUseProxy) if (!Selection.All.ContainsKey(pair.Key) || !pair.Key.CanUseProxy)