Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Drawing;
- using System.Drawing.Drawing2D;
- using System.Windows.Forms;
- namespace Screen_Recorder
- {
- public class CustomSlider : Control
- {
- private float _radius;
- private PointF _thumbPos;
- private SizeF _barSize;
- private PointF _barPos;
- private Icon settingsIcon;
- private Rectangle iconArea;
- public event EventHandler ValueChanged;
- public CustomSlider()
- {
- InitializeSettingsIcon();
- DoubleBuffered = true;
- _startsFromZero = true; // Set the initial state to false
- Min = _startsFromZero ? 0.0f : 1.0f; // Set Min based on _startsFromZero
- Max = _startsFromZero ? 4.0f : 5.0f; // Set Max based on _startsFromZero
- Value = Min; // Start with the minimum value
- UseIntegerValues = true; // Set to true if you need integer values
- ShowTickLabels = true; // Set to true to show labels
- // Load your settings icon
- this.Resize += CustomControlWithSettings_Resize;
- this.MouseClick += CustomControlWithSettings_MouseClick;
- }
- public float ThumbSize { get; set; } = 0.7f;
- public int LabelGap { get; set; } = 10;
- public bool UseIntegerValues { get; set; }
- public bool ShowTickLabels { get; set; } = true; // Default to true to show labels
- public float Min { get; set; }
- public float Max { get; set; }
- private float _value;
- public float Value
- {
- get => _value;
- set
- {
- if (UseIntegerValues)
- {
- // Snap value to the nearest integer if in integer mode
- _value = (float)Math.Round(Math.Max(Min, Math.Min(Max, value)));
- }
- else
- {
- // Allow floating point values within range
- _value = Math.Max(Min, Math.Min(Max, value));
- }
- ValueChanged?.Invoke(this, EventArgs.Empty);
- RecalculateParameters();
- }
- }
- private bool _startsFromZero = false; // Default to true as per your setup
- public bool StartsFromZero
- {
- get => _startsFromZero;
- set
- {
- if (_startsFromZero == value) return; // Exit if no change
- _startsFromZero = value;
- Min = _startsFromZero ? 0.0f : 1.0f;
- Max = _startsFromZero ? 4.0f : 5.0f;
- // Set Value to the new Min whenever StartsFromZero changes
- Value = Min;
- RecalculateParameters();
- Invalidate(); // Request a redraw
- }
- }
- private void InitializeSettingsIcon()
- {
- // Convert the PNG image from resources to an icon
- using (Bitmap bmp = new Bitmap(Properties.Resources.icons8_gear_50))
- {
- settingsIcon = Icon.FromHandle(bmp.GetHicon());
- }
- // Define the initial area for the icon (top-right corner)
- iconArea = new Rectangle(this.Width - 20, 0, 20, 20);
- }
- protected override void OnPaint(PaintEventArgs e)
- {
- base.OnPaint(e);
- e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
- // Dynamic sizing of the icon based on control size
- int iconSize = Math.Max(20, this.Height / 5); // Example: icon size is 20 or 20% of height
- iconArea = new Rectangle(this.Width - iconSize - 5, 5, iconSize, iconSize); // 5 pixels padding
- // Draw the track
- e.Graphics.FillRectangle(Brushes.DimGray, _barPos.X, _barPos.Y, _barSize.Width, _barSize.Height);
- // Draw the filled part of the track
- e.Graphics.FillRectangle(Brushes.Red, _barPos.X, _barPos.Y, _thumbPos.X - _barPos.X, _barSize.Height);
- // Draw the thumb
- e.Graphics.FillCircle(Brushes.White, _thumbPos.X, _thumbPos.Y, _radius);
- e.Graphics.FillCircle(Brushes.Red, _thumbPos.X, _thumbPos.Y, ThumbSize * _radius);
- // Draw ticks
- DrawTicks(e.Graphics);
- // Draw the settings icon at the dynamic position and size
- if (settingsIcon != null)
- {
- e.Graphics.DrawIcon(settingsIcon, iconArea);
- }
- }
- private void RecalculateParameters()
- {
- _radius = 0.1f * ClientSize.Height;
- _barSize = new SizeF(ClientSize.Width - 2f * _radius, 0.1f * ClientSize.Height);
- _barPos = new PointF(_radius, (ClientSize.Height - _barSize.Height) / 2);
- const int tickCount = 5;
- float tickSpacing = (_barSize.Width - 2 * _radius) / (tickCount - 1);
- float scaledValue = (Value - Min) / (Max - Min); // Normalize value between 0 and 1
- float position = scaledValue * (_barSize.Width - 2 * _radius); // Scale position according to control width
- if (UseIntegerValues)
- {
- // Round the position to snap to the nearest tick mark
- int nearestTickIndex = (int)Math.Round(scaledValue * (tickCount - 1));
- position = nearestTickIndex * tickSpacing;
- }
- _thumbPos = new PointF(_barPos.X + _radius + position, _barPos.Y + _barSize.Height / 2);
- Invalidate();
- }
- private void DrawTicks(Graphics g)
- {
- const int tickCount = 5;
- float tickSpacing = (_barSize.Width - 2 * _radius) / (tickCount - 1);
- for (int i = 0; i < tickCount; i++)
- {
- float x = _barPos.X + _radius + i * tickSpacing;
- g.DrawLine(Pens.Black, x, _barPos.Y, x, _barPos.Y + _barSize.Height);
- if (ShowTickLabels)
- {
- string label = ((Min + (Max - Min) / (tickCount - 1) * i).ToString("0.#"));
- SizeF labelSize = g.MeasureString(label, SystemFonts.DefaultFont);
- float labelX = x - labelSize.Width / 2;
- float labelYTop = _barPos.Y - LabelGap - labelSize.Height;
- float labelYBottom = _barPos.Y + _barSize.Height + LabelGap;
- // Check if the label goes outside of the control bounds and adjust
- labelYTop = Math.Max(0, labelYTop);
- labelYBottom = Math.Min(ClientSize.Height - labelSize.Height, labelYBottom);
- g.DrawString(label, SystemFonts.DefaultFont, Brushes.Black, labelX, labelYTop);
- g.DrawString(label, SystemFonts.DefaultFont, Brushes.Black, labelX, labelYBottom);
- }
- }
- }
- protected override void OnResize(EventArgs e)
- {
- base.OnResize(e);
- RecalculateParameters();
- }
- protected override void OnMouseMove(MouseEventArgs e)
- {
- if (iconArea.Contains(e.Location))
- {
- // Possibly change cursor to indicate clickable area
- this.Cursor = Cursors.Hand;
- }
- else
- {
- this.Cursor = Cursors.Default;
- if (e.Button == MouseButtons.Left)
- {
- float newValue = ((e.X - _barPos.X) / _barSize.Width) * (Max - Min) + Min;
- Value = newValue; // Update value while moving
- }
- }
- }
- protected override void OnMouseDown(MouseEventArgs e)
- {
- if (!iconArea.Contains(e.Location))
- {
- base.OnMouseDown(e);
- float newValue = ((e.X - _barPos.X) / _barSize.Width) * (Max - Min) + Min;
- Value = newValue; // Set value based on click position
- }
- else
- {
- OpenSettingsForm(); // Open settings form when icon is clicked
- }
- }
- protected override void OnMouseUp(MouseEventArgs e)
- {
- base.OnMouseUp(e);
- }
- private void CustomControlWithSettings_Resize(object sender, EventArgs e)
- {
- // Keep the icon in the top right corner
- iconArea = new Rectangle(this.Width - 20, 0, 20, 20);
- }
- private void CustomControlWithSettings_MouseClick(object sender, MouseEventArgs e)
- {
- // Check if the click is within the icon area
- if (iconArea.Contains(e.Location))
- {
- OpenSettingsForm();
- }
- }
- private void OpenSettingsForm()
- {
- // Open a form with settings
- SettingsForm settingsForm = new SettingsForm();
- settingsForm.Show();
- }
- }
- public static class GraphicsExtensions
- {
- public static void DrawCircle(this Graphics g, Pen pen, float centerX, float centerY, float radius)
- {
- g.DrawEllipse(pen, centerX - radius, centerY - radius, radius + radius, radius + radius);
- }
- public static void FillCircle(this Graphics g, Brush brush, float centerX, float centerY, float radius)
- {
- g.FillEllipse(brush, centerX - radius, centerY - radius, radius + radius, radius + radius);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment