Click here to Skip to main content
11,415,164 members (64,914 online)
Click here to Skip to main content

Add GIF-animation to your MFC and ATL projects with the help of CPictureEx and CPictureExWnd

, 23 Nov 2001
Rate this:
Please Sign up or sign in to vote.
A class for displaying animated GIFs and more. MFC and ATL versions available!

Sample Image

Introduction

Class CPictureEx was written for an MFC-project t

hat required support for banners in JPEG and GIF formats. Static banners weren't hard to display using the OleLoadPicture function and the IPicture interface, but dealing with animated GIFs was a whole different story.

Having rummaged through numerous Internet-links, I discovered that there's only one free option available - a COM-object by George Tersaakov on CodeGuru. Unfortunately, it had problems with displaying some of my test GIFs. Of course, I could buy a third-party library, but in that case, I would pay for an extra functionality (which I didn't actually need). I decided to give it a try and write my own class. The basic idea was to split a GIF into separate frames and display the frames with the familiar combination of OleLoadPicture and IPicture. After thoroughly reading through specifications of GIF87a and GIF89a, I wrote the class that I bring to your attention. Note that CPictureEx can display not only GIFs (including animated GIFs) but also JPEG, BMP, WMF, ICO and CUR (that is, everything that OleLoadPicture knows of). Later on, I wrote an ATL-version of the class.

How you use the MFC-version (CPictureEx)

Add a static text or a Picture control to your dialog (group box will do the trick as well); change the ID of that control to something like IDC_MYPICTURE; use the ClassWizard to associate a member variable (for example, m_Picture) with the control added, Category - Control, Variable type - CStatic; in your dialog's header file, replace the variable type from CStatic to CPictureEx (don't forget to #include "PictureEx.h" and add PictureEx.h and PictureEx.cpp to your project); in OnInitDialog (or anywhere you fancy), add these lines:

if (m_Picture.Load(_T("mypicture.gif")))
    m_Picture.Draw();

Sit back and enjoy the animation Smile | :)

