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



我做了一个非常简单的游戏,吃豆人追逐你的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);
}

我已经将"双缓冲"设置为 true,但这并没有改变任何东西。

如何消除闪烁?

我刚刚遇到了问题...

您必须将面板更改为图片框。

向面板创建一个子项,并将以下代码添加到构造函数中:

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

已经有这个问题了...

最新更新