我是 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));
}
}
uj5u.com熱心網友回復:
我對您使用的尺寸做了一些調整:
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());

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/391155.html
上一篇:帶有自定義Telerik主題的C#Winforms:嵌入式資源和ThemeResolutionService.LoadPackageResource
