Tec*_*eak 4 c# graphics controls winforms
我正在尝试制作带有圆角的标签控件。这是我继承自Label的myclass中的OnPaint()重写方法中的代码。
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
LinearGradientBrush brush = new LinearGradientBrush(this.ClientRectangle, GradientColor1, GradientColor2, 90);
e.Graphics.FillRectangle(brush, new Rectangle(new Point(0, 0), new Size(this.Width, this.Height)));
using (GraphicsPath gp = new GraphicsPath())
{
gp.AddArc(new Rectangle(new Point(0, 0), new Size(this.Height, this.Height)), 90, 180);
gp.AddLine(new Point(this.Height / 2, 0), new Point(this.Width - (this.Height / 2), 0));
gp.AddArc(new Rectangle(new Point(this.Width - this.Height, 0), new Size(this.Height, this.Height)), -90, 180);
gp.CloseFigure();
this.Region = new Region(gp);
}
base.OnPaint(e);
}
Run Code Online (Sandbox Code Playgroud)
The problem is that it is not smoothing the curve on the sides and showing the glitches. Here is the screenshot of the control :
如评论中所述,您不能对区域进行抗锯齿,因此必须使用图形的FillPath方法才能对图形进行抗锯齿
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
LinearGradientBrush brush = new LinearGradientBrush(this.ClientRectangle, Color.Aqua, Color.Blue, 90);
using (GraphicsPath gp = new GraphicsPath())
{
gp.AddArc(new Rectangle(new Point(0, 0), new Size(this.Height, this.Height)), 90, 180);
gp.AddLine(new Point(this.Height / 2, 0), new Point(this.Width - (this.Height / 2), 0));
gp.AddArc(new Rectangle(new Point(this.Width - this.Height, 0), new Size(this.Height, this.Height)), -90, 180);
gp.CloseFigure();
e.Graphics.FillPath(brush, gp);
}
base.OnPaint(e);
}
Run Code Online (Sandbox Code Playgroud)
如果您确实需要更改区域,我想您必须使用比用于绘图的区域稍大的区域:
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.InterpolationMode = InterpolationMode.HighQualityBilinear;
e.Graphics.CompositingQuality = CompositingQuality.HighQuality;
e.Graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
LinearGradientBrush brush = new LinearGradientBrush(this.ClientRectangle, Color.Aqua, Color.Blue, 90);
using (GraphicsPath gp = new GraphicsPath())
{
AddRoundedRectangle(gp, new Point(1, 1), new Size(this.Size.Width - 2, Size.Height - 2));
e.Graphics.FillPath(brush, gp);
}
using (GraphicsPath gp = new GraphicsPath())
{
AddRoundedRectangle(gp, new Point(0, 0), this.Size);
this.Region = new Region(gp);
}
base.OnPaint(e);
}
private void AddRoundedRectangle(GraphicsPath gp, Point upperLeft, Size size)
{
gp.AddArc(new Rectangle(upperLeft, new Size(size.Height, size.Height)), 90, 180);
gp.AddLine(new Point(size.Height / 2 , upperLeft.Y), new Point(size.Width - (size.Height / 2), upperLeft.Y));
gp.AddArc(new Rectangle(new Point(size.Width - size.Height, upperLeft.Y), new Size(size.Height, size.Height)), -90, 180);
gp.CloseFigure();
}
Run Code Online (Sandbox Code Playgroud)
第二个选项的优点是可以影响MouseHover之类的事件,但可能会导致您看到该区域的“边界”。