缩小带有圆角和彩色的用户控件时边框,边框的右侧变得不可见。另外,放大时右侧会出现多个黄色边框。
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); }
至为了解决这些问题,提出以下建议:
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); } }
更新的代码片段成功防止了由带圆角的可缩放 UserControl 的彩色边框引起的视觉伪影。应用缩放和平移矩阵的修改后的区域可确保所有缩放级别的边框抗锯齿。
以上是如何修复具有圆角和彩色边框的可缩放用户控件中的视觉伪影?的详细内容。更多信息请关注PHP中文网其他相关文章!