Soy un principiante en diseño de interfaz de usuario y formularios de Windows en general. Y estoy encontrando algunos problemas al diseñar un control de control personalizado
El problema que tengo es que los formularios de Windows no se ajustan correctamente a mi control
Así es como se ve:
Incluso al deshabilitar AutoSize, todavía se ve mal.
Nuevamente, no tengo mucha experiencia y vi un tutorial para hacer este botón de cambio.
Como puede ver, los bordes del círculo están completamente cortados, y no sé por qué.
Aquí está el código:
public AzTogglebutton() { this.AutoSize = true; this.MinimumSize = new Size(50, 22); } private GraphicsPath GetFigurePath() { int arcSize = this.Height - 1; Rectangle leftArc = new Rectangle(0, 0, arcSize, arcSize); Rectangle rightArc = new Rectangle(this.Width - arcSize - 2, 0, arcSize, arcSize); GraphicsPath path = new GraphicsPath(); path.StartFigure(); path.AddArc(leftArc, 90, 180); path.AddArc(rightArc, 270, 180); path.CloseFigure(); return path; } protected override void OnPaint(PaintEventArgs pevent) { int toggleSize = this.Height + 3; pevent.Graphics.SmoothingMode = SmoothingMode.AntiAlias; pevent.Graphics.Clear(this.Parent.BackColor); if (this.Checked) //ON { //Draw the control surface if (solidStyle) pevent.Graphics.FillPath(new SolidBrush(onBackColor), GetFigurePath()); else pevent.Graphics.DrawPath(new Pen(onBackColor, 2), GetFigurePath()); //Draw the toggle pevent.Graphics.FillEllipse(new SolidBrush(onToggleColor), new Rectangle(this.Width - this.Height + - 2, -2, toggleSize, toggleSize)); } else //OFF { //Draw the control surface if (solidStyle) pevent.Graphics.FillPath(new SolidBrush(offBackColor), GetFigurePath()); else pevent.Graphics.DrawPath(new Pen(offBackColor, 2), GetFigurePath()); //Draw the toggle pevent.Graphics.FillEllipse(new SolidBrush(offToggleColor), new Rectangle(-2, -2, toggleSize, toggleSize)); } }Hice algunos ajustes al tamaño que estás usando:
private GraphicsPath GetFigurePath() { var arcSize = this.ClientSize.Height; var leftArc = new Rectangle(0, 0, arcSize, arcSize - 1); var rightArc = new Rectangle(this.ClientSize.Width - arcSize - 1, 0, arcSize, arcSize - 1); var path = new GraphicsPath(); path.StartFigure(); path.AddArc(leftArc, 90, 180); path.AddArc(rightArc, 270, 180); path.CloseFigure(); return path; }Para mantener la coherencia, seguiría usando GraphicsPath para dibujar el círculo resaltado:
private GraphicsPath GetOnPath() { var arcSize = this.ClientSize.Height; var leftArc = new Rectangle(0, 0, arcSize, arcSize - 1); var rightArc = new Rectangle(0, 0, arcSize, arcSize - 1); var path = new GraphicsPath(); path.StartFigure(); path.AddArc(leftArc, 90, 180); path.AddArc(rightArc, 270, 180); path.CloseFigure(); return path; }El final resulto:
pevent.Graphics.Clear(this.Parent.BackColor); pevent.Graphics.SmoothingMode = SmoothingMode.AntiAlias; pevent.Graphics.FillPath(new SolidBrush(offBackColor), GetFigurePath()); pevent.Graphics.FillPath(new SolidBrush(offToggleColor), GetOnPath());