更改位置时WinForm上的闪烁控制

时间:2017-10-27 06:08:18

标签: c# winforms animation

我做了一个非常简单的游戏,其中一个吃豆人追逐你的mouspointer。 但是每当它改变它的位置时,pacMan - 带有图像的面板就会闪烁。

private void Move_Tick(object sender, EventArgs e)
{
    int newPositionX = pacMan.Location.X;
    int newPositionY = pacMan.Location.Y;

    lblMousX.Text = PointToClient(Cursor.Position).X.ToString();
    lblMouseY.Text = PointToClient(Cursor.Position).Y.ToString();
    lblThingX.Text = pacMan.Location.X.ToString();
    lblThingY.Text = pacMan.Location.Y.ToString();

    if (pacMan.Location.X + 15 < PointToClient(Cursor.Position).X)
    {
        newPositionX = pacMan.Location.X + 1;
        if (pacMan.BackgroundImage != Properties.Resources.Pac_Rechts)
            pacMan.BackgroundImage = Properties.Resources.Pac_Rechts;
    }
    if (pacMan.Location.X + 15 > PointToClient(Cursor.Position).X)
    {
        newPositionX = pacMan.Location.X - 1;
        if (pacMan.BackgroundImage != Properties.Resources.Pac_Links)
            pacMan.BackgroundImage = Properties.Resources.Pac_Links;
    }
    if (pacMan.Location.Y + 15 < PointToClient(Cursor.Position).Y)
    {
        newPositionY = pacMan.Location.Y + 1;
        if (pacMan.BackgroundImage != Properties.Resources.Pac_Unten)
            pacMan.BackgroundImage = Properties.Resources.Pac_Unten;
    }
    if (pacMan.Location.Y + 15 > PointToClient(Cursor.Position).Y)
    {
        newPositionY = pacMan.Location.Y - 1;
        if (pacMan.BackgroundImage != Properties.Resources.Pac_Oben)
            pacMan.BackgroundImage = Properties.Resources.Pac_Oben;
    }

    pacMan.Location = new Point(newPositionX, newPositionY);
}

我已经设置了#34; DoubleBuffered&#34;是真的,但没有改变任何事情。

如何消除闪烁?

2 个答案:

答案 0 :(得分:0)

我刚遇到问题......

您必须将Panel更改为PictureBox。

答案 1 :(得分:0)

为Panel创建一个子项并将此代码添加到构造函数中:

this.SetStyle(
            System.Windows.Forms.ControlStyles.UserPaint |
            System.Windows.Forms.ControlStyles.AllPaintingInWmPaint |
            System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer,
            true);

这个问题已经......