Animated Icon on Titlebar of a window

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

Introduction:

This simple class is for people who want to make their application more attractive using icon animation in the title bar of frame window.

Technique:

All the consecutive images used for animation are stored in a single bitmap resource. The animation could have been achieved also by storing each image as separate icon in the resource, but that would have made managing them more difficult. Putting the images as a list of image also makes it easier to keep track of the sequence of animation.

To start with the class loads the bitmap resource in a CImageList using CimageList::Create function. Then whenever ShowNextImage is called the class loads the image using CimageList::ExtractIcon and displays it in title bar by sending WM_SETICON to the frame window.

Usage:

1. Add AnimateIcon.cpp and AnimateIcon.h files in your project.

2. Create a bitmap resource and put all the images (16 pixels x 16 pixels) in the desired sequence.

3. Add #include "AnimateIcon.h" at the beginning of your mainframe.h file.

4. Add the following protected members to your mainframe class


    CAnimateIcon m_animIcon;
    UINT m_timerID;
5. Add the following code in OnCreate member of your frame class

    m_animIcon.SetImageList(IDB_ANIM_IMGLIST,4,RGB(0,0,0));
    m_timerID = this->SetTimer(99,500,NULL);
SetImageList takes 3 parameters pass ID of your bitmap resource created in step 2 as first parameter. The second parameter is the no of (16x16) images in your bitmap, which is 4 in our case. The last parameter is the RGB value of color you want to make transparent. Second line of code sets a new timer. Here I have made it to fire every 500 ms. you can change it to suit your animation speed.

6. Create a OnDestroy()handler for your main frame class and add the following code. This is very important because if you do not kill the timer created you will lose system resources.


    CFrameWnd::OnDestroy();
    if(m_timerID != 0)
        KillTimer(m_timerID);

7. Add WM_TIMERhandler to your frame class and add the following code


    m_animIcon.ShowNextImage();

Compile and run the code. Now you should be able to see your window icon being animated. Note that just adding the above code also would bring up default MFC icon for a moment. If you want to avoid this change the IDR_MAINFRAME icon in the resource.

Note:

The sample project is built using Visual C++ 4.1, and has been tested on Visual C++ versions 4.1 and 6.0. If you directly try to run the .exe in the sample project it may ask for VC 4.1 runtime DLLs.

This version also fixes resources problem reported by few users.

Download demo project - 46 KB

Download source - 2 KB

Date Updated: December 2, 1998



Comments

  • Thanks

    Posted by Legacy on 07/09/2002 12:00am

    Originally posted by: Code Guy

    Rajesh,

    Thanks for the handy code. It's nice to be able to add a little flair to otherwise drab loooking applications with something like this icon animator.

    -Code Guy

    Reply
  • How to display a text with owner fonts

    Posted by Legacy on 12/10/2001 12:00am

    Originally posted by: Bui

    I don't know How to display a text with owner fonts in
    title bar . Please help me , Thank you very much

    Reply
  • Ref: Request for tech help on VB++

    Posted by Legacy on 06/13/2001 12:00am

    Originally posted by: Shuvro

    I want to Manipulate a Image File which is Scanned from the newspaper Page.
    Image size is in Pixel: Width:-2440
    Height:-3381

    I have developed a Browser with Vb6 using Picture Box Control.
    I have kept all options like Zooming in , Zooming Out and Cropping a selected Portion.
    Sometimes it works and Sometimes it Fails.When it fails it shows an error Message.
    When I make the picture bigger than its Original size and also try to Crop a Selected Part it shows error.Error message is : AutoRedraw cannot Create Image.Err Number is 480
    But I have to work with big Image.

    Reply
  • Quick way to make imagelists...

    Posted by Legacy on 03/15/2000 12:00am

    Originally posted by: Mark Stevens

    A real easy way to manage the bitmaps in an imagelist is to use a toolbar resource. It is actually stored as two separate resources, a regular bitmap resource and a toolbar resource. the nice thing is you can move buttons around and edit/resizing is a piece of cake.

    In your code you just use the toolbar's resource ID as a bitmap and ignore the other part.

    Reply
  • You can use this with CDialog

    Posted by Legacy on 03/09/2000 12:00am

    Originally posted by: Sergey

    Just change the original line:
    HICON hPrevIcon = (HICON)AfxGetMainWnd()->SendMessage(WM_SETICON,TRUE,(LPARAM)hIcon);

    to
    HICON hPrevIcon = (HICON)AfxGetMainWnd()->SetIcon(hIcon, FALSE);

    Thanks

    Reply
  • Nice Code It is But.................

    Posted by Legacy on 07/06/1999 12:00am

    Originally posted by: Nauman Ahmad Khan

    Very simple code is written to animate an icon on the title bar but one problem is there that how to keep all images in the same bitmap resource is not more clear. One way in my mind is that you create an image which holds the small images ( they should be explicitly created ) and include this image as bitmap resource. Thanks and congratulations on such a simple to use code 
    

    Reply
  • How about stop the timer.

    Posted by Legacy on 03/08/1999 12:00am

    Originally posted by: Yuan Songjun

    I have setup an application as what you have shown.
    It works. There is a ever-changing icon in the application's title bar.
    While I want to stop it from the CView derived class, so I call it by ((CFramWnd*) GetParentFrame()->OnDestroy();
    It does not work: the icon is still changina as ever.
    How to stop it from outside?
    Thanks.

    Reply
  • Nice Code

    Posted by Legacy on 02/23/1999 12:00am

    Originally posted by: Thomas Gathercoal

    Fun idea. Might I suggest adding menu items to allow the user to disable
    the animation and/or change the frame rate.

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date