看来这是一个已知的错误...
以下代码似乎可以按您的要求运行:
protected override void OnPaint(PaintEventArgs e)
{
PointF[] points = new PointF[] { new PointF(73.36f, 196),
new PointF(75.44f, 32),
new PointF(77.52f, 32),
new PointF(79.6f, 196),
new PointF(85.84f, 196) };
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
Rectangle b = new Rectangle(70, 32, 20, 165);
e.Graphics.SetClip(b);
e.Graphics.DrawLines(Pens.Red, points); // clipped incorrectly
e.Graphics.TranslateTransform(80, 0);
e.Graphics.ResetClip();
e.Graphics.DrawLines(Pens.Red, points);
}
注意:我已经对线条进行了抗锯齿处理,并将您的剪切区域扩展了 1
似乎以下变通方法可能会有所帮助(尽管未经测试):
- 笔的厚度超过一个像素
- 线条完全水平或垂直
- 剪裁是针对窗口边界而不是剪裁矩形进行的
以下是可能/或再次可能无济于事的文章列表:
http://www.tech-archive.net/pdf/Archive/Development/microsoft.public.win32.programmer.gdi/2004-08/0350.pdf
http://www.tech-archive.net/Archive/Development/microsoft.public.win32.programmer.gdi/2004-08/0368.html
或者...
以下也是可能的:
protected override void OnPaint ( PaintEventArgs e )
{
PointF[] points = new PointF[] { new PointF(73.36f, 196),
new PointF(75.44f, 32),
new PointF(77.52f, 32),
new PointF(79.6f, 196),
new PointF(85.84f, 196) };
Rectangle b = new Rectangle( 70, 32, 20, 164 );
Region reg = new Region( b );
e.Graphics.SetClip( reg, System.Drawing.Drawing2D.CombineMode.Union);
e.Graphics.DrawLines( Pens.Red, points ); // clipped incorrectly
e.Graphics.TranslateTransform( 80, 0 );
e.Graphics.ResetClip();
e.Graphics.DrawLines( Pens.Red, points );
}
这有效地使用区域组合/联合(我认为)与画布/控件的 ClientRectangle 进行剪辑。由于该区域与矩形不同,因此结果应该是预期的。可以通过添加来证明此代码可以工作
e.Graphics.FillRectangle( new SolidBrush( Color.Black ), b );
在 setClip() 调用之后。这清楚地显示了仅出现在剪切区域中的黑色矩形。
如果抗锯齿线不是一个选项,这可能是一个有效的解决方法。
希望对你有帮助