gpt4 book ai didi

c# - 尽管有双缓冲,行情仍然闪烁

转载 作者:太空狗 更新时间:2023-10-29 23:10:22 26 4
gpt4 key购买 nike

有没有人知道如何消除闪烁?我在 SO 和网络上进行了研究,并尝试了许多不同的方法,例如将 TickerControl 放入双缓冲面板中 Double Buffering when not drawing in OnPaint(): why doesn't it work?等等,还有很多其他的东西。它仍然闪烁,不是在每次重绘时都闪烁,而是每秒闪烁几次。

此外,即使在 OnPaint 中删除了“g.Clear(BackColor)”之后,某些内容仍必须清除背景,因为文本继续以可读方式滚动。

这里是我的 TickerControl 类的相关部分:

class TickerControl : Control
{
private static readonly StringFormat stringFormat = new StringFormat(StringFormatFlags.NoWrap);

private const int padding = 40;
private const int scrollSleep = 10;
private const int scrollAdvancePixels = 1;

private float textWidth;
private float currentX;

private static readonly Timer scrollTimer = new Timer();

public TickerControl()
{
this.SetStyle(ControlStyles.UserPaint |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.OptimizedDoubleBuffer
, true);
scrollTimer.Tick += AdvanceText;
scrollTimer.Interval = scrollSleep;
scrollTimer.Enabled = true;
}

private void AdvanceText(object sender, EventArgs e)
{
if (IsDisposed)
return;

currentX -= scrollAdvancePixels;
if (currentX <= -textWidth)
currentX = 0;
Invalidate();
}

protected override void OnPaintBackground(PaintEventArgs pevent)
{
}

protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;

g.Clear(BackColor);

using (SolidBrush brush = new SolidBrush(ForeColor))
{
g.DrawString(Text, Font, brush, currentX, 0, stringFormat);
g.DrawString(Text, Font, brush, currentX + textWidth, 0, stringFormat);
}
}

有什么想法吗?

更新:

正如 sallushan 建议的手动双缓冲,让我补充一点,我之前已经尝试过,使用下面的代码。但在屏幕上它看起来与上面完全一样,所以问题似乎不在我的 OnPaint 方法中。我想它一定在我的控件设置中的某个地方。

    private Bitmap backBuffer;

protected override void OnPaint(PaintEventArgs e)
{
if (backBuffer == null)
backBuffer = new Bitmap(this.ClientSize.Width, this.ClientSize.Height);

Graphics g = Graphics.FromImage(backBuffer);

g.Clear(BackColor);

using (SolidBrush brush = new SolidBrush(ForeColor))
{
g.DrawString(Text, Font, brush, currentX, 0, stringFormat);
g.DrawString(Text, Font, brush, currentX + textWidth, 0, stringFormat);
}

g.Dispose();

e.Graphics.DrawImageUnscaled(backBuffer, 0, 0);
}

protected override void OnPaintBackground(PaintEventArgs pevent)
{
// Don't call base!
}

protected override void OnSizeChanged(EventArgs e)
{
if (backBuffer != null)
{
backBuffer.Dispose();
backBuffer = null;
}
base.OnSizeChanged(e);
}

最佳答案

在您的表单中设置此代码。它将消除闪烁

 protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000;

return cp;
}
}

希望对你有帮助

关于c# - 尽管有双缓冲,行情仍然闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7117705/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com