私が持っているアイデアは、ウィンドウ/フォームが大きくなったときにアニメーションを与えることです。たとえば、Windows 7 の電卓は、単位変換や新機能を表示すると、ウィンドウのサイズが滑らかなアニメーションで変化します。問題は、ボタンを使用してサイズを変更するときに使用するコードがわからないことです (たとえば、200 ピクセル以上)。初心者なので教えていただいた「タイマー」の使い方がわかりません。
質問する
811 次
2 に答える
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 に答える
0
このページでは、概念全体 (C#) を見つけることができます。既に作成されたコードをダウンロードすることもできます (これも C# です): http://www.vcskicks.com/size-transform.php
于 2012-09-08T03:20:00.257 に答える