【问题标题】:Add smooth animation when window grow窗口增长时添加平滑动画
【发布时间】:2012-08-27 06:09:38
【问题描述】:

我的想法是在窗口/窗体增长时为其提供动画。例如,Windows 7 的计算器在显示单位转换器或新功能时,窗口会随着动画的流畅变化而改变大小。 问题是我不知道在使用按钮更改大小(例如 200 像素以上)时使用哪个代码。 我是初学者,所以我不知道如何使用向我建议的“计时器”。

【问题讨论】:

    标签: vb.net visual-studio-2010 .net-4.0


    【解决方案1】:

    在这个页面上,可以找到整个概念(在 C# 中)。您甚至可以下载已经完成的代码(同样,在 C# 中): http://www.vcskicks.com/size-transform.php

    【讨论】:

      【解决方案2】:

      这是一个简单的 C++/CLI 实现。我使用了 C++/CLI,所以我可以使用语法。您可以轻松地将其转换为 C#。

      AnimateWindowResize.h

      #pragma once
      
      using namespace System;
      using namespace System::IO; // Stream
      using namespace System::Drawing; // Bitmap
      using namespace System::Reflection; // Assembly
      using namespace System::Collections::Generic;
      using namespace System::Text;
      using namespace System::Windows::Forms;
      
      public ref class AnimateWindowResize
      {
      private:
          static const int DEFAULT_STEP_AMOUNT = 15;
      
          bool m_startExpanding;
          bool m_animationEnabled;
          bool m_isHorizontal;
          Form^ m_windowsForm;
          Button^ m_initiateButton;
          int m_minSize;
          int m_maxSize;
          Timer^ m_timer;
          int m_stepRate;
          String^ m_initButtonText;
          String^ m_finalButtonText;
          String^ m_initButtonEmbeddedImageName;
          String^ m_finalButtonEmbeddedImageName;
          Bitmap^ m_initBitmap;
          Bitmap^ m_finalBitmap;
      
      
      public: 
          AnimateWindowResize(Form^ windowsForm, Button^ initiateButton, bool isHorizontal, int minSize, int maxSize, bool isWindowExpanded);
      
          // init[final]EmbeddedImageName are images added under Linker -> Input -> Embed Managed Resource File.
          AnimateWindowResize(Form^ windowsForm, Button^ initiateButton, String^ initEmbeddedImageName, String^ finalEmbeddedImageName, String^ initButtonText, String^ finalButtonText, bool isHorizontal, int minSize, int maxSize, bool isWindowExpanded);
      
      protected:
      
          void Init(Form^ windowsForm, Button^ initiateButton, String^ initEmbeddedImageName, String^ finalEmbeddedImageName, String^ initButtonText, String^ finalButtonText, bool isHorizontal, int minSize, int maxSize, bool isWindowExpanded)
          {
              m_initBitmap = nullptr;
              m_finalBitmap = nullptr;
              m_windowsForm = windowsForm;
              m_initiateButton = initiateButton;
              if (initButtonText != nullptr)
                  m_initiateButton->Text = initButtonText;
      
              Stream^ stm = nullptr;
              if(initEmbeddedImageName != nullptr)
                  stm = Assembly::GetEntryAssembly()->GetManifestResourceStream (initEmbeddedImageName);
              if (stm != nullptr)
              {
                  m_initBitmap = gcnew Bitmap(stm);
                  m_initiateButton->Image = m_initBitmap;
              }
      
              stm = nullptr;
              if(finalEmbeddedImageName != nullptr)
                  stm = Assembly::GetEntryAssembly()->GetManifestResourceStream (finalEmbeddedImageName);
              if (stm != nullptr)
                  m_finalBitmap = gcnew Bitmap(stm);
      
              m_minSize = minSize;
              m_maxSize = maxSize;
              m_startExpanding = isWindowExpanded;
              m_isHorizontal = isHorizontal;
              m_initButtonText = initButtonText;
              m_finalButtonText = finalButtonText;
              m_initButtonEmbeddedImageName = initEmbeddedImageName;
              m_finalButtonEmbeddedImageName = finalEmbeddedImageName;
      
              m_animationEnabled = true;
              m_stepRate = DEFAULT_STEP_AMOUNT;
      
              // Calculate the step rate
              m_stepRate = (int)((maxSize - minSize) / 10.0);
              if (m_stepRate <= 0)
                  m_stepRate = DEFAULT_STEP_AMOUNT;
      
              m_timer = gcnew Timer();
              m_timer->Interval = 1;
              m_timer->Enabled = false;
              m_timer->Tick += gcnew EventHandler(this, &AnimateWindowResize::m_timer_Tick);
          }
      
          void m_timer_Tick(Object^ sender, EventArgs^ e)
          {
              if (m_startExpanding)
              {
                  if (m_isHorizontal)
                  {
                      if (m_windowsForm->Width >= m_maxSize)
                      {
                          m_timer->Enabled = false;
                      }
                      else
                      {
                          int tempWidth = m_windowsForm->Width + m_stepRate;
      
                          if (tempWidth >= m_maxSize)
                          {
                              m_windowsForm->Width = m_maxSize;
                              m_timer->Enabled = false;
                              return;
                          }
      
                          m_windowsForm->Width += m_stepRate;
                      }
                  }
                  else // Vertical
                  {
                      if (m_windowsForm->Height >= m_maxSize)
                      {
                          m_timer->Enabled = false;
                      }
                      else
                      {
                          int tempHeight = m_windowsForm->Height + m_stepRate;
      
                          if (tempHeight >= m_maxSize)
                          {
                              m_windowsForm->Height = m_maxSize;
                              m_timer->Enabled = false;
                              return;
                          }
      
                          m_windowsForm->Height += m_stepRate;
                      }
                  }
              }
              else // Collaspe
              {
                  if (m_isHorizontal)
                  {
                      if (m_windowsForm->Width <= m_minSize)
                      {
                          m_timer->Enabled = false;
                      }
                      else
                      {
                          int tempWidth = m_windowsForm->Width - m_stepRate;
      
                          if (tempWidth <= m_minSize)
                          {
                              m_windowsForm->Width = m_minSize;
                              m_timer->Enabled = false;
                              return;
                          }
      
                          m_windowsForm->Width -= m_stepRate;
                      }
                  }
                  else // Vertical
                  {
                      if (m_windowsForm->Height <= m_minSize)
                      {
                          m_timer->Enabled = false;
                      }
                      else
                      {
                          int tempHeight = m_windowsForm->Height - m_stepRate;
      
                          if (tempHeight <= m_minSize)
                          {
                              m_windowsForm->Height = m_minSize;
                              m_timer->Enabled = false;
                              return;
                          }
      
                          m_windowsForm->Height -= m_stepRate;
                      }
                  }
              }
          }
      
      public: 
          void StartAnimation()
          {
              m_timer->Enabled = m_animationEnabled;
              m_startExpanding = !m_startExpanding;
      
              // Change the text of the initiate button.
              if (m_startExpanding)
              {
                  m_initiateButton->Text = m_finalButtonText == nullptr ? "Less" : m_finalButtonText;
                  if (m_finalBitmap != nullptr)
                      m_initiateButton->Image = m_finalBitmap;
              }
              else
              {
                  m_initiateButton->Text = m_initButtonText == nullptr ? "More" : m_initButtonText;
                  if (m_initBitmap != nullptr)
                      m_initiateButton->Image = m_initBitmap;
              }
      
      
              if (!m_animationEnabled)
                  ResizeWithNoAnimation();
          }
      
      public: 
          property bool AnimationEnabled
          {
              bool get() { return m_animationEnabled; }
              void set(bool value) 
              { 
                  m_animationEnabled = value;
              }
          }
      
      private: 
          void ResizeWithNoAnimation()
          {
              // Just resize the window.
              if (m_startExpanding)
              {
                  if (m_isHorizontal)
                      m_windowsForm->Width = m_maxSize;
                  else
                      m_windowsForm->Height = m_maxSize;
              }
              else
              {
                  if (m_isHorizontal)
                      m_windowsForm->Width = m_minSize;
                  else
                      m_windowsForm->Height = m_minSize;
              }
          }
      };
      

      AnimateWindowResize.cpp

      #include "AnimateWindowResize.h"
      
      AnimateWindowResize::AnimateWindowResize(Form^ windowsForm, Button^ initiateButton, bool isHorizontal, int minSize, int maxSize, bool isWindowExpanded)
      {
          Init(windowsForm, initiateButton, nullptr, nullptr, nullptr, nullptr, isHorizontal, minSize, maxSize, isWindowExpanded);
      }
      
      // init[final]EmbeddedImageName are images added under Linker -> Input -> Embed Managed Resource File.
      AnimateWindowResize::AnimateWindowResize(Form^ windowsForm, Button^ initiateButton, String^ initEmbeddedImageName, String^ finalEmbeddedImageName, String^ initButtonText, String^ finalButtonText, bool isHorizontal, int minSize, int maxSize, bool isWindowExpanded)
      {
          Init(windowsForm, initiateButton, initEmbeddedImageName, finalEmbeddedImageName, initButtonText, finalButtonText, isHorizontal, minSize, maxSize, isWindowExpanded);
      }
      

      【讨论】:

        猜你喜欢
        • 2014-07-09
        • 2012-03-05
        • 1970-01-01
        • 2017-03-12
        • 2021-10-19
        • 2014-05-29
        • 1970-01-01
        • 2017-11-23
        • 1970-01-01
        相关资源
        最近更新 更多