Skip to content

Animated Icon

A dialog demo that displays an animated cursor or icon loaded from a resource or file.

Published Mar 27, 1999

Shows how to animate an icon inside a plain Win32 dialog. The key move is loading the .ani as a cursor (LoadCursor with IMAGE_ICON semantics) and handing it to a static control via STM_SETIMAGE - Windows then plays the animation frames automatically. The commented variants show the same for an AniIcon resource and for loading straight from a file with LoadImage. Use it when you want a live animated badge in a dialog without any timer code.

/////////////////////////////////////////////////////////////////////

BOOL CALLBACK MainDialog(HWND xHandle, UINT xMessage, WPARAM wParam, LPARAM lParam)
{
    //--------------------------------------------------------------------------

    static HWND Picture_hWnd;

    //--------------------------------------------------------------------------

    if(xMessage == WM_INITDIALOG) // Received A Initilize Dialog Message
    {
        MainDialog_hWnd = xHandle;

        SendMessage(MainDialog_hWnd, (UINT)WM_SETTEXT, (WPARAM)0, (LPARAM)TitleCaption);
        SendMessage(MainDialog_hWnd, WM_SETICON, TRUE, (LPARAM)LoadIcon(MainInstance, MAKEINTRESOURCE(100)) );

        Picture_hWnd   = GetDlgItem(MainDialog_hWnd, 2000);

        //------ Load From Resource "AniCursor"
        HCURSOR CursorHandle = LoadCursor(MainInstance,MAKEINTRESOURCE(500));
        SendMessage(Picture_hWnd,(UINT)STM_SETIMAGE, (WPARAM)IMAGE_ICON, (LPARAM)CursorHandle);

/*
        //------ Load From Resource "AniIcon"
        HICON IconHandle = LoadIcon(MainInstance,MAKEINTRESOURCE(500));
        SendMessage(Picture_hWnd,(UINT)STM_SETIMAGE, (WPARAM)IMAGE_ICON, (LPARAM)IconHandle );
*/
        /*
        //------ Load From File
        HANDLE Handle = LoadImage(0,"C:\\Test.Ani",IMAGE_ICON, 0, 0,LR_LOADFROMFILE | LR_CREATEDIBSECTION);
        SendMessage(Picture_hWnd,(UINT)STM_SETIMAGE, (WPARAM)IMAGE_ICON, (LPARAM)Handle );
        */

        CenterWindow(MainDialog_hWnd);

        return 0;
    }

    //--------------------------------------------------------------------------

    if(xMessage == WM_COMMAND) // Received A Message
    {
        if(wParam == 100) //- Okay (Dialog Command)
        {
            return 0;
        }

        if(wParam == 200) //- Exit (Dialog Command)
        {
            EndDialog(MainDialog_hWnd,0);
            DestroyWindow(MainDialog_hWnd);
            return 0;
        }

        return 0;
    }

    //--------------------------------------------------------------------------

    if(xMessage == WM_SYSCOMMAND) // Received A System Menu Message
    {
        if(LOWORD(wParam) == 1000) //- Version (SysMenu Command)
        {
            char VersionText[128];
            sprintf(VersionText,"%s, Version %s",TitleCaption,FileVersion);
            MessageBox(MainDialog_hWnd, VersionText, "Application Version", MB_TASKMODAL);
            return 0;
        }

        if(LOWORD(wParam) == 1001) //- www.NetWorkDLS.com (SysMenu Command)
        {
            ShellExecute(0,"Open","Http://www.NetWorkDLS.com",NULL,NULL,SW_SHOWNORMAL);
            return 0;
        }

        return 0;
    }

    //--------------------------------------------------------------------------

    if(xMessage == WM_CLOSE)  // Received Close Message
    {
        EndDialog(MainDialog_hWnd,0);
        DestroyWindow(MainDialog_hWnd);
        return 0;
    }

    //--------------------------------------------------------------------------

    if(xMessage == WM_DESTROY) // Received Destroy Message
    {
//        PostQuitMessage(0);
        return 0;
    }

    //--------------------------------------------------------------------------

    return 0;
}
//////////////////////////////////////////////////////////////////////
An unhandled error has occurred. Reload 🗙