MFC 对话框淡入淡出效果

来源:互联网 发布:魔法王座翅膀9上10数据 编辑:程序博客网 时间:2024/06/16 01:43
example 1 :
set up with function AnimateWindow() .
::this method can't set up one CDialog which is transparent
1.在头文件StdAfx.h中,添加: 
#undef WINVER //取消原有版本定义,重新定义版本 
#define WINVER 0x5000 //为了使AnimateWindow函数可用 
2.实现函数: 
AnimateWindow(GetSafeHwnd(), 1000, AW_BLEND); //淡入窗体1秒
AnimateWindow(GetSafeHwnd(), 1000, AW_BLEND | AW_HIDE); //淡出窗体1秒 
/*
example 2 :
set up with function ::SetLayeredWindowAttributes() */
//SmoothDlgDlg.h
HMODULEm_hUserDll;
//SmoothDlgDlg.CPP
//这个函数通过调用SetLayeredWindowAttributes API 函数来设定窗体的透明效果
BOOL CSmoothDlgDlg::SetTransparent(HWND hWnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags)
{
BOOL bRet = TRUE;
typedef BOOL (WINAPI* lpfnSetTransparent)(HWND hWnd, COLORREF crKey, BYTE bAlpha, DWORD dwFlags);
 
//检查User32动态连接库是否已经装入
if(m_hUserDll)
{
 lpfnSetTransparentpFnSetTransparent = NULL;
 pFnSetTransparent=(lpfnSetTransparent)GetProcAddress(m_hUserDll,"SetLayeredWindowAttributes");
 if(pFnSetTransparent)
  bRet = pFnSetTransparent(hWnd, crKey, bAlpha, dwFlags);
 else
  bRet = FALSE;
}
returnbRet;
}
voidCSmoothDlgDlg::OnClose()
{
//循环增加窗口的透明度
for(int nPercent = 100; nPercent >= 0 ; nPercent --)
 SetTransparent(m_hWnd, 0, 255 * nPercent / 100, LWA_ALPHA);
 
//释放User32动态连接库
if(m_hUserDll)
 ::FreeLibrary(m_hUserDll);
 
CDialog::OnClose();}
 
另需在窗口初始化函数中添加如下代码:
BOOLCSmoothDlgDlg::OnInitDialog()
{
CDialog::OnInitDialog();
//装载User32动态连接库
m_hUserDll=::LoadLibrary(_T("USER32.dll"));
//设置窗口样式为WS_EX_LAYERED(层叠窗口样式)
::SetWindowLong(m_hWnd, GWL_EXSTYLE, ::GetWindowLong(m_hWnd,GWL_EXSTYLE) | WS_EX_LAYERED);
SetTransparent(m_hWnd, 0, 255 * 100 / 100, LWA_ALPHA);
//......此处略去了自动生成的代码若干
return TRUE;//return TRUE unless you set the focus to acontrol
}
原创粉丝点击