【问题标题】:C# draw a moving particleC#画一个运动的粒子
【发布时间】:2013-05-10 04:40:45
【问题描述】:

我对 c# 非常陌生,我有一个简单的问题: 我应该在黑色背景上绘制一个白色粒子(矩形)并将其从一个屏幕水平移动到另一个屏幕。 我做到了,但问题是它闪烁太多(即即使速度很高也不流畅,我可以很容易地看到每个动作和另一个动作之间的黑色背景)

t.Interval = 1000 / speed;
t.Tick += new EventHandler(t_Tick);
t.Start();

....

void t_Tick(object sender, EventArgs e)
        {
            //g.Clear(Color.Black);
            g.DrawRectangle(new Pen(Brushes.Black, 20), r);      //draw a black rectangle in the old position...20 is the thickness of the pen
            r.X += move_x;
            g.DrawRectangle(new Pen(Brushes.White, 20), r);      //draw a white rectangle in the new position...20 is the thickness of the pen
            if (r.X >= 1700)       ///this means it reached the end of the screen
                t.Stop();
        }

我使用 g.Clear 清除图形,但这也不起作用,所以我在旧位置绘制了一个黑色矩形,然后将其移动到新位置。

任何想法如何消除这种闪烁,甚至以其他方式做到这一点?

【问题讨论】:

  • 尝试将表单的DoubleBuffered 属性设置为true
  • 在表单的设计视图中,进入属性窗口(View -> Properties Window),找到“DoubleBuffered”行,将值更改为True
  • 谢谢。我试过了,但我没有工作..还是太眨眼了!

标签: c# draw move particles


【解决方案1】:

试试这个...在表单中添加一个面板(panel1):

public partial class Form1 : Form
{

    private Rectangle r;
    private const int rSize = 50;
    private const int move_x = 10;
    private System.Windows.Forms.Timer tmr;

    public Form1()
    {
        InitializeComponent();

        panel1.BackColor = Color.Black;
        r = new Rectangle(0, panel1.Height / 2 - rSize / 2, rSize, rSize);

        tmr = new System.Windows.Forms.Timer();
        tmr.Interval = 50;
        tmr.Tick += new EventHandler(tmr_Tick);
        tmr.Start();

        panel1.Paint += new PaintEventHandler(panel1_Paint);
    }

    void tmr_Tick(object sender, EventArgs e)
    {
        r.X += move_x;
        panel1.Refresh();
        if (r.X > panel1.Width)
        {
            tmr.Stop();
            MessageBox.Show("Done");
        }
    }

    void panel1_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.DrawRectangle(Pens.White, r);
    }

}

【讨论】:

  • 更好?...更糟?面板有多大?你还有别的图吗?你能告诉我们你正在使用的完整代码吗?
  • 一样,没有好坏之分,没有其他图纸。我的代码和你的完全一样。我只是复制/粘贴它!
  • 我运行的是Win8,没有闪烁。你在做什么?
  • windows7.我会在另一台机器上检查它,并会回复你。非常感谢
猜你喜欢
  • 1970-01-01
  • 2021-09-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多