MoonComboBox.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. using System.Runtime.InteropServices;
  5. using System.Windows.Forms;
  6. namespace Optimizer
  7. {
  8. public sealed class MoonComboBox : ComboBox
  9. {
  10. [DllImport("user32.dll")]
  11. static extern IntPtr GetDC(IntPtr ptr);
  12. [DllImport("gdi32.dll")]
  13. static extern int GetDeviceCaps(IntPtr hdc, int nIndex);
  14. [DllImport("user32.dll", EntryPoint = "ReleaseDC")]
  15. static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);
  16. private Color _color = Color.FromArgb(40, 40, 40);
  17. private Color _selectedItemColor = Color.FromArgb(40, 40, 40);
  18. private Color _borderColor = Color.FromArgb(61, 61, 61);
  19. private Color _separatorLineColor = Color.FromArgb(61, 61, 61);
  20. public Color SelectColor
  21. {
  22. get => _selectedItemColor;
  23. set
  24. {
  25. _selectedItemColor = value;
  26. Invalidate();
  27. }
  28. }
  29. protected override void OnDrawItem(DrawItemEventArgs e)
  30. {
  31. base.OnDrawItem(e);
  32. LinearGradientBrush LGB = new LinearGradientBrush(e.Bounds, _selectedItemColor, ColorHelper.ChangeColorBrightness(_selectedItemColor, 0.7), 90.0F);
  33. if (Convert.ToInt32(e.State & DrawItemState.Selected) == (int)DrawItemState.Selected)
  34. {
  35. if (!(e.Index == -1))
  36. {
  37. e.Graphics.FillRectangle(LGB, e.Bounds);
  38. e.Graphics.DrawString(GetItemText(Items[e.Index]), e.Font, new SolidBrush(GetContrastColor(SelectColor)), e.Bounds);
  39. }
  40. }
  41. else
  42. {
  43. if (!(e.Index == -1))
  44. {
  45. e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(40, 40, 40)), e.Bounds);
  46. e.Graphics.DrawString(GetItemText(Items[e.Index]), e.Font, Brushes.DimGray, e.Bounds);
  47. }
  48. }
  49. LGB.Dispose();
  50. }
  51. protected override void OnLostFocus(EventArgs e)
  52. {
  53. base.OnLostFocus(e);
  54. SuspendLayout();
  55. Update();
  56. ResumeLayout();
  57. }
  58. protected override void OnPaintBackground(PaintEventArgs e)
  59. {
  60. base.OnPaintBackground(e);
  61. }
  62. protected override void OnResize(EventArgs e)
  63. {
  64. base.OnResize(e);
  65. if (!Focused)
  66. {
  67. SelectionLength = 0;
  68. }
  69. }
  70. private static int DPIScale
  71. {
  72. get
  73. {
  74. IntPtr hdc = GetDC(IntPtr.Zero);
  75. int DC = GetDeviceCaps(hdc, 0x58);
  76. ReleaseDC(IntPtr.Zero, hdc);
  77. return DC;
  78. }
  79. }
  80. private int GetDPIScaleFactor(int currentDPI)
  81. {
  82. switch (currentDPI)
  83. {
  84. case 96:
  85. return 25;
  86. case 120:
  87. return 30;
  88. case 144:
  89. return 35;
  90. case 192:
  91. return 40;
  92. default:
  93. return 25;
  94. }
  95. }
  96. protected override void OnDropDown(EventArgs e)
  97. {
  98. base.OnDropDown(e);
  99. DropDownHeight = Items.Count * GetDPIScaleFactor(DPIScale);
  100. }
  101. public MoonComboBox()
  102. {
  103. SetStyle((ControlStyles)0x22016, true);
  104. SetStyle(ControlStyles.Selectable, false);
  105. DrawMode = DrawMode.OwnerDrawFixed;
  106. DropDownStyle = ComboBoxStyle.DropDownList;
  107. BackColor = Color.FromArgb(246, 246, 246);
  108. ForeColor = Color.FromArgb(76, 76, 97);
  109. Size = new Size(135, 27);
  110. Cursor = Cursors.Hand;
  111. }
  112. private GraphicsPath GetFigurePath(Rectangle rect, int radius)
  113. {
  114. GraphicsPath path = new GraphicsPath();
  115. float curveSize = radius * 2F;
  116. path.StartFigure();
  117. path.AddArc(rect.X, rect.Y, curveSize, curveSize, 180, 90);
  118. path.AddArc(rect.Right - curveSize, rect.Y, curveSize, curveSize, 270, 90);
  119. path.AddArc(rect.Right - curveSize, rect.Bottom - curveSize, curveSize, curveSize, 0, 90);
  120. path.AddArc(rect.X, rect.Bottom - curveSize, curveSize, curveSize, 90, 90);
  121. path.CloseFigure();
  122. return path;
  123. }
  124. private Color GetContrastColor(Color c)
  125. {
  126. double brightness = c.R * 0.299 + c.G * 0.587 + c.B * 0.114;
  127. return brightness > 149 ? Color.Black : Color.White;
  128. }
  129. public enum Direction
  130. {
  131. Up, Down
  132. }
  133. private void DrawTriangle(Graphics g, Rectangle rect, Direction direction)
  134. {
  135. int halfWidth = rect.Width / 2;
  136. Point p0 = Point.Empty;
  137. Point p1 = Point.Empty;
  138. Point p2 = Point.Empty;
  139. switch (direction)
  140. {
  141. case Direction.Up:
  142. p0 = new Point(rect.Left + halfWidth, rect.Top);
  143. p1 = new Point(rect.Left, rect.Bottom);
  144. p2 = new Point(rect.Right, rect.Bottom);
  145. break;
  146. case Direction.Down:
  147. p0 = new Point(rect.Left + halfWidth, rect.Bottom);
  148. p1 = new Point(rect.Left, rect.Top);
  149. p2 = new Point(rect.Right, rect.Top);
  150. break;
  151. }
  152. g.FillPolygon(Brushes.DarkGray, new Point[] { p0, p1, p2 });
  153. }
  154. protected override void OnPaint(PaintEventArgs e)
  155. {
  156. base.OnPaint(e);
  157. e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
  158. e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
  159. e.Graphics.InterpolationMode = InterpolationMode.High;
  160. e.Graphics.Clear(Parent.BackColor);
  161. var rect = new Rectangle(0, 0, Width - 1, Height - 1);
  162. GraphicsPath GP = GetFigurePath(rect, 5);
  163. LinearGradientBrush LGB = new LinearGradientBrush(ClientRectangle, _color, ColorHelper.ChangeColorBrightness(_color, 0.8), 90.0F);
  164. e.Graphics.SetClip(GP);
  165. e.Graphics.FillRectangle(LGB, ClientRectangle);
  166. e.Graphics.ResetClip();
  167. e.Graphics.DrawPath(new Pen(_borderColor), GP);
  168. e.Graphics.DrawString(Text, Font, new SolidBrush(ForeColor), new Rectangle(3, 0, Width - 20, Height), new StringFormat
  169. {
  170. LineAlignment = StringAlignment.Center,
  171. Alignment = StringAlignment.Near
  172. });
  173. DrawTriangle(e.Graphics, new Rectangle(Width - 17, (Height / 2) - 4, 8, 8), DroppedDown ? Direction.Up : Direction.Down);
  174. e.Graphics.DrawLine(new Pen(ColorHelper.ChangeColorBrightness(_separatorLineColor, 0.8)), Width - 24, 5, Width - 24, Height - 6);
  175. e.Graphics.DrawLine(new Pen(_separatorLineColor), Width - 25, 5, Width - 25, Height - 6);
  176. }
  177. }
  178. }