Download demo project - 24 Kb
Introduction
Recently I had to develop an processing indicator to show to the user that my application is
doing some heavy calculations or retrieving lots of data from a database.
My first thought was about using the CAnimateCtrl. I must confess i tried, but without success.
I couldn't make an AVI file with transparent background (maybe I'm too lazy).
So I decided to develop my own Animate Control using bitmaps through an image list.
For each control of this I have a thread to constantly update the animation.
How to use
- Call the static method
RegisterWnd
on the InitInstance
of your CWinApp
derived class
- In dialogs: you have to place a custom control a give it the class name
"X_WND_ANIMATE". Next you declare an
CXWndAnimate
member variable on your
dialog class. On the OnInitDialog
you have to subclass the control:
m_ctrlAnimation.SubclassDlgItem(IDC_ANIMATION, this);
- In Status Bar you have to create it on a pane.
Also remember to correct the control's position when the StatusBar is resized.
BOOL CMyStatusBar::CreateAnimation(int iIndex)
{
CFrameWnd* pFrame = reinterpret_cast<CFRAMEWND*> (GetParent());
pFrame->RecalcLayout();
m_pwndBmpAnimate = new CWndBmpAnimate;
CRect rcItem;
GetItemRect(iIndex, &rcItem);
rcItem.right = rcItem.left + CX_ANIMATION;
rcItem.InflateRect(-1, -1);
BOOL bReturn = m_pwndBmpAnimate->Create(WND_BMP_ANIMATE,
NULL, WS_CHILD | WS_VISIBLE, rcItem, this, 0);
ASSERT(bReturn);
return bReturn;
}
void CMyStatusBar::OnSize(UINT nType, int cx, int cy)
{
CStatusBar::OnSize(nType, cx, cy);
if (m_pwndBmpAnimate)
{
CRect rcItem;
GetItemRect(CommandToIndex(ID_ANIMATED), &rcItem);
rcItem.right = rcItem.left + CX_ANIMATION;
rcItem.InflateRect(-1, -1);
m_pwndBmpAnimate->SetWindowPos(NULL, rcItem.left,
rcItem.top, 0, 0, SWP_NOZORDER | SWP_NOSIZE);
}
}
- To select the animation you must construct
bitmaps files with the frames for the animation (like an animated gif). To set the
images that are going to be used, do this:
CImageList imgList;
imgList.Create(IDB_BITMAP, 14, 0, RGB(255, 255, 255));
m_ctrlAnimation.SetImageList(imgList.Detach());
In this case the white color will be the transparent color. Each frame has 14 pixels width.
If your animation need a total redrawing (redraw the background) make sure to call the method
SetClearBackground
.
The sample project demonstrates the use of the control in a dialog box.
Other relevants methods:
BOOL IsRunning()
: indicates if the animation is playing
BOOL StartAnimation(short nStart, short nEnd = -1)
: Starts to play an animation. If nEnd if -1 the animation will play until it's end
BOOL StopAnimation(BOOL bHideWindow = TRUE)
: Stops the
animation and hide the window if bHideWindow is equal TRUE