when i drag the control a custom slider to the form1 designer and then resize the slider control on the height if i resize it bigger it’s working as expected and the gear icon png is also resize and getting bigger with the slider.
but if i resize the slider control to be smaller on the height at some point the gear icon stop resizing and get overlap on the slider value 4.
How to make that even if resizing the slider control more down on the height the gear icon will keep the ratio and will get also smaller ?
at the top:
the top of the code:
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;
}
the method that init the gear icon:
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);
}
the OnPaint event code
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);
}
}
other events that i’m not sure if needed. the whole code is bit longer.
the problem is that everything in the code is connected , the gear icon with the control.
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);
}