我正在尝试实现以下方法: void Ball::DrawOn(Graphics g);
该方法应绘制球的所有先前位置(存储在队列中),最后绘制当前位置。我不知道这是否重要,但我使用 g.DrawEllipse(...) 打印以前的位置,使用 g.FillEllipse(...) 打印当前位置。
问题是,正如您可以想象的那样,有很多绘图要做,因此显示屏开始闪烁很多。我一直在寻找一种双缓冲的方法,但我能找到的只有这两种方法:
1) System.Windows.Forms.Control.DoubleBuffered = true;
2) SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
在尝试使用第一个时,我收到一个错误,说明在此方法中,属性 DoubleBuffered 由于其保护级别而无法访问。虽然我不知道如何使用 SetStyle 方法。
当我拥有的所有访问权限都是对我在方法中作为输入获得的图形对象时,是否有可能加倍缓冲区?
提前致谢,
编辑:我创建了以下类
namespace doubleBuffer
{
class BufferedBall : System.Windows.Forms.Form{
private Ball ball;
public BufferedBall(Ball ball)
{
this.ball = ball;
}
public void DrawOn(Graphics g){
this.DoubleBuffered = true;
int num = 0;
Rectangle drawArea1 = new Rectangle(5, 35, 30, 100);
LinearGradientBrush linearBrush1 =
new LinearGradientBrush(drawArea1, Color.Green, Color.Orange, LinearGradientMode.Horizontal);
Rectangle drawArea2 = new Rectangle(5, 35, 30, 100);
LinearGradientBrush linearBrush2 =
new LinearGradientBrush(drawArea2, Color.Black, Color.Red, LinearGradientMode.Vertical);
foreach (PointD point in ball.previousLocations)
{
Pen myPen1;
if (num % 3 == 0)
myPen1 = new Pen(Color.Yellow, 1F);
else if (num % 3 == 1)
myPen1 = new Pen(Color.Green, 2F);
else
myPen1 = new Pen(Color.Red, 3F);
num++;
myPen1.DashStyle = System.Drawing.Drawing2D.DashStyle.Solid;
myPen1.StartCap = System.Drawing.Drawing2D.LineCap.RoundAnchor;
myPen1.EndCap = System.Drawing.Drawing2D.LineCap.AnchorMask;
g.DrawEllipse(myPen1, (float)(point.X - ball.radius), (float)(point.Y + ball.radius), (float)(2 * ball.radius), (float)(2 * ball.radius));
}
if ((ball.Host.ElapsedTime * ball.Host.FPS * 10) % 2 == 0){
g.FillEllipse(linearBrush1, (float)(ball.Location.X - ball.radius), (float)(ball.Location.Y + ball.radius), (float)(2 * ball.radius), (float)(2 * ball.radius));
}else{
g.FillEllipse(linearBrush2, (float)(ball.Location.X - ball.radius), (float)(ball.Location.Y + ball.radius), (float)(2 * ball.radius), (float)(2 * ball.radius));
}
}
}
}
球 drawOn 看起来像这样:
new BufferedBall(this).DrawOn(g);
这是你的意思吗?因为它还在闪烁?