|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.ComponentModel; |
| 4 | +using System.Linq; |
| 5 | +using System.Runtime.InteropServices; |
| 6 | +using System.Text; |
| 7 | +using System.Threading.Tasks; |
| 8 | + |
| 9 | +namespace DarkModeForms |
| 10 | +{ |
| 11 | + // *** CREDITS: https://github.com/r-aghaei/FlatComboExample/tree/master |
| 12 | + |
| 13 | + public class FlatComboBox : ComboBox |
| 14 | + { |
| 15 | + private Color borderColor = Color.Gray; |
| 16 | + [DefaultValue(typeof(Color), "Gray")] |
| 17 | + public Color BorderColor |
| 18 | + { |
| 19 | + get { return borderColor; } |
| 20 | + set |
| 21 | + { |
| 22 | + if (borderColor != value) |
| 23 | + { |
| 24 | + borderColor = value; |
| 25 | + Invalidate(); |
| 26 | + } |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + private Color buttonColor = Color.LightGray; |
| 31 | + [DefaultValue(typeof(Color), "LightGray")] |
| 32 | + public Color ButtonColor |
| 33 | + { |
| 34 | + get { return buttonColor; } |
| 35 | + set |
| 36 | + { |
| 37 | + if (buttonColor != value) |
| 38 | + { |
| 39 | + buttonColor = value; |
| 40 | + Invalidate(); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + protected override void WndProc(ref Message m) |
| 46 | + { |
| 47 | + if (m.Msg == WM_PAINT && DropDownStyle != ComboBoxStyle.Simple) |
| 48 | + { |
| 49 | + var clientRect = ClientRectangle; |
| 50 | + var dropDownButtonWidth = SystemInformation.HorizontalScrollBarArrowWidth; |
| 51 | + var outerBorder = new Rectangle(clientRect.Location, |
| 52 | + new Size(clientRect.Width - 1, clientRect.Height - 1)); |
| 53 | + var innerBorder = new Rectangle(outerBorder.X + 1, outerBorder.Y + 1, |
| 54 | + outerBorder.Width - dropDownButtonWidth - 2, outerBorder.Height - 2); |
| 55 | + var innerInnerBorder = new Rectangle(innerBorder.X + 1, innerBorder.Y + 1, |
| 56 | + innerBorder.Width - 2, innerBorder.Height - 2); |
| 57 | + var dropDownRect = new Rectangle(innerBorder.Right + 1, innerBorder.Y - 1, |
| 58 | + dropDownButtonWidth, innerBorder.Height + 2); |
| 59 | + if (RightToLeft == RightToLeft.Yes) |
| 60 | + { |
| 61 | + innerBorder.X = clientRect.Width - innerBorder.Right; |
| 62 | + innerInnerBorder.X = clientRect.Width - innerInnerBorder.Right; |
| 63 | + dropDownRect.X = clientRect.Width - dropDownRect.Right; |
| 64 | + dropDownRect.Width += 1; |
| 65 | + } |
| 66 | + var innerBorderColor = Enabled ? BackColor : SystemColors.Control; |
| 67 | + var outerBorderColor = Enabled ? BorderColor : SystemColors.ControlDark; |
| 68 | + var buttonColor = Enabled ? ButtonColor : SystemColors.Control; |
| 69 | + var middle = new Point(dropDownRect.Left + dropDownRect.Width / 2, |
| 70 | + dropDownRect.Top + dropDownRect.Height / 2); |
| 71 | + var arrow = new Point[] |
| 72 | + { |
| 73 | + new Point(middle.X - 3, middle.Y - 2), |
| 74 | + new Point(middle.X + 4, middle.Y - 2), |
| 75 | + new Point(middle.X, middle.Y + 2) |
| 76 | + }; |
| 77 | + var ps = new PAINTSTRUCT(); |
| 78 | + bool shoulEndPaint = false; |
| 79 | + IntPtr dc; |
| 80 | + if (m.WParam == IntPtr.Zero) |
| 81 | + { |
| 82 | + dc = BeginPaint(Handle, ref ps); |
| 83 | + m.WParam = dc; |
| 84 | + shoulEndPaint = true; |
| 85 | + } |
| 86 | + else |
| 87 | + { |
| 88 | + dc = m.WParam; |
| 89 | + } |
| 90 | + var rgn = CreateRectRgn(innerInnerBorder.Left, innerInnerBorder.Top, |
| 91 | + innerInnerBorder.Right, innerInnerBorder.Bottom); |
| 92 | + SelectClipRgn(dc, rgn); |
| 93 | + DefWndProc(ref m); |
| 94 | + DeleteObject(rgn); |
| 95 | + rgn = CreateRectRgn(clientRect.Left, clientRect.Top, |
| 96 | + clientRect.Right, clientRect.Bottom); |
| 97 | + SelectClipRgn(dc, rgn); |
| 98 | + |
| 99 | + using (var g = Graphics.FromHdc(dc)) |
| 100 | + { |
| 101 | + g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBilinear; |
| 102 | + g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality; |
| 103 | + g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; |
| 104 | + |
| 105 | + #region DropDown Button |
| 106 | + |
| 107 | + using (var b = new SolidBrush(buttonColor)) |
| 108 | + { |
| 109 | + g.FillRectangle(b, dropDownRect); |
| 110 | + } |
| 111 | + |
| 112 | + #endregion |
| 113 | + |
| 114 | + #region Chevron |
| 115 | + |
| 116 | + //Replaced 'arrow' triangle with a Windows 11's Chevron: |
| 117 | + //using (var b = new SolidBrush(outerBorderColor)) |
| 118 | + //{ |
| 119 | + // g.FillPolygon(b, arrow); |
| 120 | + //} |
| 121 | + |
| 122 | + Size cSize = new Size(8, 4); //<- Size of the Chevron: 8x4 px |
| 123 | + var chevron = new Point[] |
| 124 | + { |
| 125 | + new Point(middle.X - (cSize.Width / 2), middle.Y - (cSize.Height / 2)), |
| 126 | + new Point(middle.X + (cSize.Width / 2), middle.Y - (cSize.Height / 2)), |
| 127 | + new Point(middle.X, middle.Y + (cSize.Height / 2)) |
| 128 | + }; |
| 129 | + using (var chevronPen = new Pen(BorderColor, 2.5f)) //<- Color and Border Width |
| 130 | + { |
| 131 | + g.DrawLine(chevronPen, chevron[0], chevron[2]); |
| 132 | + g.DrawLine(chevronPen, chevron[1], chevron[2]); |
| 133 | + } |
| 134 | + |
| 135 | + #endregion |
| 136 | + |
| 137 | + #region Borders |
| 138 | + |
| 139 | + using (var p = new Pen(innerBorderColor)) |
| 140 | + { |
| 141 | + g.DrawRectangle(p, innerBorder); |
| 142 | + g.DrawRectangle(p, innerInnerBorder); |
| 143 | + } |
| 144 | + using (var p = new Pen(outerBorderColor)) |
| 145 | + { |
| 146 | + g.DrawRectangle(p, outerBorder); |
| 147 | + } |
| 148 | + |
| 149 | + #endregion |
| 150 | + } |
| 151 | + if (shoulEndPaint) |
| 152 | + EndPaint(Handle, ref ps); |
| 153 | + DeleteObject(rgn); |
| 154 | + } |
| 155 | + else |
| 156 | + base.WndProc(ref m); |
| 157 | + } |
| 158 | + |
| 159 | + private const int WM_PAINT = 0xF; |
| 160 | + [StructLayout(LayoutKind.Sequential)] |
| 161 | + public struct RECT |
| 162 | + { |
| 163 | + public int L, T, R, B; |
| 164 | + } |
| 165 | + [StructLayout(LayoutKind.Sequential)] |
| 166 | + public struct PAINTSTRUCT |
| 167 | + { |
| 168 | + public IntPtr hdc; |
| 169 | + public bool fErase; |
| 170 | + public int rcPaint_left; |
| 171 | + public int rcPaint_top; |
| 172 | + public int rcPaint_right; |
| 173 | + public int rcPaint_bottom; |
| 174 | + public bool fRestore; |
| 175 | + public bool fIncUpdate; |
| 176 | + public int reserved1; |
| 177 | + public int reserved2; |
| 178 | + public int reserved3; |
| 179 | + public int reserved4; |
| 180 | + public int reserved5; |
| 181 | + public int reserved6; |
| 182 | + public int reserved7; |
| 183 | + public int reserved8; |
| 184 | + } |
| 185 | + [DllImport("user32.dll")] |
| 186 | + private static extern IntPtr BeginPaint(IntPtr hWnd, |
| 187 | + [In, Out] ref PAINTSTRUCT lpPaint); |
| 188 | + |
| 189 | + [DllImport("user32.dll")] |
| 190 | + private static extern bool EndPaint(IntPtr hWnd, ref PAINTSTRUCT lpPaint); |
| 191 | + |
| 192 | + [DllImport("gdi32.dll")] |
| 193 | + public static extern int SelectClipRgn(IntPtr hDC, IntPtr hRgn); |
| 194 | + |
| 195 | + [DllImport("user32.dll")] |
| 196 | + public static extern int GetUpdateRgn(IntPtr hwnd, IntPtr hrgn, bool fErase); |
| 197 | + public enum RegionFlags |
| 198 | + { |
| 199 | + ERROR = 0, |
| 200 | + NULLREGION = 1, |
| 201 | + SIMPLEREGION = 2, |
| 202 | + COMPLEXREGION = 3, |
| 203 | + } |
| 204 | + [DllImport("gdi32.dll")] |
| 205 | + internal static extern bool DeleteObject(IntPtr hObject); |
| 206 | + |
| 207 | + [DllImport("gdi32.dll")] |
| 208 | + private static extern IntPtr CreateRectRgn(int x1, int y1, int x2, int y2); |
| 209 | + } |
| 210 | +} |
0 commit comments