Click here to Skip to main content
15,905,782 members
Articles / Desktop Programming / MFC

GIF Animation Control

Rate me:
Please Sign up or sign in to vote.
4.70/5 (47 votes)
18 Apr 20042 min read 401.6K   25.6K   104   112
A CStatic derived class for displaying GIF Animations.



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.

Static control

Figure 2- Add new static control to dialog resource


Figure 3- Change ID of static control from IDC_STATIC to IDC_GIF_ANIMATION

Add member variable

Figure 4- Add member variable with variable type CStatic

OK. Now, in your dialog header file, change this line:

CStatic m_Animation;


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

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include "GifAnimation.h"
// CAnimatedGifDlg dialog

class CAnimatedGifDlg : public CDialog
// Construction
    CAnimatedGifDlg(CWnd* pParent = NULL);    // standard constructor

// Dialog Data
    CGifAnimation m_Animation;    //changed from CStatic m_Animation;

    // ClassWizard generated virtual function overrides
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
    HICON m_hIcon;

    // Generated message map functions
    virtual BOOL OnInitDialog();
    afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
    afx_msg void OnPaint();
    afx_msg HCURSOR OnQueryDragIcon();

// 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:


Note: To apply transparency, download and use ApplyTransparency() function. Thanks kingdomkao for modifying the code.


Part of this code was taken from WinImage library (generic classes for raster images) from Juan Soulie.



This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Written By
CEO Solaris Electronics LLC
United Arab Emirates United Arab Emirates
I was born in Shiraz, a very beautiful famous city in Iran. I started programming when I was 12 years old with GWBASIC. Since now, I worked with various programming languages from Basic, Foxpro, C/C++, Visual Basic, Pascal to MATLAB and now Visual C++.
I graduated from Iran University of Science & Technology in Communication Eng., and now work as a system programmer for a telecommunication industry.
I wrote several programs and drivers for Synthesizers, Power Amplifiers, GPIB, GPS devices, Radio cards, Data Acquisition cards and so many related devices.
I'm author of several books like Learning C (primary and advanced), Learning Visual Basic, API application for VB, Teach Yourself Object Oriented Programming (OOP) and etc.
I'm winner of January, May, August 2003 and April 2005 best article of month competition, my articles are:

You can see list of my articles, by clicking here

Comments and Discussions

QuestionSome gifs always showing first frame on every frame Pin
KajiChinzo21-Aug-17 23:08
KajiChinzo21-Aug-17 23:08 
QuestionAccess Violation Pin
zdanman5-Jan-15 12:57
zdanman5-Jan-15 12:57 
BugMust Be Modified to Work in VC6 Pin
zdanman4-Jan-15 7:10
zdanman4-Jan-15 7:10 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey26-Feb-12 19:48
professionalManoj Kumar Choubey26-Feb-12 19:48 
QuestionWhy won't my gif load? Pin
Michael B Pliam3-Nov-11 18:59
Michael B Pliam3-Nov-11 18:59 
GeneralMy vote of 4 Pin
buyong27-Sep-11 19:47
buyong27-Sep-11 19:47 
Questiontransparency - a better solution Pin
Jim Dill27-Jun-11 10:53
Jim Dill27-Jun-11 10:53 
Generalmemory leaks! Pin
batsword8-Sep-10 16:11
batsword8-Sep-10 16:11 
Generalmemory leaks here. Pin
David_LoveCpp7-Oct-10 15:15
David_LoveCpp7-Oct-10 15:15 
GeneralRe: memory leaks here. Pin
batsword11-Nov-10 15:05
batsword11-Nov-10 15:05 
Generalvery good! I give two suggestions Pin
huyl200212-Aug-10 3:05
huyl200212-Aug-10 3:05 
GeneralVery Nice. Have threading question. Pin
Michael B Pliam20-Apr-10 9:15
Michael B Pliam20-Apr-10 9:15 
QuestionHow can I change the loaded animated-GIF in runtime? Pin
mobi01024-Oct-09 22:30
mobi01024-Oct-09 22:30 
AnswerRe: How can I change the loaded animated-GIF in runtime? Pin
Member 1258772216-Aug-16 16:42
Member 1258772216-Aug-16 16:42 
Questioncreating gif files??? Pin
sandeepkavade23-Nov-08 18:35
sandeepkavade23-Nov-08 18:35 
GeneralApplyTransparency() doesn't work!!! Pin
www216-Jul-08 19:21
www216-Jul-08 19:21 
GeneralProblem with unicode Pin
vigya shah21-Apr-08 16:36
vigya shah21-Apr-08 16:36 
QuestionI can't use ApplyTransparency function..;; Pin
Jerlgun9-Apr-08 2:18
Jerlgun9-Apr-08 2:18 
Generaldoesn't lock the workstation Pin
venkirtz27-Sep-07 21:07
venkirtz27-Sep-07 21:07 
GeneralApplyTransparency can't Transparency image Pin
huynhvanliem27-Aug-07 20:12
huynhvanliem27-Aug-07 20:12 
GeneralGif Animation in TaskbakNotifier window Pin
lbispo11-Jul-07 9:30
professionallbispo11-Jul-07 9:30 
GeneralMemory leak in int C_ImageSet::LoadGIF (char * szFileName) Pin
TPN23-May-07 3:55
TPN23-May-07 3:55 
GeneralFix for PocketPC: Pin
DVE9-Oct-06 1:08
DVE9-Oct-06 1:08 
GeneralRe: Fix for PocketPC: Pin
Houser2-Nov-06 3:36
Houser2-Nov-06 3:36 
QuestionCan not compile under UNICODE! Pin
SeanNick19-Jul-06 4:24
SeanNick19-Jul-06 4:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.