GIF Animation Control






4.70/5 (43 votes)
Jun 3, 2003
2 min read

413852

25652
A CStatic derived class for displaying GIF Animations.
- Download source files - 13 Kb
- Download source files with supporting transparent background - 11 Kb by kingdomkao
- Download demo project - 200 Kb
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:
// AnimatedGifDlg.h : header file // #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 // _MSC_VER > 1000 #include "GifAnimation.h" ///////////////////////////////////////////////////////////////////////////// // CAnimatedGifDlg dialog class CAnimatedGifDlg : public CDialog { // Construction public: CAnimatedGifDlg(CWnd* pParent = NULL); // standard constructor // Dialog Data //{{AFX_DATA(CAnimatedGifDlg) enum { IDD = IDD_ANIMATEDGIF_DIALOG }; CGifAnimation m_Animation; //changed from CStatic m_Animation; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAnimatedGifDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: HICON m_hIcon; // Generated message map functions //{{AFX_MSG(CAnimatedGifDlg) virtual BOOL OnInitDialog(); afx_msg void OnSysCommand(UINT nID, LPARAM lParam); afx_msg void OnPaint(); afx_msg HCURSOR OnQueryDragIcon(); //}}AFX_MSG DECLARE_MESSAGE_MAP() }; //{{AFX_INSERT_LOCATION}} // Microsoft Visual C++ will insert additional // declarations immediately before the previous line. #endif // !defined(AFX_ANIMATEDGIFDLG_H__ // 6003B39F_D033_4867_8D34_FAD74F42A098__INCLUDED_)
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!