|
| 1 | +// ============================================================================ |
| 2 | +// PROJECT: Synix Game Server Control Panel |
| 3 | +// AUTHOR: Jason Turner (ubidzz) |
| 4 | +// COPYRIGHT: © 2026 All Rights Reserved. |
| 5 | +// ============================================================================ |
| 6 | +using System.Runtime.InteropServices; |
| 7 | + |
| 8 | +namespace Synix_Control_Panel.SynixApp.Design |
| 9 | +{ |
| 10 | + public static class SynixMenuStyler |
| 11 | + { |
| 12 | + private const int DwmWindowCornerPreference = 33; |
| 13 | + private const int DwmCornerPreferenceRound = 2; |
| 14 | + private static readonly Font MenuFont = new("Segoe UI", 11F, FontStyle.Regular); |
| 15 | + |
| 16 | + [DllImport("dwmapi.dll", CharSet = CharSet.Unicode, PreserveSig = false)] |
| 17 | + private static extern void DwmSetWindowAttribute( |
| 18 | + IntPtr window, |
| 19 | + int attribute, |
| 20 | + ref int value, |
| 21 | + uint valueSize); |
| 22 | + |
| 23 | + public static void Apply(ToolStripDropDown menu) |
| 24 | + { |
| 25 | + ArgumentNullException.ThrowIfNull(menu); |
| 26 | + menu.Renderer = new SynixMenuRenderer(); |
| 27 | + menu.BackColor = SettingsPalette.Card; |
| 28 | + menu.ForeColor = SettingsPalette.PrimaryText; |
| 29 | + menu.Font = MenuFont; |
| 30 | + if (menu is ContextMenuStrip contextMenu) |
| 31 | + contextMenu.ShowImageMargin = false; |
| 32 | + |
| 33 | + ApplyRoundedWindow(menu); |
| 34 | + foreach (ToolStripItem item in menu.Items) |
| 35 | + { |
| 36 | + item.BackColor = SettingsPalette.Card; |
| 37 | + item.ForeColor = SettingsPalette.PrimaryText; |
| 38 | + item.Padding = new Padding(0, 4, 0, 4); |
| 39 | + if (item is ToolStripDropDownItem dropDownItem && dropDownItem.HasDropDownItems) |
| 40 | + Apply(dropDownItem.DropDown); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + private static void ApplyRoundedWindow(ToolStripDropDown menu) |
| 45 | + { |
| 46 | + void ApplyDwm() |
| 47 | + { |
| 48 | + if (Environment.OSVersion.Version.Build < 22000) |
| 49 | + return; |
| 50 | + try |
| 51 | + { |
| 52 | + int preference = DwmCornerPreferenceRound; |
| 53 | + DwmSetWindowAttribute( |
| 54 | + menu.Handle, |
| 55 | + DwmWindowCornerPreference, |
| 56 | + ref preference, |
| 57 | + sizeof(int)); |
| 58 | + } |
| 59 | + catch (ExternalException) |
| 60 | + { |
| 61 | + // Windows can reject this visual hint; the renderer still supplies the theme. |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + if (menu.IsHandleCreated) |
| 66 | + ApplyDwm(); |
| 67 | + else |
| 68 | + menu.HandleCreated += (_, _) => ApplyDwm(); |
| 69 | + } |
| 70 | + } |
| 71 | +} |
0 commit comments