我的想法是在窗口/表单增长时为它提供动画。例如,Windows 7 的计算器在显示单位转换器或新功能时,窗口会随着动画的流畅变化而改变大小。问题是我不知道在使用按钮更改大小(例如 200 像素以上)时使用哪个代码。我是初学者,所以我不知道如何使用向我建议的“计时器”。
问问题
811 次
2 回答
0
在此页面上,可以找到整个概念(在 C# 中)。您甚至可以下载已经完成的代码(同样,在 C# 中): http ://www.vcskicks.com/size-transform.php
于 2012-09-08T03:20:00.257 回答
0
这是 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);
}
于 2012-12-06T18:45:05.700 回答