Home >Backend Development >C++ >How to Fix Visual Artifacts in a Zoomable UserControl with Rounded Corners and a Colored Border?
When zooming out a user control with rounded corners and a colored border, the right side of the border becomes invisible. Additionally, multiple yellow borders appear on the right side when zooming in.
Form1.Designer.cs
trackBar1.Value = 100; BackColor = Color.Gray;
Form1.cs
private void trackBar1_Scroll(object sender, EventArgs e) { UserControl11.SetZoomFactor(trackBar1.Value / 100F); }
UserControl1.cs
// Properties, constructors, and event handlers omitted for brevity internal GraphicsPath GraphicsPathWithBorder; internal void SetZoomFactor(float z) { Width = (int)(MyBaseWidth * z); GraphicsPathWithBorder = RoundedCornerRectangle(ClientRectangle); Region = new Region(GraphicsPathWithBorder); }
To address the artifacts, the following recommendations are made:
public partial class RoundControl : UserControl { // Properties, constructors, and event handlers omitted for brevity protected override void OnPaint(PaintEventArgs e) { e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; RectangleF rect = GraphicsPathWithBorder.GetBounds(); float scaleX = 1 - ((m_PenSize + 1) / rect.Width); float scaleY = 1 - ((m_PenSize + 1) / rect.Height); using (Pen pen = new Pen(m_BorderColor, m_PenSize)) using (Brush brush = new SolidBrush(m_FillColor)) using (Matrix mx = new Matrix(scaleX, 0, 0, scaleY, pen.Width / 2, pen.Width / 2)) { e.Graphics.Transform = mx; e.Graphics.FillPath(brush, GraphicsPathWithBorder); e.Graphics.DrawPath(pen, GraphicsPathWithBorder); } base.OnPaint(e); } }
The updated code snippet successfully prevents the visual artifacts caused by the colored border of the zoomable UserControl with rounded corners. The modified Region with applied scale and translate matrix ensures anti-aliased borders at all zoom levels.
The above is the detailed content of How to Fix Visual Artifacts in a Zoomable UserControl with Rounded Corners and a Colored Border?. For more information, please follow other related articles on the PHP Chinese website!