Click here to Skip to main content
Licence 
First Posted 2 Jun 2003
Views 209,790
Bookmarked 89 times

GIF Animation Control

By | 18 Apr 2004 | Article
A CStatic derived class for displaying GIF Animations.

GifAnimation.jpg

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.

Static control

Figure 2- Add new static control to dialog resource

Properties

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;

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!

License

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

About the Author

A. Riazi



Iran (Islamic Republic Of) Iran (Islamic Republic Of)

Member

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 Acqusition 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 competetion, my articles are:

You can see list of my articles, by clicking here



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 Pinmembermanoj kumar choubey19:48 26 Feb '12  
QuestionWhy won't my gif load? [modified] PinmemberMichael B Pliam18:59 3 Nov '11  
GeneralMy vote of 4 Pinmemberbuyong19:47 27 Sep '11  
Questiontransparency - a better solution PinmemberJim Dill10:53 27 Jun '11  
Generalmemory leaks! Pinmemberbatsword16:11 8 Sep '10  
Generalmemory leaks here. PinmemberDavid_LoveCpp15:15 7 Oct '10  
GeneralRe: memory leaks here. Pinmemberbatsword15:05 11 Nov '10  
Generalvery good! I give two suggestions Pinmemberhuyl20023:05 12 Aug '10  
GeneralVery Nice. Have threading question. PinmemberMichael B Pliam9:15 20 Apr '10  
QuestionHow can I change the loaded animated-GIF in runtime? Pinmembermobi01022:30 24 Oct '09  
Questioncreating gif files??? Pinmembersandeepkavade18:35 23 Nov '08  
GeneralApplyTransparency() doesn't work!!! Pinmemberwww2119:21 6 Jul '08  
GeneralProblem with unicode Pinmembervigya shah16:36 21 Apr '08  
QuestionI can't use ApplyTransparency function..;; PinmemberJerlgun2:18 9 Apr '08  
Generaldoesn't lock the workstation Pinmembervenkirtz21:07 27 Sep '07  
GeneralApplyTransparency can't Transparency image Pinmemberhuynhvanliem20:12 27 Aug '07  
GeneralGif Animation in TaskbakNotifier window Pinmemberlbispo9:30 11 Jul '07  
GeneralMemory leak in int C_ImageSet::LoadGIF (char * szFileName) PinmemberTPN3:55 23 May '07  
GeneralFix for PocketPC: PinmemberDVE1:08 9 Oct '06  
GeneralRe: Fix for PocketPC: PinmemberHouser3:36 2 Nov '06  
QuestionCan not compile under UNICODE! PinmemberSeanNick4:24 19 Jul '06  
QuestionLZW decoder doesn't work correctly PinmemberTimo Kunze6:00 18 May '06  
AnswerRe: LZW decoder doesn't work correctly PinmemberA. Riazi6:46 18 May '06  
I used WinImage Library for decoding the GIFs. I didn't know that there is any update for it or not. Must refer to WinImage.
 
Best regards,
A. Riazi
GeneralRe: LZW decoder doesn't work correctly PinmemberTimo Kunze7:15 18 May '06  
AnswerRe: LZW decoder doesn't work correctly PinmemberA. Riazi20:58 18 May '06  

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

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120529.1 | Last Updated 19 Apr 2004
Article Copyright 2003 by A. Riazi
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid