我正在尝试实现以下方法:
问题是,由于需要进行大量绘制,因此显示器开始出现闪烁。我已经搜索了双缓冲的方法,但我只能找到以下两种:
1.
当我只有作为输入参数的Graphics对象的所有访问权限时,是否有可能使用双缓冲?
提前感谢,
编辑: 我创建了以下类
而球drawOn的样子如下:
你的意思是这样吗?因为它仍然在闪烁?
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方法。当我只有作为输入参数的Graphics对象的所有访问权限时,是否有可能使用双缓冲?
提前感谢,
编辑: 我创建了以下类
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 (Point 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);
你的意思是这样吗?因为它仍然在闪烁?