Chocolade19721309

Untitled

May 12th, 2024
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.44 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. using System.Windows.Forms;
  5.  
  6. namespace Screen_Recorder
  7. {
  8.  
  9. public class CustomSlider : Control
  10. {
  11. private float _radius;
  12. private PointF _thumbPos;
  13. private SizeF _barSize;
  14. private PointF _barPos;
  15. private Icon settingsIcon;
  16. private Rectangle iconArea;
  17.  
  18. public event EventHandler ValueChanged;
  19.  
  20. public CustomSlider()
  21. {
  22. InitializeSettingsIcon();
  23. DoubleBuffered = true;
  24. _startsFromZero = true; // Set the initial state to false
  25. Min = _startsFromZero ? 0.0f : 1.0f; // Set Min based on _startsFromZero
  26. Max = _startsFromZero ? 4.0f : 5.0f; // Set Max based on _startsFromZero
  27. Value = Min; // Start with the minimum value
  28. UseIntegerValues = true; // Set to true if you need integer values
  29. ShowTickLabels = true; // Set to true to show labels
  30.  
  31. // Load your settings icon
  32. this.Resize += CustomControlWithSettings_Resize;
  33. this.MouseClick += CustomControlWithSettings_MouseClick;
  34. }
  35.  
  36. public float ThumbSize { get; set; } = 0.7f;
  37. public int LabelGap { get; set; } = 10;
  38. public bool UseIntegerValues { get; set; }
  39. public bool ShowTickLabels { get; set; } = true; // Default to true to show labels
  40. public float Min { get; set; }
  41. public float Max { get; set; }
  42. private float _value;
  43. public float Value
  44. {
  45. get => _value;
  46. set
  47. {
  48. if (UseIntegerValues)
  49. {
  50. // Snap value to the nearest integer if in integer mode
  51. _value = (float)Math.Round(Math.Max(Min, Math.Min(Max, value)));
  52. }
  53. else
  54. {
  55. // Allow floating point values within range
  56. _value = Math.Max(Min, Math.Min(Max, value));
  57. }
  58. ValueChanged?.Invoke(this, EventArgs.Empty);
  59. RecalculateParameters();
  60. }
  61. }
  62.  
  63. private bool _startsFromZero = false; // Default to true as per your setup
  64.  
  65. public bool StartsFromZero
  66. {
  67. get => _startsFromZero;
  68. set
  69. {
  70. if (_startsFromZero == value) return; // Exit if no change
  71.  
  72. _startsFromZero = value;
  73. Min = _startsFromZero ? 0.0f : 1.0f;
  74. Max = _startsFromZero ? 4.0f : 5.0f;
  75.  
  76. // Set Value to the new Min whenever StartsFromZero changes
  77. Value = Min;
  78.  
  79. RecalculateParameters();
  80. Invalidate(); // Request a redraw
  81. }
  82. }
  83.  
  84. private void InitializeSettingsIcon()
  85. {
  86. // Convert the PNG image from resources to an icon
  87. using (Bitmap bmp = new Bitmap(Properties.Resources.icons8_gear_50))
  88. {
  89. settingsIcon = Icon.FromHandle(bmp.GetHicon());
  90. }
  91.  
  92. // Define the initial area for the icon (top-right corner)
  93. iconArea = new Rectangle(this.Width - 20, 0, 20, 20);
  94. }
  95.  
  96. protected override void OnPaint(PaintEventArgs e)
  97. {
  98. base.OnPaint(e);
  99. e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
  100.  
  101. // Dynamic sizing of the icon based on control size
  102. int iconSize = Math.Max(20, this.Height / 5); // Example: icon size is 20 or 20% of height
  103. iconArea = new Rectangle(this.Width - iconSize - 5, 5, iconSize, iconSize); // 5 pixels padding
  104.  
  105. // Draw the track
  106. e.Graphics.FillRectangle(Brushes.DimGray, _barPos.X, _barPos.Y, _barSize.Width, _barSize.Height);
  107.  
  108. // Draw the filled part of the track
  109. e.Graphics.FillRectangle(Brushes.Red, _barPos.X, _barPos.Y, _thumbPos.X - _barPos.X, _barSize.Height);
  110.  
  111. // Draw the thumb
  112. e.Graphics.FillCircle(Brushes.White, _thumbPos.X, _thumbPos.Y, _radius);
  113. e.Graphics.FillCircle(Brushes.Red, _thumbPos.X, _thumbPos.Y, ThumbSize * _radius);
  114.  
  115. // Draw ticks
  116. DrawTicks(e.Graphics);
  117.  
  118. // Draw the settings icon at the dynamic position and size
  119. if (settingsIcon != null)
  120. {
  121. e.Graphics.DrawIcon(settingsIcon, iconArea);
  122. }
  123. }
  124.  
  125. private void RecalculateParameters()
  126. {
  127. _radius = 0.1f * ClientSize.Height;
  128. _barSize = new SizeF(ClientSize.Width - 2f * _radius, 0.1f * ClientSize.Height);
  129. _barPos = new PointF(_radius, (ClientSize.Height - _barSize.Height) / 2);
  130.  
  131. const int tickCount = 5;
  132. float tickSpacing = (_barSize.Width - 2 * _radius) / (tickCount - 1);
  133.  
  134. float scaledValue = (Value - Min) / (Max - Min); // Normalize value between 0 and 1
  135. float position = scaledValue * (_barSize.Width - 2 * _radius); // Scale position according to control width
  136.  
  137. if (UseIntegerValues)
  138. {
  139. // Round the position to snap to the nearest tick mark
  140. int nearestTickIndex = (int)Math.Round(scaledValue * (tickCount - 1));
  141. position = nearestTickIndex * tickSpacing;
  142. }
  143.  
  144. _thumbPos = new PointF(_barPos.X + _radius + position, _barPos.Y + _barSize.Height / 2);
  145. Invalidate();
  146. }
  147.  
  148. private void DrawTicks(Graphics g)
  149. {
  150. const int tickCount = 5;
  151. float tickSpacing = (_barSize.Width - 2 * _radius) / (tickCount - 1);
  152.  
  153. for (int i = 0; i < tickCount; i++)
  154. {
  155. float x = _barPos.X + _radius + i * tickSpacing;
  156. g.DrawLine(Pens.Black, x, _barPos.Y, x, _barPos.Y + _barSize.Height);
  157.  
  158. if (ShowTickLabels)
  159. {
  160. string label = ((Min + (Max - Min) / (tickCount - 1) * i).ToString("0.#"));
  161. SizeF labelSize = g.MeasureString(label, SystemFonts.DefaultFont);
  162. float labelX = x - labelSize.Width / 2;
  163. float labelYTop = _barPos.Y - LabelGap - labelSize.Height;
  164. float labelYBottom = _barPos.Y + _barSize.Height + LabelGap;
  165.  
  166. // Check if the label goes outside of the control bounds and adjust
  167. labelYTop = Math.Max(0, labelYTop);
  168. labelYBottom = Math.Min(ClientSize.Height - labelSize.Height, labelYBottom);
  169.  
  170. g.DrawString(label, SystemFonts.DefaultFont, Brushes.Black, labelX, labelYTop);
  171. g.DrawString(label, SystemFonts.DefaultFont, Brushes.Black, labelX, labelYBottom);
  172. }
  173. }
  174. }
  175.  
  176. protected override void OnResize(EventArgs e)
  177. {
  178. base.OnResize(e);
  179. RecalculateParameters();
  180. }
  181.  
  182. protected override void OnMouseMove(MouseEventArgs e)
  183. {
  184. if (iconArea.Contains(e.Location))
  185. {
  186. // Possibly change cursor to indicate clickable area
  187. this.Cursor = Cursors.Hand;
  188. }
  189. else
  190. {
  191. this.Cursor = Cursors.Default;
  192. if (e.Button == MouseButtons.Left)
  193. {
  194. float newValue = ((e.X - _barPos.X) / _barSize.Width) * (Max - Min) + Min;
  195. Value = newValue; // Update value while moving
  196. }
  197. }
  198. }
  199.  
  200. protected override void OnMouseDown(MouseEventArgs e)
  201. {
  202. if (!iconArea.Contains(e.Location))
  203. {
  204. base.OnMouseDown(e);
  205. float newValue = ((e.X - _barPos.X) / _barSize.Width) * (Max - Min) + Min;
  206. Value = newValue; // Set value based on click position
  207. }
  208. else
  209. {
  210. OpenSettingsForm(); // Open settings form when icon is clicked
  211. }
  212. }
  213.  
  214. protected override void OnMouseUp(MouseEventArgs e)
  215. {
  216. base.OnMouseUp(e);
  217. }
  218.  
  219. private void CustomControlWithSettings_Resize(object sender, EventArgs e)
  220. {
  221. // Keep the icon in the top right corner
  222. iconArea = new Rectangle(this.Width - 20, 0, 20, 20);
  223. }
  224.  
  225. private void CustomControlWithSettings_MouseClick(object sender, MouseEventArgs e)
  226. {
  227. // Check if the click is within the icon area
  228. if (iconArea.Contains(e.Location))
  229. {
  230. OpenSettingsForm();
  231. }
  232. }
  233.  
  234. private void OpenSettingsForm()
  235. {
  236. // Open a form with settings
  237. SettingsForm settingsForm = new SettingsForm();
  238. settingsForm.Show();
  239. }
  240. }
  241.  
  242. public static class GraphicsExtensions
  243. {
  244. public static void DrawCircle(this Graphics g, Pen pen, float centerX, float centerY, float radius)
  245. {
  246. g.DrawEllipse(pen, centerX - radius, centerY - radius, radius + radius, radius + radius);
  247. }
  248.  
  249. public static void FillCircle(this Graphics g, Brush brush, float centerX, float centerY, float radius)
  250. {
  251. g.FillEllipse(brush, centerX - radius, centerY - radius, radius + radius, radius + radius);
  252. }
  253. }
  254. }
Advertisement
Add Comment
Please, Sign In to add comment