Skip to main content
Email Password   helpLost your password?

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

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

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

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
Generalsuggestion SetCursor Pin
guyuewuhua
20:55 9 Sep '09  
GeneralThanks Pin
TClarke
1:58 4 Aug '09  
GeneralImporting gif resources in VS2005 - Invalid File Pin
Elliot Rice
6:52 3 Apr '09  
GeneralThank you! It's a great class! Pin
yiwenjun
11:33 29 Mar '09  
GeneralThanks for this great class!!! Pin
czy0408
15:59 5 Mar '09  
GeneralLicense ? Pin
kosasihkho
20:17 22 Feb '09  
GeneralThanks for this great class!!! Pin
coder_kevin
2:52 12 Feb '09  
GeneralHow to display a bitmap resource using PictureEx Pin
Shup
14:15 12 Jun '08  
GeneralRe: How to display a bitmap resource using PictureEx Pin
Dave Streeter
8:01 12 Mar '09  
GeneralRe: How to display a bitmap resource using PictureEx Pin
Shup
8:24 12 Mar '09  
GeneralUseful source of progress animators gifs Pin
Pandele Florin
0:59 24 Jan '08  
QuestionAnimation Gif as Splash before start.. Pin
shizu looi
17:28 11 Sep '07  
GeneralTransparent background Pin
Shlomo
8:09 21 Jun '07  
QuestionHello Pin
SuF1234
2:07 8 Mar '07  
GeneralNice class, But LNK2001 occurs when use static lib Pin
Jerry.Wang
1:06 30 Jan '07  
GeneralGIF doesn't animate with ShowWindow() Pin
btboja
23:40 31 Jul '06  
GeneralLoad More GIF one time Pin
badboyyp
21:53 25 Jul '06  
GeneralOptimized GIF problem (global palette) Pin
daganesh
0:32 16 Jul '06  
GeneralModified Load() for DLL Resource Pin
HydroDan
9:03 27 Jun '06  
AnswerRe: Modified Load() for DLL Resource Pin
chris175
10:44 27 Jun '06  
GeneralRe: Modified Load() for DLL Resource Pin
Dave Streeter
0:12 13 Mar '09  
Generalhelp,an error occured Pin
lsmart
20:10 16 May '06  
GeneralFit Size error Pin
lemonxinmei330
17:38 7 May '06  
GeneralRe: Fit Size error Pin
guyuewuhua
22:34 24 Aug '09  
GeneralRe: Fit Size error Pin
guyuewuhua
23:19 24 Aug '09  


Last Updated 23 Nov 2001 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009