如何解决在一个WinForms形式忽悠? [英] How to fix flicker in a WinForms form?
本文介绍了如何解决在一个WinForms形式忽悠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不断地画帧,而我需要的形式,不闪烁。我如何做到这一点?
I am constantly drawing frames, and I need the form to not flicker. How do I accomplish this?
public partial class Form1 : Form
{
Image[] dude = new Image[3];
static int renderpoint = 0;
int lastimage = 0;
public Form1()
{
dude[1] = new Bitmap(@"snipe1.bmp");
dude[0] = new Bitmap(@"snipe0.bmp");
InitializeComponent();
}
private void Form1_Shown(object sender, EventArgs e)
{
MainLoop();
}
private void MainLoop()
{
double FPS = 10;
long ticks1 = 0;
long ticks2 = 0;
double interval = (double)Stopwatch.Frequency / FPS;
while (true)
{
ticks2 = Stopwatch.GetTimestamp();
if (ticks2 >= ticks1 + interval)
{
ticks1 = Stopwatch.GetTimestamp();
MoveGraphics();
this.Refresh();
}
Thread.Sleep(1);
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Rectangle rect = new Rectangle(renderpoint, 0, 100, 100);
Color lowcolor = Color.FromArgb(0, 128, 64);
Color highcolor = Color.FromArgb(0, 128, 64);
ImageAttributes imageAttr = new ImageAttributes();
imageAttr.SetColorKey(lowcolor, highcolor);
if (lastimage == 1)
{
lastimage = 0;
g.DrawImage(dude[1], rect, 0, 0, 100, 100, GraphicsUnit.Pixel, imageAttr);
}
else
{
lastimage = 1;
g.DrawImage(dude[0], rect, 0, 0, 100, 100, GraphicsUnit.Pixel, imageAttr);
}
}
void MoveGraphics()
{
if (renderpoint > 950)
{
renderpoint = 0;
}
else
{
renderpoint += 10;
}
}
}
有是当前code。建议?
There's the current code. Suggestions?
推荐答案
粘贴到Form1构造这样的:
Paste this into your Form1 constructor:
this.DoubleBuffered = true;
这篇关于如何解决在一个WinForms形式忽悠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文