|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionA friend of mine recently asked for advice related to showing animations on a C# WinForm. The question reminded me of some code I wrote back in 2001 to create a managed wrapper for the Windows common animation control. This article outlines that control and demonstrates a possible use of the control with a small sample application. Using the codeUsing the control in an application is no different than using any other WinForm control. Essentially, you should follow these steps:
Once an instance of the animation control exists on the form, you may adjust the properties using the Visual Studio property designer. The properties exposed by the control include:
The control also exposes a few methods for altering the behavior at runtime. Here is a list of those methods:
That's pretty much it. The control is simple to use. Inside the controlThe control gains the ability to play animations by subclassing the ' protected override CreateParams CreateParams
{
get
{
// Create the parameters for the animation control.
CreateParams parms = base.CreateParams;
parms.ClassName = "SysAnimate32";
// Apply the appropriate animation control styles.
if (AutoCenter)
parms.Style |= NativeMethods.ACS_CENTER;
if (AutoPlay)
parms.Style |= NativeMethods.ACS_AUTOPLAY;
if (Transparent)
parms.Style |= NativeMethods.ACS_TRANSPARENT;
if (UseTimer)
parms.Style |= NativeMethods.ACS_TIMER;
return parms;
} // End get
} // End CreateParms()
In order to use a Windows common control class, you should start by initializing the common control library. That step is performed using the following code: protected override void CreateHandle()
{
// Should we make sure the common control library is initialized?
if (!RecreatingHandle)
{
NativeMethods.INITCOMMONCONTROLSEX iccex =
new NativeMethods.INITCOMMONCONTROLSEX(
NativeMethods.ICC_ANIMATE_CLASS
);
NativeMethods.InitCommonControls(iccex);
} // End if we should init the common control library.
base.CreateHandle();
} // End CreateHandle()
The standard Windows animations are loaded dynamically from the shell32.dll library. I used this approach because I didn't want to embed standard AVI files in my application resources, and I figured most other people would feel the same way. The code to load the library resources is shown here: private void OpenHelper(
CGAVIFileType aviFileType
)
{
// If the control hasn't been created then simply exit.
if (!IsHandleCreated)
return;
// Close any currently open animation.
Close();
// Should we load the shell library?
if (m_hShellModule == 0)
m_hShellModule = NativeMethods.LoadLibraryEx(
"shell32.dll",
0,
2
);
// Did the animation open?
m_isOpen = (NativeMethods.SendMessage(
new HandleRef(this, Handle),
NativeMethods.ACM_OPEN,
m_hShellModule,
(int)aviFileType
) != 0);
// Should we automtically play the animation?
if (AutoPlay)
Play();
} // End OpenHelper()
The private void OpenHelper(
string fileName
)
{
// If the control hasn't been created then simply exit.
if (!IsHandleCreated)
return;
// Close any currently open animation.
Close();
// Did the animation open?
m_isOpen = (NativeMethods.SendMessage(
new HandleRef(this, Handle),
NativeMethods.ACM_OPEN,
0,
fileName
) != 0);
// Should we automtically play the animation?
if (AutoPlay)
Play();
} // End OpenHelper()
ConclusionThat's pretty much all that is interesting about the internals of the control. I have used the code for a number of years without problems but there is always room for improvement - right? I cleaned things up somewhat before I published the project (be glad you don't have to look at my C# code from 2001). As always, I am interested in any feedback or suggestions. Have fun! :o) HistoryI wrote the original code in 2001. I have updated things sporadically since then but I haven't always maintained versions and change history. I decided to bump the version number up to 2.0 for this release. Version 2.1 --> I changed the names of the enumerated animation types and the default size of the control in order to differentiate my code from other animation controls.
|
||||||||||||||||||||||