AutoSize 不适合我的自定义 UI 控件

AutoSize not fitting my custom UI control

我是 ui 设计和 windows 一般表格的初学者。我在设计自定义控件控件时遇到了一些问题

我遇到的问题是 windows 表单不适合我的控制

外观如下:

即使禁用 AutoSize,它仍然看起来不对。

再说一遍,我经验不多,看了教程做这个开关按钮

如你所见,圆的边缘被完全切掉了,我不知道为什么

代码如下:

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));
            }
        }

我对您使用的尺寸做了一些调整:

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;
}

为了保持一致性,我会继续使用 GraphicsPath 来绘制高亮圆:

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;
}

最终结果:

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());