如何用VC实现淡入淡出的动画特技


在程序中加入适当的动画效果能够使程序更加生动活泼,要实现淡入淡出的效果,要如何做?

VC Windows界面开发

卡尔梅恩上尉 12 years, 6 months ago

1.窗口效果的淡入淡出可以用 AnimateWindow

   
  void CAboutDlg::OnClose()
  
{
AnimateWindow(GetSafeHwnd(), 500, AW_BLEND | AW_HIDE); //淡出0.5秒
CDialog::OnClose();
}
int CAboutDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CDialog::OnCreate(lpCreateStruct) == -1)
return -1;
CenterWindow(); //居中
::AnimateWindow(GetSafeHwnd(), 500, AW_BLEND); //淡入0.5秒
return 0;
}

2.图片等的淡入淡出效果可以用 AlphaBlend 或者通过修改alpha通道,来达到淡入淡出效果

   
  void AlphaDisplay(CDC &pDC;, CClientDC &dc;, BLENDFUNCTION& rBlendProps, int width, int heigh, byte nLevel)
  
{
//nLevel是透明度,0表示不显示,255则完全显示
rBlendProps.SourceConstantAlpha = nLevel;
AlphaBlend( dc.m_hDC, 0, 0, width, heigh, pDC.m_hDC, 0, 0,
width, heigh, rBlendProps );
}

UINT tdDisplay(LPVOID lpParam)
{
CAlphaCtrl* AlphaCtrl = (CAlphaCtrl*)lpParam; CClientDC dc(AlphaCtrl);
CDC pDC;
pDC.CreateCompatibleDC(&dc;);
pDC.SelectObject(&AlphaCtrl-;>Bmp); BLENDFUNCTION rBlendProps;
rBlendProps.BlendOp = AC_SRC_OVER;
rBlendProps.BlendFlags = 0;
rBlendProps.AlphaFormat = 0; BITMAP bmInfo;
::GetObject( AlphaCtrl->Bmp.m_hObject, sizeof(BITMAP), &bmInfo; );
INT nWidth, nHeigh;
nWidth = bmInfo.bmWidth;
nHeigh = bmInfo.bmHeight;AlphaCtrl->SetWindowPos(NULL, 0, 0, nWidth, nHeigh, SWP_NOMOVE);
int i = 0;
while(i <= 255)
{
AlphaCtrl->AlphaDisplay(pDC, dc, rBlendProps, nWidth, nHeigh, i);
i += 5;
Sleep(AlphaCtrl->nTimer);
}
AlphaCtrl->bCanPaint = 1; //Make OnPaint Word
AfxEndThread(0);
return 0;
}

gethell answered 12 years, 6 months ago

Your Answer