You can also treat CPicture as a standard CStatic, and manually create it (you'll have to, if your host window is not a dialog) by calling CPictureEx::Create(), and then CPictureEx::Load and CPictureEx::Draw.

How you use the ATL-version (CPictureExWnd)

To use the ATL-version (CPictureExWnd), follow the same steps, but instead of using ClassWizard, manually add a variable of type CPictureExWnd in your class and add the following code to your WM_INITDIALOG handler function:

HWND hWnd = GetDlgItem(IDC_MYPIC);
if (hWnd) m_wndBanner.SubclassWindow(hWnd);

After that, you can call CPictureExWnd::Load() and CPictureExWnd::Draw(). Of course, you can also call CPictureExWnd::Create directly - CPictureExWnd is just another window with some extra functionality in its window procedure.

Interface functions

  • BOOL Load(...) - loads a GIF and prepares an object for drawing;
  • BOOL Draw() - draws the picture or continues animation;
  • void Stop() - stops animation;
  • void UnLoad() - stops animation and releases all resources;
  • void SetBkColor(COLORREF) - sets the fill color for transparent areas;
  • COLORREF GetBkColor() - gets the current fill color;
  • BOOL IsGIF() - TRUE if the current picture is a GIF;
  • BOOL IsAnimatedGIF() - TRUE if the current picture is an animated GIF;
  • BOOL IsPlaying() - TRUE if an animation is being shown for the current picture;
  • SIZE GetSize() - returns the picture's dimensions;
  • int GetFrameCount() - returns the number of frames in the current picture;
  • BOOL GetPaintRect(RECT *lpRect) - returns the current painting rectangle;
  • BOOL SetPaintRect(const RECT *lpRect) - sets the current painting rectangle;

CPictureEx[Wnd]::Load is available in three versions:

BOOL Load(LPCTSTR szFileName);

This version loads a picture from the file szFileName. The function's return type indicates the success of the loading.

BOOL Load(HGLOBAL hGlobal, DWORD dwSize);

This Load gets a handle to the global memory block, allocated with GlobalAlloc and GMEM_MOVEABLE flag. The function does not free the memory, so don't forget to GlobalFree it. The return value indicates the success of the loading.

BOOL Load(LPCTSTR szResourceName,LPCTSTR szResourceType);

The function gets a name for the resource with a picture and a name for the type of that resource. For example:

m_Picture.Load(MAKEINTRESOURCE(IDR_MYPIC),_T("GIFTYPE"));

After loading a picture, display it with CPictureEx[Wnd]::Draw() function. If the picture is an animated GIF, the function will spawn a background thread to perform the animation; if it's a still picture, it will be displayed right away with OleLoadPicture/IPicture. You can stop the spawned thread anytime with the CPictureEx[Wnd]::Stop() function. If you want to not only stop the animation but to free all its resources, use CPictureEx[Wnd]::UnLoad() (CPictureEx[Wnd]::Load() calls UnLoad() automatically).

By default, the picture's background is filled with COLOR_3DFACE (the background color of dialog windows). If you need to change the picture's background, call CPictureEx[Wnd]::SetBkColor(COLORREF) after calling CPictureEx[Wnd]::Load().

Version history

  • 1.0 (7 Aug 2001) - initial release;
  • 1.1 (6 Sept 2001) - ATL version of the class;
  • 1.2 (31 Oct 2001) - various bugfixes:
    • Fixed a problem with loading GIFs from resources in MFC-version of the class for multi-module apps. Thanks to Ruben Avila-Carretero for finding this out.
    • Got rid of waitable timer in ThreadAnimation(), now CPictureEx[Wnd] works in Win95 too. Thanks to Alex Egiazarov and Wayne King for the idea.
    • Fixed a visual glitch when using SetBkColor. Thanks to Kwangjin Lee for finding this out.
  • 1.3 (18 Nov 2001) - a bugfix and new features:
    • Fixed a DC leak. One DC leaked per each UnLoad() (forgot to put a ReleaseDC() in the end of CPictureExWnd::PrepareDC() function).
    • Now it is possible to set a clipping rectangle using CPictureEx[Wnd]::SetPaintRect(const LPRECT) function. The LPRECT parameter tells the class what portion of a picture it should display. If the clipping rect is not set, the whole picture is shown. Thanks to Fabrice Rodriguez for the idea.
    • Added support for Stop/Draw. Now you can Stop() an animated GIF, then Draw() it again, it will continue animation from the frame it was stopped on. You can also know if a GIF is currently playing, with the help of IsPlaying() function.
    • Got rid of math.h and made m_bExitThread volatile. Thanks to Piotr Sawicki for the suggestion.

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

Oleg Bykov
Web Developer
Russian Federation Russian Federation
No Biography provided

Comments and Discussions

 
Questionlicense? Pin
TheCoderGuy9-Feb-15 23:10
memberTheCoderGuy9-Feb-15 23:10 
QuestionLicense terms Pin
Member 371256722-Jan-15 12:37
memberMember 371256722-Jan-15 12:37 
GeneralMy vote of 5 Pin
Xjkstar20-Jul-14 22:47
memberXjkstar20-Jul-14 22:47 
QuestionMy vote of 5 Pin
Dave Calkins14-Nov-13 5:11
memberDave Calkins14-Nov-13 5:11 
Question'CStatic' : base class undefined in vs2008 Pin
Member 1003924912-Aug-13 21:52
memberMember 1003924912-Aug-13 21:52 
GeneralMy vote of 5 Pin
SemiEleven21-Jun-13 0:58
memberSemiEleven21-Jun-13 0:58 
GeneralMy vote of 5 Pin
stern_h13-May-13 6:55
memberstern_h13-May-13 6:55 
QuestionNot animating in VS2010 Pin
Shahzad Khan29-Jan-13 14:55
memberShahzad Khan29-Jan-13 14:55 
AnswerRe: Not animating in VS2010 Pin
Shahzad Khan30-Jan-13 5:01
memberShahzad Khan30-Jan-13 5:01 
GeneralRe: Not animating in VS2010 Pin
Member 113062666-Feb-15 3:39
memberMember 113062666-Feb-15 3:39 
QuestionGIF Resizing Pin
shetkarabhijeet10-Jan-13 21:04
membershetkarabhijeet10-Jan-13 21:04 
Questionthe source link had broken now Pin
Baesky2-Jan-13 21:18
memberBaesky2-Jan-13 21:18 
QuestionHow do I display a RawData image? Pin
Erix Chou11-Nov-12 17:51
memberErix Chou11-Nov-12 17:51 
QuestionError LNK2001 Pin
Member 951351614-Oct-12 19:35
memberMember 951351614-Oct-12 19:35 
QuestionIn VS2010 not animation Pin
hu__yong24-Aug-12 1:18
memberhu__yong24-Aug-12 1:18 
Questioncrash under Microsoft application verifer Pin
supersu3014-Aug-12 21:00
membersupersu3014-Aug-12 21:00 
QuestionLoad .PNG file also Pin
Jean Bruno28-Jul-12 4:33
memberJean Bruno28-Jul-12 4:33 
QuestionHow to render CPictureEx by using Gdiplus Pin
bibhucodeproject25-Jun-12 22:36
memberbibhucodeproject25-Jun-12 22:36 
GeneralMy vote of 4 Pin
fengyhack27-May-12 5:31
memberfengyhack27-May-12 5:31 
GeneralMy vote of 1 Pin
tyqrw22-Feb-12 12:16
membertyqrw22-Feb-12 12:16 
Questionwant to use it in CView but... Pin
yadavrahul7-Feb-12 20:00
memberyadavrahul7-Feb-12 20:00 
GeneralMy vote of 5 Pin
Adi Shavit27-Nov-11 0:18
memberAdi Shavit27-Nov-11 0:18 
QuestionLicense? Pin
joerg_reichelt16-Nov-11 18:28
memberjoerg_reichelt16-Nov-11 18:28 
QuestionThanks, this is just what I needed! Pin
Member 771925631-Aug-11 5:34
memberMember 771925631-Aug-11 5:34 
GeneralVS2008 Pin
Pandele Florin14-Sep-10 8:07
memberPandele Florin14-Sep-10 8:07 
GeneralRe: VS2008 Pin
badguy200214-Sep-10 21:27
memberbadguy200214-Sep-10 21:27 
QuestionGreate job, and does anybody know how to contact the author for license problem Pin
badguy200213-Sep-10 17:42
memberbadguy200213-Sep-10 17:42 
Generalgood job Pin
batsword8-Sep-10 17:31
memberbatsword8-Sep-10 17:31 
GeneralPlay GIF once and stop Pin
vijayvinoth16-Jul-10 22:48
membervijayvinoth16-Jul-10 22:48 
Questionhow to unload and remove the jpeg before WM_PAINT is generated Pin
kivanc_10-Jun-10 21:03
memberkivanc_10-Jun-10 21:03 
AnswerRe: how to unload and remove the jpeg before WM_PAINT is generated Pin
kivanc_15-Jun-10 3:23
memberkivanc_15-Jun-10 3:23 
GeneralImporting GIF as a resource in Visual C++ 6.0 Pin
vijayvinoth14-May-10 0:28
membervijayvinoth14-May-10 0:28 
I have read your article " Add GIF-animation to your MFC and ATL projects with the help of CPictureEx and CPictureExWnd " and I am using
in my MFC application.I am developing one GUI for my application.In that GUI i need to use GIF files.But it should be loaded from executable/resource not from the file location.
Since I am new to Visual C++ I don't know how to add/import the "gif" files in resourse.I am using visual C++ 6.0 versionwhen i give right click and try to add the resource in the resourse view it is not showing "GIF" in the import
menu.All other options are there.
Kindly help me.Thanks in advance.
GeneralCImageList and CPictureEx Pin
Hoitabuam25-Jan-10 7:50
memberHoitabuam25-Jan-10 7:50 
Generalsuggestion SetCursor Pin
guyuewuhua9-Sep-09 20:55
memberguyuewuhua9-Sep-09 20:55 
GeneralThanks Pin
TClarke4-Aug-09 1:58
memberTClarke4-Aug-09 1:58 
GeneralImporting gif resources in VS2005 - Invalid File Pin
Elliot Rice3-Apr-09 6:52
memberElliot Rice3-Apr-09 6:52 
GeneralThank you! It's a great class! Pin
yiwenjun29-Mar-09 11:33
memberyiwenjun29-Mar-09 11:33 
GeneralThanks for this great class!!! Pin
czy04085-Mar-09 15:59
memberczy04085-Mar-09 15:59 
QuestionLicense ? Pin
kosasihkho22-Feb-09 20:17
memberkosasihkho22-Feb-09 20:17 
AnswerRe: License ? Pin
phoen1x09322-May-10 5:28
memberphoen1x09322-May-10 5:28 
GeneralRe: License ? Pin
badguy20027-Sep-10 0:39
memberbadguy20027-Sep-10 0:39 
GeneralThanks for this great class!!! Pin
coder_kevin12-Feb-09 2:52
membercoder_kevin12-Feb-09 2:52 
QuestionHow to display a bitmap resource using PictureEx Pin
Shup12-Jun-08 14:15
memberShup12-Jun-08 14:15 
AnswerRe: How to display a bitmap resource using PictureEx Pin
Dave Streeter12-Mar-09 8:01
memberDave Streeter12-Mar-09 8:01 
GeneralRe: How to display a bitmap resource using PictureEx Pin
Shup12-Mar-09 8:24
memberShup12-Mar-09 8:24 
GeneralUseful source of progress animators gifs [modified] Pin
Pandele Florin24-Jan-08 0:59
memberPandele Florin24-Jan-08 0:59 
GeneralRe: More Pin
dankomil24-May-10 2:38
memberdankomil24-May-10 2:38 
QuestionAnimation Gif as Splash before start.. Pin
shizu looi11-Sep-07 17:28
membershizu looi11-Sep-07 17:28 
GeneralTransparent background Pin
Shlomo21-Jun-07 8:09
memberShlomo21-Jun-07 8:09 
QuestionHello Pin
SuF12348-Mar-07 2:07
memberSuF12348-Mar-07 2:07 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150427.4 | Last Updated 24 Nov 2001
Article Copyright 2001 by Oleg Bykov
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid