|
| 1 | +using System; |
| 2 | +using System.ComponentModel; |
| 3 | +using System.Drawing; |
| 4 | +using System.Drawing.Drawing2D; |
| 5 | +using System.Reflection; |
| 6 | +using System.Runtime.InteropServices; |
| 7 | +using System.Windows.Forms; |
| 8 | + |
| 9 | + |
| 10 | +namespace NativeDarkMode_Lib |
| 11 | +{ |
| 12 | + public class ComboBoxPainter : NativeWindow, IDisposable |
| 13 | + { |
| 14 | + // Public Properties for Customization |
| 15 | + [DefaultValue(typeof(Color), "Gray")] |
| 16 | + public Color BorderColor { get; set; } = Color.Gray; |
| 17 | + |
| 18 | + [DefaultValue(typeof(Color), "LightGray")] |
| 19 | + public Color ButtonColor { get; set; } = Color.FromArgb(99, 99, 99); |
| 20 | + |
| 21 | + [DefaultValue(typeof(Color), "White")] |
| 22 | + public Color ArrowColor { get; set; } = Color.White; |
| 23 | + |
| 24 | + private readonly ComboBox originalComboBox; |
| 25 | + private Bitmap buffer; // Our double buffer for flicker-free drawing |
| 26 | + |
| 27 | + public ComboBoxPainter(ComboBox comboBox) |
| 28 | + { |
| 29 | + this.originalComboBox = comboBox; |
| 30 | + |
| 31 | + // Hook events to manage the buffer and force redraws |
| 32 | + originalComboBox.SizeChanged += OnSizeChanged; |
| 33 | + this.AssignHandle(comboBox.Handle); |
| 34 | + |
| 35 | + // *** THE FIX: Use reflection to call the protected SetStyle method *** |
| 36 | + SetControlStyle(originalComboBox, ControlStyles.OptimizedDoubleBuffer, true); |
| 37 | + SetControlStyle(originalComboBox, ControlStyles.ResizeRedraw, true); |
| 38 | + |
| 39 | + CreateBuffer(); |
| 40 | + } |
| 41 | + |
| 42 | + /// <summary> |
| 43 | + /// Uses reflection to call the protected SetStyle method on a control. |
| 44 | + /// </summary> |
| 45 | + private static void SetControlStyle(Control control, ControlStyles style, bool value) |
| 46 | + { |
| 47 | + MethodInfo method = typeof(Control).GetMethod("SetStyle", BindingFlags.NonPublic | BindingFlags.Instance); |
| 48 | + if (method != null) |
| 49 | + { |
| 50 | + method.Invoke(control, new object[] { style, value }); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + private void OnSizeChanged(object sender, EventArgs e) |
| 56 | + { |
| 57 | + CreateBuffer(); |
| 58 | + originalComboBox.Invalidate(); |
| 59 | + } |
| 60 | + |
| 61 | + private void CreateBuffer() |
| 62 | + { |
| 63 | + // Dispose of the old buffer if it exists |
| 64 | + buffer?.Dispose(); |
| 65 | + |
| 66 | + // Create a new buffer if the control is valid |
| 67 | + if (originalComboBox.IsHandleCreated && originalComboBox.Width > 0 && originalComboBox.Height > 0) |
| 68 | + { |
| 69 | + buffer = new Bitmap(originalComboBox.Width, originalComboBox.Height); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + protected override void WndProc(ref Message m) |
| 74 | + { |
| 75 | + switch (m.Msg) |
| 76 | + { |
| 77 | + // We are taking full control of the WM_PAINT message. |
| 78 | + // We will not call base.WndProc here. |
| 79 | + case WM_PAINT: |
| 80 | + if (originalComboBox.DropDownStyle != ComboBoxStyle.Simple && buffer != null) |
| 81 | + { |
| 82 | + // Start the painting process |
| 83 | + PAINTSTRUCT ps = new PAINTSTRUCT(); |
| 84 | + IntPtr hdc = BeginPaint(originalComboBox.Handle, ref ps); |
| 85 | + |
| 86 | + // 1. Draw the entire control's appearance to our in-memory buffer |
| 87 | + using (Graphics g = Graphics.FromImage(buffer)) |
| 88 | + { |
| 89 | + PaintComboBox(g); |
| 90 | + } |
| 91 | + |
| 92 | + // 2. Draw the completed buffer onto the screen in one operation |
| 93 | + using (Graphics screenG = Graphics.FromHdc(hdc)) |
| 94 | + { |
| 95 | + screenG.DrawImage(buffer, Point.Empty); |
| 96 | + } |
| 97 | + |
| 98 | + // End the painting process |
| 99 | + EndPaint(originalComboBox.Handle, ref ps); |
| 100 | + } |
| 101 | + else |
| 102 | + { |
| 103 | + base.WndProc(ref m); |
| 104 | + } |
| 105 | + // Message is handled, do not call base. |
| 106 | + return; |
| 107 | + |
| 108 | + // Prevent the OS from drawing its default non-client area (border/button) |
| 109 | + case WM_NCPAINT: |
| 110 | + // By not calling base.WndProc, we suppress the default painting |
| 111 | + // that causes the flicker. |
| 112 | + break; |
| 113 | + |
| 114 | + default: |
| 115 | + base.WndProc(ref m); |
| 116 | + break; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + private void PaintComboBox(Graphics g) |
| 121 | + { |
| 122 | + g.SmoothingMode = SmoothingMode.AntiAlias; |
| 123 | + |
| 124 | + // --- Define Rectangles --- |
| 125 | + var clientRect = originalComboBox.ClientRectangle; |
| 126 | + var dropDownButtonWidth = SystemInformation.HorizontalScrollBarArrowWidth + 2; |
| 127 | + var borderRect = new Rectangle(clientRect.Location, new Size(clientRect.Width - 1, clientRect.Height - 1)); |
| 128 | + var dropDownRect = new Rectangle(clientRect.Width - dropDownButtonWidth, 0, dropDownButtonWidth, clientRect.Height); |
| 129 | + // Define a text area with a small padding |
| 130 | + var textRect = new Rectangle(2, 2, clientRect.Width - dropDownButtonWidth - 4, clientRect.Height - 4); |
| 131 | + |
| 132 | + // --- Determine Colors based on state --- |
| 133 | + var currentBorderColor = originalComboBox.Enabled ? this.BorderColor : SystemColors.ControlDark; |
| 134 | + var currentButtonColor = originalComboBox.Enabled ? this.ButtonColor : SystemColors.Control; |
| 135 | + var currentArrowColor = originalComboBox.Enabled ? this.ArrowColor : SystemColors.ControlDark; |
| 136 | + var currentBackColor = originalComboBox.Enabled ? originalComboBox.BackColor : SystemColors.Control; |
| 137 | + var currentTextColor = originalComboBox.Enabled ? originalComboBox.ForeColor : SystemColors.GrayText; |
| 138 | + |
| 139 | + // --- Perform Drawing operations sequentially --- |
| 140 | + |
| 141 | + // 1. Draw Background |
| 142 | + using (var b = new SolidBrush(currentBackColor)) |
| 143 | + { |
| 144 | + g.FillRectangle(b, clientRect); |
| 145 | + } |
| 146 | + |
| 147 | + // 2. Draw Text (This is now our responsibility) |
| 148 | + TextRenderer.DrawText(g, |
| 149 | + originalComboBox.Text, |
| 150 | + originalComboBox.Font, |
| 151 | + textRect, |
| 152 | + currentTextColor, |
| 153 | + TextFormatFlags.Left | TextFormatFlags.VerticalCenter | TextFormatFlags.EndEllipsis); |
| 154 | + |
| 155 | + // 3. Draw Button Background |
| 156 | + using (var b = new SolidBrush(currentButtonColor)) |
| 157 | + { |
| 158 | + g.FillRectangle(b, dropDownRect); |
| 159 | + } |
| 160 | + |
| 161 | + // 4. Draw Dropdown Arrow |
| 162 | + var middle = new Point(dropDownRect.Left + dropDownRect.Width / 2, dropDownRect.Top + dropDownRect.Height / 2); |
| 163 | + var arrow = new Point[] |
| 164 | + { |
| 165 | + new Point(middle.X - 3, middle.Y - 2), |
| 166 | + new Point(middle.X + 4, middle.Y - 2), |
| 167 | + new Point(middle.X, middle.Y + 2) |
| 168 | + }; |
| 169 | + using (var b = new SolidBrush(currentArrowColor)) |
| 170 | + { |
| 171 | + g.FillPolygon(b, arrow); |
| 172 | + } |
| 173 | + |
| 174 | + // 5. Draw Border |
| 175 | + using (var p = new Pen(currentBorderColor)) |
| 176 | + { |
| 177 | + g.DrawRectangle(p, borderRect); |
| 178 | + } |
| 179 | + } |
| 180 | + |
| 181 | + // --- P/Invoke for custom painting --- |
| 182 | + private const int WM_PAINT = 0xF; |
| 183 | + private const int WM_NCPAINT = 0x85; |
| 184 | + |
| 185 | + [StructLayout(LayoutKind.Sequential)] |
| 186 | + public struct PAINTSTRUCT { public IntPtr hdc; public bool fErase; public RECT rcPaint; public bool fRestore; public bool fIncUpdate; [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)] public byte[] rgbReserved; } |
| 187 | + [StructLayout(LayoutKind.Sequential)] |
| 188 | + public struct RECT { public int Left, Top, Right, Bottom; } |
| 189 | + |
| 190 | + [DllImport("user32.dll")] |
| 191 | + private static extern IntPtr BeginPaint(IntPtr hWnd, ref PAINTSTRUCT lpPaint); |
| 192 | + [DllImport("user32.dll")] |
| 193 | + private static extern bool EndPaint(IntPtr hWnd, ref PAINTSTRUCT lpPaint); |
| 194 | + |
| 195 | + // --- IDisposable for cleaning up resources --- |
| 196 | + public void Dispose() |
| 197 | + { |
| 198 | + buffer?.Dispose(); |
| 199 | + // This stops the message interception |
| 200 | + this.ReleaseHandle(); |
| 201 | + } |
| 202 | + } |
| 203 | +} |
0 commit comments