C#面板闪烁。当我尝试CreateParams时,情况要糟糕得多

时间:2013-05-13 07:43:37

标签: c# graphics paint createparams

标题几乎说明了。我的表格中有一个面板。我在面板中有很多形状互相反弹。我的形状越多,闪烁就越严重。在这个网站上搜索了一段时间后,我将其添加到构造函数

    this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint 
    | ControlStyles.DoubleBuffer, true);

然后我发现我也应该添加这个

protected override CreateParams CreateParams
{
  get
  {
    CreateParams cp = base.CreateParams;
    cp.ExStyle |= 0x02000000;  // Turn on WS_EX_COMPOSITED (child control double-buffering)
    return cp;
  }
}

我加上这个后,屏幕闪烁得更厉害。说它闪烁是不公平的。大多数时候,它只是空白,每隔一段时间,形状就会短暂闪现。我绘制形状的代码看起来像这样。

pnl.Refresh();
Graphics g = pnl.CreateGraphics();
for(int i = 0; i < numOfShapes; i++)
{
    Rectangle myRect = shapeList[i];
    g.FillRectangle(new SolidBrush(Color.Black), myRect);
}

编辑:

我不知道为什么我不能让最后一点代码看起来正确。对于那个很抱歉。我只是刷新面板然后循环遍历一个矩形阵列并将它们绘制到面板

0 个答案:

没有答案
相关问题