0

Столкнулся с такой проблемкой. На форме есть 4 кнопки при наведении которой они увеличиваются в ширину . Так вот я использовал async чтобы плавно её анимировать , вроде получилось как надо , но собственно проблема : При быстром наведении на все кнопки они начинают хаотично увеличиваться в размере не достигая нужного размера

Как выглядит после медленного наведения на все кнопки как выглядит после медленного наведения на все кнопки :

Как выглядит после быстрого наведения на все кнопки как выглядит после быстрого наведения на все кнопки

Код :

private void MainWindow_Load(object sender, EventArgs e)
        {
            this.CenterToScreen();
            new Thread(NewGameAnimation).Start();
            new Thread(RecordsAnimation).Start();
            new Thread(CreditsAnimation).Start();
            new Thread(ExitAnimation).Start();
        }
        //=========================================================
        //=========================================================
        //=========================================================
        private void NewGameAnimation()
        {
            //=========================================================
            //====================NewGame Animation====================
            //=========================================================
            NewGame.MouseEnter += async (s, e) =>
            {
                do
                {
                    EnterExpect = true;
                    await Task.Delay(1);
                    NewGame.Size = new Size(NewGame.Width + NewGame.Width / 35, NewGame.Height);
                    NewGame.Location = new Point((this.Width - NewGame.Width) / 2 - 9, NewGame.Location.Y);
                    EnterExpect = false;
                }
                while (!EnterExpect && NewGame.Width <= 450);
            };
        NewGame.MouseLeave += async (s, e) =&gt;
        {
            do
            {
                EnterExpect = true;
                await Task.Delay(1);
                NewGame.Size = new Size(NewGame.Width - NewGame.Width / 35, NewGame.Height);
                NewGame.Location = new Point((this.Width - NewGame.Width) / 2 - 9, NewGame.Location.Y);
                EnterExpect = false;
            }
            while (!EnterExpect &amp;&amp; NewGame.Width &gt;= 350);
        };
        NewGame.Click += new EventHandler(GameStart);
    }
    private void RecordsAnimation()
    {
        //=========================================================
        //=================RecordButton Animation==================
        //=========================================================
        Records.MouseEnter += async (s, e) =&gt;
        {
            do
            {
                EnterExpect = true;
                await Task.Delay(1);
                Records.Size = new Size(Records.Width + Records.Width / 35, Records.Height);
                Records.Location = new Point((this.Width - Records.Width) / 2 - 9, Records.Location.Y);
                Exit.Size = new Size(Exit.Width - Exit.Width / 35, Exit.Height);
                Exit.Location = new Point((this.Width - Exit.Width) / 2 - 9, Exit.Location.Y);
                Credits.Size = new Size(Credits.Width - Credits.Width / 35, Credits.Height);
                Credits.Location = new Point((this.Width - Credits.Width) / 2 - 9, Credits.Location.Y);
                NewGame.Size = new Size(NewGame.Width - NewGame.Width / 35, NewGame.Height);
                NewGame.Location = new Point((this.Width - NewGame.Width) / 2 - 9, NewGame.Location.Y);
                EnterExpect = false;
            }
            while (!EnterExpect &amp;&amp; Records.Width &lt;= 450);
        };
        Records.MouseLeave += async (s, e) =&gt;
        {
            do
            {
                EnterExpect = true;
                await Task.Delay(1);
                Records.Size = new Size(Records.Width - Records.Width / 35, Records.Height);
                Records.Location = new Point((this.Width - Records.Width) / 2 - 9, Records.Location.Y);
                EnterExpect = false;
            }
            while (!EnterExpect &amp;&amp; Records.Width &gt;= 350);
        };
    }
    private void CreditsAnimation()
    {
        //=========================================================
        //=================CreditsButton Animation=================
        //=========================================================
        Credits.MouseEnter += async (s, e) =&gt;
        {
            do
            {
                EnterExpect = true;
                await Task.Delay(1);
                Credits.Size = new Size(Credits.Width + Credits.Width / 35, Credits.Height);
                Credits.Location = new Point((this.Width - Credits.Width) / 2 - 9, Credits.Location.Y);
                Exit.Size = new Size(Exit.Width - Exit.Width / 35, Exit.Height);
                Exit.Location = new Point((this.Width - Exit.Width) / 2 - 9, Exit.Location.Y);
                Records.Size = new Size(Records.Width - Records.Width / 35, Records.Height);
                Records.Location = new Point((this.Width - Records.Width) / 2 - 9, Records.Location.Y);
                NewGame.Size = new Size(NewGame.Width - NewGame.Width / 35, NewGame.Height);
                NewGame.Location = new Point((this.Width - NewGame.Width) / 2 - 9, NewGame.Location.Y);
                EnterExpect = false;
            }
            while (!EnterExpect &amp;&amp; Credits.Width &lt;= 450);
        };
        Credits.MouseLeave += async (s, e) =&gt;
        {
            do
            {
                EnterExpect = true;
                await Task.Delay(1);
                Credits.Size = new Size(Credits.Width - Credits.Width / 35, Credits.Height);
                Credits.Location = new Point((this.Width - Credits.Width) / 2 - 9, Credits.Location.Y);
                EnterExpect = false;
            }
            while (!EnterExpect &amp;&amp; Credits.Width &gt;= 350);
        };
    }
    private void ExitAnimation()
    {
        //=========================================================
        //==================ExitButton Animation===================
        //=========================================================

        Exit.MouseEnter += async (s, e) =&gt;
        {
            do
            {
                EnterExpect = true;
                await Task.Delay(1);
                Exit.Size = new Size(Exit.Width + Exit.Width / 35, Exit.Height);
                Exit.Location = new Point((this.Width - Exit.Width) / 2 - 9, Exit.Location.Y);
                Credits.Size = new Size(Credits.Width - Credits.Width / 35, Credits.Height);
                Credits.Location = new Point((this.Width - Credits.Width) / 2 - 9, Credits.Location.Y);
                Records.Size = new Size(Records.Width - Records.Width / 35, Records.Height);
                Records.Location = new Point((this.Width - Records.Width) / 2 - 9, Records.Location.Y);
                NewGame.Size = new Size(NewGame.Width - NewGame.Width / 35, NewGame.Height);
                NewGame.Location = new Point((this.Width - NewGame.Width) / 2 - 9, NewGame.Location.Y);
                EnterExpect = false;
            }
            while (!EnterExpect &amp;&amp; Exit.Width &lt;= 450);
        };
        Exit.MouseLeave += async (s, e) =&gt;
        {
            do
            {
                EnterExpect = true;
                await Task.Delay(1);
                Exit.Size = new Size(Exit.Width - Exit.Width / 35, Exit.Height);
                Exit.Location = new Point((this.Width - Exit.Width) / 2 - 9, Exit.Location.Y);
                EnterExpect = false;
            }
            while (!EnterExpect &amp;&amp; Exit.Width &gt;= 350);
        };
    }

  • И пожалуйста не предлагайте таймер. Мне нужна плавная анимация , а с таймером она получается резкой (даже с Двойной Буферизацией) , единственный вариант это async , но как решить этот баг я не пойму. Спасибо кстати всем кто обратил на мой вопрос внимание и потратил свое время ! Очень приятно – John Doe Jan 14 '23 at 10:30
  • 1
    https://ru.stackoverflow.com/a/1265190/373567 – aepot Jan 14 '23 at 11:11
  • Зря писал ответ, от вас ноль реакции. Пришлось ответ удалить, зря время тратил. – aepot Jan 22 '23 at 10:35

0 Answers0