но если я в какой-то момент изменю размер ползунка, чтобы он был меньше по высоте, значок шестеренки перестанет изменять размер и перекроется со значением ползунка 4.

Как это сделать, даже если изменить размер ползунка вниз по высоте, которую сохранит значок шестеренки соотношение и тоже станет меньше?
вверху:
вверху кода:
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;
}
метод, инициализирующий значок шестеренки:
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);
}
код события OnPaint
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);
}
}
другие события, необходимость в которых я не уверен. весь код немного длиннее.
Проблема в том, что в коде все связано, значок шестеренки с элементом управления.
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);
}
Подробнее здесь: https://stackoverflow.com/questions/784 ... -the-heigh