Click here to Skip to main content
12,245,040 members (59,681 online)
Click here to Skip to main content
Add your own
alternative version

Stats

276.2K views
20.5K downloads
99 bookmarked
Posted

GIF Animation Control

, 18 Apr 2004
Rate this:
Please Sign up or sign in to vote.
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

Share

About the Author

A. Riazi
CEO Misbah3Com
Iran (Islamic Republic of) Iran (Islamic Republic of)
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


You may also be interested in...

Comments and Discussions

 
QuestionAccess Violation Pin
zdanman5-Jan-15 13:57
memberzdanman5-Jan-15 13:57 
BugMust Be Modified to Work in VC6 Pin
zdanman4-Jan-15 8:10
memberzdanman4-Jan-15 8:10 
GeneralMy vote of 5 Pin
manoj kumar choubey26-Feb-12 20:48
membermanoj kumar choubey26-Feb-12 20:48 
QuestionWhy won't my gif load? Pin
Michael B Pliam3-Nov-11 19:59
memberMichael B Pliam3-Nov-11 19:59 
GeneralMy vote of 4 Pin
buyong27-Sep-11 20:47
memberbuyong27-Sep-11 20:47 
Questiontransparency - a better solution Pin
Jim Dill27-Jun-11 11:53
memberJim Dill27-Jun-11 11:53 
Generalmemory leaks! Pin
batsword8-Sep-10 17:11
memberbatsword8-Sep-10 17:11 
Generalmemory leaks here. Pin
David_LoveCpp7-Oct-10 16:15
memberDavid_LoveCpp7-Oct-10 16:15 
GeneralRe: memory leaks here. Pin
batsword11-Nov-10 16:05
memberbatsword11-Nov-10 16:05 
Generalvery good! I give two suggestions Pin
huyl200212-Aug-10 4:05
memberhuyl200212-Aug-10 4:05 
GeneralVery Nice. Have threading question. Pin
Michael B Pliam20-Apr-10 10:15
memberMichael B Pliam20-Apr-10 10:15 
QuestionHow can I change the loaded animated-GIF in runtime? Pin
mobi01024-Oct-09 23:30
membermobi01024-Oct-09 23:30 
Questioncreating gif files??? Pin
sandeepkavade23-Nov-08 19:35
membersandeepkavade23-Nov-08 19:35 
GeneralApplyTransparency() doesn't work!!! Pin
www216-Jul-08 20:21
memberwww216-Jul-08 20:21 
GeneralProblem with unicode Pin
vigya shah21-Apr-08 17:36
membervigya shah21-Apr-08 17:36 
QuestionI can't use ApplyTransparency function..;; Pin
Jerlgun9-Apr-08 3:18
memberJerlgun9-Apr-08 3:18 
Generaldoesn't lock the workstation Pin
venkirtz27-Sep-07 22:07
membervenkirtz27-Sep-07 22:07 
GeneralApplyTransparency can't Transparency image Pin
huynhvanliem27-Aug-07 21:12
memberhuynhvanliem27-Aug-07 21:12 
GeneralGif Animation in TaskbakNotifier window Pin
lbispo11-Jul-07 10:30
memberlbispo11-Jul-07 10:30 
GeneralMemory leak in int C_ImageSet::LoadGIF (char * szFileName) Pin
TPN23-May-07 4:55
memberTPN23-May-07 4:55 
GeneralFix for PocketPC: Pin
DVE9-Oct-06 2:08
memberDVE9-Oct-06 2:08 
GeneralRe: Fix for PocketPC: Pin
Houser2-Nov-06 4:36
memberHouser2-Nov-06 4:36 
QuestionCan not compile under UNICODE! Pin
SeanNick19-Jul-06 5:24
memberSeanNick19-Jul-06 5:24 
QuestionLZW decoder doesn't work correctly Pin
Timo Kunze18-May-06 7:00
memberTimo Kunze18-May-06 7:00 
AnswerRe: LZW decoder doesn't work correctly Pin
A. Riazi18-May-06 7:46
memberA. Riazi18-May-06 7:46 

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.

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