C# 如何修复矩形形状上的闪烁?

C# 如何修复矩形形状上的闪烁?,c#,winforms,C#,Winforms,我用一个简单的游戏的形式完成了这段代码,在点击带有透明背景的图片时显示一个splat。当屏幕上的闪烁次数增加时,闪烁也会增加,如何消除闪烁 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.For

我用一个简单的游戏的形式完成了这段代码,在点击带有透明背景的图片时显示一个splat。当屏幕上的闪烁次数增加时,闪烁也会增加,如何消除闪烁

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;


using System.Runtime.InteropServices;namespace UFC
{
    public partial class FormDropAndShoot : Form
    {
    int time1 = 0;
    int y1 = -10;
    int y2 = -1000;
    int y3 = -800;
    int y4 = -400;
    int y5 = -1200;
    int y6 = -500;
    int position = 0;
    int counter1 = 0;
    int counter2 = 0;
    int intOriginalExStyle = -1;
    bool bEnableAntiFlicker = true;

    public FormDropAndShoot()
    {
        //ToggleAntiFlicker(false);
        InitializeComponent();
        //this.ResizeBegin += new EventHandler(Form1_ResizeBegin);
        //this.ResizeEnd += new EventHandler(Form1_ResizeEnd);
    }


    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.ExStyle |= 0x02000000;  // Turn on WS_EX_COMPOSITED
            return cp;
        }
    }


    protected override CreateParams CreateParams
    {
        get
        {
            if (intOriginalExStyle == -1)
            {
                intOriginalExStyle = base.CreateParams.ExStyle;
            }
            CreateParams cp = base.CreateParams;

            if (bEnableAntiFlicker)
            {
                cp.ExStyle |= 0x02000000; //WS_EX_COMPOSITED
            }
            else
            {
                cp.ExStyle = intOriginalExStyle;
            }

            return cp;
        }
    }

    private void Form1_ResizeBegin(object sender, EventArgs e)
    {
        ToggleAntiFlicker(true);
    }

    private void Form1_ResizeEnd(object sender, EventArgs e)
    {
        ToggleAntiFlicker(false);
    }

    private void ToggleAntiFlicker(bool Enable)
    {
        bEnableAntiFlicker = Enable;
        //hacky, but works
        this.MaximizeBox = true;
    }

    #region Timer

    //timer
    private void timer1_Tick(object sender, EventArgs e)
    {
        time1++;

        if ((time1 % 2) != 0)
        {
            drop1.Location = new Point(55, y1);
            y1 = y1 + 10;
            drop1.Visible = true;

            drop2.Location = new Point(221, y2);
            y2 = y2 + 10;
            drop2.Visible = true;

            drop3.Location = new Point(381, y3);
            y3 = y3 + 10;
            drop3.Visible = true;

            drop4.Location = new Point(543, y4);
            y4 = y4 + 10;
            drop4.Visible = true;

            drop5.Location = new Point(717, y5);
            y5 = y5 + 10;
            drop5.Visible = true;

            drop6.Location = new Point(885, y6);
            y6 = y6 + 10;
            drop6.Visible = true;

            if (drop1.Location == new Point(55, 1940))
            {
                y1 = -10;
                drop1.Location = new Point(55, y1);
                random1();
                drop1.Visible = false;
            }
            else if (drop2.Location == new Point(221, 1940))
            {
                y2 = -10;
                drop2.Location = new Point(221, y1);
                random2();
                drop2.Visible = false;
            }
            else if (drop3.Location == new Point(381, 1940))
            {
                y3 = -10;
                drop3.Location = new Point(381, y1);
                random3();
                drop3.Visible = false;
            }
            else if (drop4.Location == new Point(543, 1940))
            {
                y4 = -10;
                drop4.Location = new Point(543, y1);
                random4();
                drop4.Visible = false;
            }
            else if (drop5.Location == new Point(717, 1940))
            {
                y5 = -10;
                drop5.Location = new Point(717, y1);
                random5();
                drop5.Visible = false;
            }
            else if (drop6.Location == new Point(885, 1940))
            {
                y6 = -10;
                drop6.Location = new Point(885, y1);
                random6();
                drop6.Visible = false;
            }
        }
    }


    #endregion

    #region Food

    //pictureBox_Click
    private void drop1_Click(object sender, EventArgs e)
    {
        counter1++;

        if (counter1 == 1)
        {
            position = y1;
            rectangleShape1.Visible = true;
            rectangleShape1.Location = new Point(55, position);
            rectangleShape1.BringToFront();
        }
        else if (counter1 == 2)
        {
            position = y1;
            rectangleShape2.Visible = true;
            rectangleShape2.Location = new Point(55, position);
            rectangleShape2.BringToFront();
        }
        else if (counter1 == 3)
        {
            position = y1;
            rectangleShape3.Visible = true;
            rectangleShape3.Location = new Point(55, position);
            rectangleShape3.BringToFront();
        }
        else if (counter1 == 4)
        {
            position = y1;
            rectangleShape4.Visible = true;
            rectangleShape4.Location = new Point(55, position);
            rectangleShape4.BringToFront();
        }
        else if (counter1 == 5)
        {
            position = y1;
            rectangleShape5.Visible = true;
            rectangleShape5.Location = new Point(55, position);
            rectangleShape5.BringToFront();
        }
        drop1.BringToFront();
        drop2.BringToFront();
        drop1.Location = new Point(55, -600);
        y1 = -600;
        random1();
        drop1.Visible = false;
    }

    private void drop2_Click(object sender, EventArgs e)
    {
        counter2++;

        drop2.Location = new Point(221, -200);
        y2 = -200;
        random2();
        drop2.Visible = false;
    }

    private void drop3_Click(object sender, EventArgs e)
    {
        drop3.Location = new Point(381, -800);
        y3 = -800;
        random3();
        drop3.Visible = false;
    }

    private void drop4_Click(object sender, EventArgs e)
    {
        drop4.Location = new Point(543, -500);
        y4 = -500;
        random4();
        drop4.Visible = false;
    }

    private void drop5_Click(object sender, EventArgs e)
    {
        drop5.Location = new Point(717, -300);
        y5 = -300;
        random5();
        drop5.Visible = false;
    }

    private void drop6_Click(object sender, EventArgs e)
    {
        drop6.Location = new Point(885, -1000);
        y6 = -1000;
        random6();
        drop6.Visible = false;
    }

    #endregion

    #region RandomImage

    //randomImage
    public void random1()
    {
        Random rmd = new Random();
        int x = rmd.Next(1, 6);

        if (x == 1)
        {
            drop1.BackgroundImage = Image.FromFile(@"C:\Users\H61Thin\Desktop\SamplePictures\ChickenLegs.png");
            drop1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        }
        else if (x == 2)
        {
            drop1.BackgroundImage = Image.FromFile(@"C:\Users\H61Thin\Desktop\SamplePictures\egg.png");
            drop1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        }
        else if (x == 3)
        {
            drop1.BackgroundImage = Image.FromFile(@"C:\Users\H61Thin\Desktop\SamplePictures\ham.png");
            drop1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        }
        else if (x == 4)
        {
            drop1.BackgroundImage = Image.FromFile(@"C:\Users\H61Thin\Desktop\SamplePictures\fish.png");
            drop1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        }
        else if (x == 5)
        {
            drop1.BackgroundImage = Image.FromFile(@"C:\Users\H61Thin\Desktop\SamplePictures\ChickenLegs.png");
            drop1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        }
        else if (x == 6)
        {
            drop1.BackgroundImage = Image.FromFile(@"C:\Users\H61Thin\Desktop\SamplePictures\ChickenLegs.png");
            drop1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        }
    }

    public void random2()
    {
        Random rmd = new Random();
        int x = rmd.Next(1, 6);

        if (x == 1)
        {
            drop2.BackgroundImage = Image.FromFile(@"C:\Users\H61Thin\Desktop\SamplePictures\OrangeSun.png");
            drop2.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        }
        else if (x == 2)
        {
            drop2.BackgroundImage = Image.FromFile(@"C:\Users\H61Thin\Desktop\SamplePictures\BlueSun.png");
            drop2.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        }
        else if (x == 3)
        {
            drop2.BackgroundImage = Image.FromFile(@"C:\Users\H61Thin\Desktop\SamplePictures\GreenSun.png");
            drop2.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        }
        else if (x == 4)
        {
            drop2.BackgroundImage = Image.FromFile(@"C:\Users\H61Thin\Desktop\SamplePictures\RedSun.png");
            drop2.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        }
        else if (x == 5)
        {
            drop2.BackgroundImage = Image.FromFile(@"C:\Users\H61Thin\Desktop\SamplePictures\YellowSun.png");
            drop2.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        }
        else if (x == 6)
        {
            drop2.BackgroundImage = Image.FromFile(@"C:\Users\H61Thin\Desktop\SamplePictures\PinkSun.png");
            drop2.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        }
    }

    public void random3()
    {
        Random rmd = new Random();
        int x = rmd.Next(1, 6);

        if (x == 1)
        {
            drop3.BackgroundImage = Image.FromFile(@"C:\Users\H61Thin\Desktop\SamplePictures\YellowSun.png");
            drop3.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        }
        else if (x == 2)
        {
            drop3.BackgroundImage = Image.FromFile(@"C:\Users\H61Thin\Desktop\SamplePictures\OrangeSun.png");
            drop3.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        }
        else if (x == 3)
        {
            drop3.BackgroundImage = Image.FromFile(@"C:\Users\H61Thin\Desktop\SamplePictures\RedSun.png");
            drop3.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        }
        else if (x == 4)
        {
            drop3.BackgroundImage = Image.FromFile(@"C:\Users\H61Thin\Desktop\SamplePictures\PinkSun.png");
            drop3.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        }
        else if (x == 5)
        {
            drop3.BackgroundImage = Image.FromFile(@"C:\Users\H61Thin\Desktop\SamplePictures\BlueSun.png");
            drop3.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        }
        else if (x == 6)
        {
            drop3.BackgroundImage = Image.FromFile(@"C:\Users\H61Thin\Desktop\SamplePictures\GreenSun.png");
            drop3.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        }
    }

    public void random4()
    {
        Random rmd = new Random();
        int x = rmd.Next(1, 6);

        if (x == 1)
        {
            drop4.BackgroundImage = Image.FromFile(@"C:\Users\H61Thin\Desktop\SamplePictures\PinkSun.png");
            drop4.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        }
        else if (x == 2)
        {
            drop4.BackgroundImage = Image.FromFile(@"C:\Users\H61Thin\Desktop\SamplePictures\BlueSun.png");
            drop4.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        }
        else if (x == 3)
        {
            drop4.BackgroundImage = Image.FromFile(@"C:\Users\H61Thin\Desktop\SamplePictures\YellowSun.png");
            drop4.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        }
        else if (x == 4)
        {
            drop4.BackgroundImage = Image.FromFile(@"C:\Users\H61Thin\Desktop\SamplePictures\RedSun.png");
            drop4.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        }
        else if (x == 5)
        {
            drop4.BackgroundImage = Image.FromFile(@"C:\Users\H61Thin\Desktop\SamplePictures\GreenSun.png");
            drop4.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        }
        else if (x == 6)
        {
            drop4.BackgroundImage = Image.FromFile(@"C:\Users\H61Thin\Desktop\SamplePictures\OrangeSun.png");
            drop4.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        }
    }

    public void random5()
    {
        Random rmd = new Random();
        int x = rmd.Next(1, 6);

        if (x == 1)
        {
            drop5.BackgroundImage = Image.FromFile(@"C:\Users\H61Thin\Desktop\SamplePictures\RedSun.png");
            drop5.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        }
        else if (x == 2)
        {
            drop5.BackgroundImage = Image.FromFile(@"C:\Users\H61Thin\Desktop\SamplePictures\PinkSun.png");
            drop5.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        }
        else if (x == 3)
        {
            drop5.BackgroundImage = Image.FromFile(@"C:\Users\H61Thin\Desktop\SamplePictures\BlueSun.png");
            drop5.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        }
        else if (x == 4)
        {
            drop5.BackgroundImage = Image.FromFile(@"C:\Users\H61Thin\Desktop\SamplePictures\GreenSun.png");
            drop5.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        }
        else if (x == 5)
        {
            drop5.BackgroundImage = Image.FromFile(@"C:\Users\H61Thin\Desktop\SamplePictures\OrangeSun.png");
            drop5.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        }
        else if (x == 6)
        {
            drop5.BackgroundImage = Image.FromFile(@"C:\Users\H61Thin\Desktop\SamplePictures\YellowSun.png");
            drop5.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        }
    }

    public void random6()
    {
        Random rmd = new Random();
        int x = rmd.Next(1, 6);

        if (x == 1)
        {
            drop6.BackgroundImage = Image.FromFile(@"C:\Users\H61Thin\Desktop\SamplePictures\OrangeSun.png");
            drop6.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        }
        else if (x == 2)
        {
            drop6.BackgroundImage = Image.FromFile(@"C:\Users\H61Thin\Desktop\SamplePictures\YellowSun.png");
            drop6.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        }
        else if (x == 3)
        {
            drop6.BackgroundImage = Image.FromFile(@"C:\Users\H61Thin\Desktop\SamplePictures\BlueSun.png");
            drop6.BackgroundImageLayout = 

System.Windows.Forms.ImageLayout.Stretch;
            }
            else if (x == 4)
            {
                drop6.BackgroundImage = Image.FromFile(@"C:\Users\H61Thin\Desktop\SamplePictures\RedSun.png");
                drop6.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            }
            else if (x == 5)
            {
                drop6.BackgroundImage = Image.FromFile(@"C:\Users\H61Thin\Desktop\SamplePictures\PinkSun.png");
                drop6.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            }
            else if (x == 6)
            {
                drop6.BackgroundImage = Image.FromFile(@"C:\Users\H61Thin\Desktop\SamplePictures\GreenSun.png");
                drop6.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            }
        }

        #endregion

    }
}

activate
DoubleBuffered=true
?我已经尝试了双缓冲和创建一个名为createparams的方法的方法,很抱歉我在c中的编码太新了,你的代码太长了,或者我读不懂。闪烁源于在非双缓冲控件(通常是窗体或面板)上绘制。PictureBox始终是双缓冲的,窗体有一个属性需要打开,但面板需要子类化。im使用矩形,因为PictureBox不接受png文件的透明度
,如果((time1%2)!=0)
这是不必要的。只需将计时器的时间延长一倍:?如何激活
DoubleBuffered=true
?我已经尝试了双缓冲和创建一个名为createparams的方法,很抱歉,我在c中的编码非常新,你的代码太长,我无法阅读。闪烁源于在非双缓冲控件(通常是窗体或面板)上绘制。PictureBox始终是双缓冲的,窗体有一个属性需要打开,但面板需要子类化。im使用矩形,因为PictureBox不接受png文件的透明度
,如果((time1%2)!=0)
这是不必要的。只需将计时器的时间延长一倍:?