CXWndAnimate:CAnimateCtrl 的替代方案






4.54/5 (6投票s)
2000年7月28日

59442

1165
一个动画控件,使用位图图像列表而不是 AVI 文件
引言
最近我需要开发一个处理指示器,向用户显示我的应用程序正在进行一些繁重的计算或从数据库中检索大量数据。
我的第一想法是使用 CAnimateCtrl。我必须承认我尝试过了,但没有成功。我无法制作带有透明背景的 AVI 文件(也许我太懒了)。所以我决定使用图像列表开发我自己的动画控件。
对于这个控件的每一个实例,我都有一个线程来不断更新动画。
如何使用
- 在你的
CWinApp
派生类的InitInstance
中调用静态方法RegisterWnd
- 在对话框中:你必须放置一个自定义控件并赋予它类名 "X_WND_ANIMATE"。接下来,你在对话框类中声明一个
CXWndAnimate
成员变量。在OnInitDialog
中,你必须对控件进行子类化。m_ctrlAnimation.SubclassDlgItem(IDC_ANIMATION, this);
- 在状态栏中,你必须在面板上创建它。还要记住在状态栏调整大小的时候更正控件的位置。
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); // TODO: Add your message handler code here 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); } }
- 要选择动画,你必须构造包含动画帧的位图文件(就像一个动画 GIF)。要设置要使用的图像,请执行以下操作
CImageList imgList; imgList.Create(IDB_BITMAP, 14, 0, RGB(255, 255, 255)); m_ctrlAnimation.SetImageList(imgList.Detach());
在这种情况下,白色将是透明颜色。每个帧的宽度为 14 像素。如果你的动画需要完全重绘(重绘背景),请确保调用方法
SetClearBackground
。
示例项目演示了在对话框中使用该控件。
其他相关方法
BOOL IsRunning()
:指示动画是否正在播放BOOL StartAnimation(short nStart, short nEnd = -1)
:开始播放动画。如果 nEnd 为 -1,动画将播放到结束BOOL StopAnimation(BOOL bHideWindow = TRUE)
:停止动画,如果 bHideWindow 等于 TRUE,则隐藏窗口