【发布时间】:2019-07-14 05:21:38
【问题描述】:
我有一个Form,其中包含:
- a
TrackBar(最小值 = 1,最大值 = 200,代表缩放百分比); -
UserControl和BorderStyle = BorderStyle.None。
相关代码
表格1
来自设计器代码
trackBar1.Value = 100;
BackColor = Color.Gray;
来自代码隐藏
private void trackBar1_Scroll(object sender, EventArgs e)
{
userControl11.SetZoomFactor(trackBar1.Value / 100F);
}
用户控件1
internal float MyBaseWidth;
public UserControl1()
{
InitializeComponent();
MyBaseWidth = Width;
SetZoomFactor(1);
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
Pen p = new Pen(Color.Yellow);
e.Graphics.DrawPath(p, GraphicsPathWithBorder);
}
internal GraphicsPath GraphicsPathWithBorder;
internal void SetZoomFactor(float z)
{
Width = (int)(MyBaseWidth * z);
GraphicsPathWithBorder = RoundedCornerRectangle(ClientRectangle);
Region = new Region(GraphicsPathWithBorder);
}
internal static GraphicsPath RoundedCornerRectangle(Rectangle r)
{
GraphicsPath path = new GraphicsPath();
float size = 10 * 2F;
path.StartFigure();
path.AddArc(r.X, r.Y,
size, size, 180, 90);
path.AddArc((r.X + (r.Width - size)), r.Y,
size, size, 270, 90);
path.AddArc((r.X + (r.Width - size)), (r.Y + (r.Height - size)),
size, size, 0, 90);
path.AddArc(r.X, (r.Y + (r.Height - size)),
size, size, 90, 90);
path.CloseFigure();
return path;
}
初始截图
使用轨迹栏后的屏幕截图
缩小后黄色边框右侧不可见,放大时右侧有多个黄色边框。
更新:
答案有效,但是控件的一部分超出了边界。右上角的屏幕截图,curveSize = 20:
对于curveSize = 24:
【问题讨论】:
-
您需要在更改大小后使控件无效,通过覆盖
OnSizeChanged并调用Invalidate手动或通过设置ResizeRedraw=true自动设置。 -
你在寻找类似this的东西吗?
-
@RezaAghaei
ResizeRedraw=true工作,但边界没有抗锯齿。 -
这是一个不同的故事。事实上,对于平滑的边缘,您应该忘记更改区域。相反,您需要创建一个支持透明
BackColor的控件。
标签: c# winforms user-controls rounded-corners visual-artifacts