Click here to Skip to main content
15,868,016 members
Articles / Multimedia / GDI+

T-Rex animation

Rate me:
Please Sign up or sign in to vote.
4.71/5 (16 votes)
23 Apr 2014CPOL2 min read 35.2K   1.1K   18   16
Self contained EXE animation

Introduction

This is a self contained EXE composited animation, written in procedural code with the low level SDK programming FLAT API, to get rid of any external dependencies.

Background

While I could have used the extended layered style, i made the choice of using the desktop composition feature, introduced in Windows Vista.

When desktop composition is enabled, individual windows no longer draw directly to the screen or primary display device as they did in previous versions of Windows.
Instead, their drawing is redirected to off-screen surfaces in video memory, which are then rendered into a desktop image and presented on the display.

In Windows 8, Desktop Window Manager (DWM) is always ON and cannot be disabled by end users and apps. As in Windows 7, DWM is used to compose the desktop.

In Windows Vista and Windows 7, desktop composition is enabled only with the AERO Glass Theme.

The GDIPLUS flat API is used to display each frame of the PNG image in loop mode, and the T-Rex roar is using a standard WAV audio remixed to match exactly the animation duration.
Everything is embedded as resource, to produce only one single final EXE file.

Using the code

The source code written in C++ is self explanatory, and designed to work in 64-bit mode (VS2010+).

Load_TREX is the main function in charge of loading the embedded resources

C++
LONG_PTR Load_TREX () {
    LONG_PTR nRet = 0;
    HRSRC hResource = FindResource(gP.instance, MAKEINTRESOURCE(TREX), RT_RCDATA);
    if (hResource) {
       long imageSize = SizeofResource(gP.instance, hResource);
       if (imageSize) {
          LPVOID pResourceData = LockResource(LoadResource(gP.instance, hResource));
          if (pResourceData) {
             HGLOBAL MemBuffer = GlobalAlloc(GMEM_MOVEABLE | GMEM_NODISCARD, imageSize);
             if (MemBuffer) {
                LPVOID pBuffer = GlobalLock(MemBuffer);
                if (pBuffer) {
                   CopyMemory(pBuffer, pResourceData, imageSize);
                   LPSTREAM pStream = NULL;
                   if (CreateStreamOnHGlobal(MemBuffer, FALSE, &pStream) == 0) {
                      LONG_PTR hImage = 0;
                      if (GdipCreateBitmapFromStream(pStream, hImage) == 0) {
                         nRet = hImage;
                      }
                      //IUnknown_Release(pStream);
                      ReleaseObject((PFUNKNOWN*) pStream);
                   }
                }
                GlobalUnlock(MemBuffer);
             }
             GlobalFree(MemBuffer);
          }
       }
    }
    return nRet;
}  

Points of Interest

The animation itself is performed from a child thread, rather than using a timer (but the timer code is also provided). The main advantage of using a thread is when dealing with multi-core, and when you have to use animation while the main process is running.

There is a much more complex demo based on the same DWM composited concept, here:
http://www.codeproject.com/Articles/705243/HUD-window-bit-DWM-composition

How to use the animation

Use drag and drop to move T-Rex everywhere on the desktop.
To close the animation, press the escape key or the ALT-F4 key combination.

And for those that are unable to compile the code, here is the link to download the EXE:

download T_Rex_EXE.zip

Here is another animation (written in PowerBASIC), but it is based on the same concept
download Cheetah.zip

If you like them i have a few more :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer zapsolution
France France
I am a low level SDK programmer, focusing mainly on graphic imaging and multimedia applications.
I am using several languages, including C, C++, PowerBASIC, WinDev.
I wrote also a few demos in C#, but i never used DotNET in real code production.

Comments and Discussions

 
QuestionHow to use it on win32 mode? Pin
YDLU29-Aug-16 6:01
YDLU29-Aug-16 6:01 
AnswerRe: How to use it on win32 mode? Pin
YDLU30-Aug-16 5:43
YDLU30-Aug-16 5:43 
QuestionWow Pin
N. Henrik Lauridsen31-May-15 4:13
N. Henrik Lauridsen31-May-15 4:13 
QuestionT-Rex is not a valid win32 application Pin
myyr23-Apr-14 8:13
myyr23-Apr-14 8:13 
AnswerRe: T-Rex is not a valid win32 application Pin
zapsolution23-Apr-14 8:32
zapsolution23-Apr-14 8:32 
GeneralRe: T-Rex is not a valid win32 application Pin
myyr15-May-14 11:28
myyr15-May-14 11:28 
QuestionNew animations Pin
zapsolution23-Apr-14 7:47
zapsolution23-Apr-14 7:47 
QuestionHow to start? Pin
Ronny9322-Apr-14 21:41
Ronny9322-Apr-14 21:41 
AnswerRe: How to start? Pin
zapsolution22-Apr-14 22:16
zapsolution22-Apr-14 22:16 
GeneralRe: How to start? Pin
Ronny9322-Apr-14 23:10
Ronny9322-Apr-14 23:10 
GeneralCompiled EXE Pin
zapsolution23-Apr-14 2:10
zapsolution23-Apr-14 2:10 
GeneralRe: Compiled EXE Pin
Ronny9323-Apr-14 2:17
Ronny9323-Apr-14 2:17 
QuestionAt last! Pin
megaadam22-Apr-14 3:44
professionalmegaadam22-Apr-14 3:44 
AnswerRe: At last! Pin
zapsolution22-Apr-14 22:19
zapsolution22-Apr-14 22:19 
Who is Bob ? Smile | :)
GeneralRe: At last! Pin
megaadam23-Apr-14 0:45
professionalmegaadam23-Apr-14 0:45 
Questionfiles Pin
Nagrom22-Apr-14 1:39
Nagrom22-Apr-14 1:39 

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.