縮小帶有圓角和彩色的使用者控制時邊框,邊框的右側變得不可見。另外,放大時右側會出現多個黃色邊框。
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中文網其他相關文章!