Introduction
Graphics has an important role to transfer valuable data to user. For this reason, software developers try to add graphic elements to their software. Bitmap, JPEG, GIF and other graphics file formats are used to store images and show it in a proper manner. Also, it's possible to show some basic animation to improve user interface. AVI, Flash and Animation GIF are popular formats to show animation. There are controls for showing AVI and also Flash. For AVI, you can use CAnimateCtrl
, and for Flash, use Flash ActiveX. But what about GIF Animation? Unfortunately, there is no standard control for this file format.
In this article, I will show you how to employ a CStatic
derived class to show Animation GIF.
How to use
First add GifAnimation.h, GifAnimation.cpp, winimage.h and winimage.cpp files to your project. Then in your dialog resource editor, add new Static control (figure 2 shows it). Rename this static control ID to your desired ID, for example IDC_GIF_ANIMATION
(like figure 3). Run class wizard (Ctrl+W) and add new member variable with variable type CStatic
(you must choose Control in Category combo box). Name it as m_Animation
. Figure 4 shows you how to declare a new CStatic
control.
Figure 2- Add new static control to dialog resource
Figure 3- Change ID of static control from IDC_STATIC
to IDC_GIF_ANIMATION
Figure 4- Add member variable with variable type CStatic
OK. Now, in your dialog header file, change this line:
CStatic m_Animation;
to
CGifAnimation m_Animation;
Don't forget to include GifAnimation.h header file on top of your dialog class definition. Now your dialog definition looks like this:
#if !defined(AFX_ANIMATEDGIFDLG_H__6003B39F
_D033_4867_8D34_FAD74F42A098__INCLUDED_)
#define AFX_ANIMATEDGIFDLG_H__6003B39F_
D033_4867_8D34_FAD74F42A098__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif
#include "GifAnimation.h"
class CAnimatedGifDlg : public CDialog
{
public:
CAnimatedGifDlg(CWnd* pParent = NULL);
enum { IDD = IDD_ANIMATEDGIF_DIALOG };
CGifAnimation m_Animation;
protected:
virtual void DoDataExchange(CDataExchange* pDX);
protected:
HICON m_hIcon;
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
DECLARE_MESSAGE_MAP()
};
#endif
CGifAnimation class
CGifAnimation
class has simple member functions. Use these functions for loading a GIF Animation file and displaying it. Also, use other functions to stop, rewind and play animation.
int LoadAnimatedGif (LPTSTR FileName); |
Use this member function for loading animation to memory. Return 0 if any error occurred. |
void Play(); |
Play animation (Start/Resume animation loop). |
void Stop(); |
Stop animation. |
void Rewind(); |
Rewind animation (Reset animation loop to its initial values). |
BOOL IsPlaying(); |
Return TRUE if control is playing animation. |
For example:
m_Animation.LoadAnimatedGif("c:\\blahblah.gif");
m_Animation.Play();
Note: To apply transparency, download AnimationGifEx_src.zip and use ApplyTransparency()
function. Thanks kingdomkao for modifying the code.
Credit
Part of this code was taken from WinImage library (generic classes for raster images) from Juan Soulie.
Enjoy!