吵吵   2012-04-09  阅读:2,600

这年头写的烂的程序不敢出来见人,写的程序UI做的烂的更不敢出来见人。大概是从迅雷7开始,所有的公司都似乎开始追寻绚丽的界面了,原先的迅雷界面虽然上不上丑陋,也能算的上中规中矩了,迅雷七以后就是华华丽丽的界面。不管是从美观还是从用户体验来说,都有了质的飞跃。

然而要在windows下实现这些华丽的界面,如华丽的窗口出现,或者是华丽的窗口关闭就比较困难。原本系统给我们的animatewindow终究是功能有限,所以我们只得自行写代码来开发了。

吵吵这次写的是一个渐入渐出的窗口动画类,当窗口出现的时候,从下而上,透明度逐渐减低,即逐渐不透明,二是上升速度逐渐下降,越至顶端越慢。而窗口消失的时候,则是反之了,不说了,贴代码:

class CAlpha
{
	static void CALLBACK TimerProc(HWND hwnd,UINT uMsg,UINT_PTR idEvent,DWORD dwTime);
public:
	CAlpha(void);
	~CAlpha(void);
protected:
	HWND m_hWnd;
public:
	static bool Initialize(HINSTANCE hInstance);
	void Create(HWND hParentWnd);
	void SetWindowAlpha(COLORREF color,BYTE byte,DWORD dword);
	void AlphaWindowShow();
	void AlphaWindowHide(bool bCloseWindow);
	void AlphaPlus();
	int GetAlpha();
public:
	int nAlpha;
	int nAlphaStep;
	float fMoveStep;
	CRect rcParent;
	bool bCloseWindowLast;
	bool bShowOrHide;

};
#include "stdafx.h"
#include "Windows.h"
#include "CAlpha.h"
typedef BOOL (WINAPI *ShowLayer)(HWND,COLORREF,BYTE,DWORD); 

CAlpha *pAlpha;
 CAlpha:: CAlpha(void)
:m_hWnd((HWND)INVALID_HANDLE_VALUE)
{
	pAlpha=(CAlpha *)this;
}

 CAlpha::~CAlpha(void)
{

}
void CAlpha::SetWindowAlpha(COLORREF color,BYTE byte,DWORD dword)
{
		HINSTANCE hInst = LoadLibrary(_T("User32.DLL")); 
		if(hInst)
		{ 

			ShowLayer fun = NULL;
			fun = (ShowLayer)GetProcAddress(hInst, "SetLayeredWindowAttributes");
			if (fun)
			{

					fun(this->m_hWnd, color, byte, dword);
					FreeLibrary(hInst); 

			}
		}
}
void CAlpha::AlphaPlus()
{
	if(pAlpha->bShowOrHide)
	{
		this->nAlpha=this->nAlpha+this->nAlphaStep;
		if(nAlphaStep!=1){
			nAlphaStep--;
		}
	}
	else
	{
		this->nAlpha=this->nAlpha-this->nAlphaStep;
		if(nAlphaStep!=22){
			nAlphaStep++;
		}
	}

}
int CAlpha::GetAlpha()
{
	return this->nAlpha;
}

void CAlpha::AlphaWindowShow()
{
	bShowOrHide=true;
	this->nAlpha=0;
	this->nAlphaStep=22;
	this->SetWindowAlpha(0,this->nAlpha,2);
	::ShowWindow(this->m_hWnd,SW_SHOW);
	MoveWindow(this->m_hWnd,this->rcParent.left,this->rcParent.top+this->rcParent.Height(),rcParent.Width(),rcParent.Height(),false);
	::SetTimer(this-> m_hWnd,999,2,(TIMERPROC)TimerProc);

}
void CAlpha::AlphaWindowHide(bool bCloseWindow)
{
	bShowOrHide=false;
	if(bCloseWindow){
		this->bCloseWindowLast=true;
	}
	this->nAlpha=255;
	this->nAlphaStep=0;
	this->SetWindowAlpha(0,this->nAlpha,255);
	::SetTimer(this-> m_hWnd,999,2,(TIMERPROC)TimerProc);
}
void CAlpha::Create(HWND hParentWnd)
{
	this->m_hWnd=hParentWnd;
	GetWindowRect(hParentWnd,this->rcParent);
	this->fMoveStep=float(this->rcParent.Height()/255.0);
	SetWindowLong(hParentWnd,GWL_EXSTYLE,GetWindowLong(hParentWnd,GWL_EXSTYLE)^0x80000);
	this->bCloseWindowLast=false;

}

void CALLBACK CAlpha::TimerProc(HWND hwnd,UINT uMsg,UINT_PTR idEvent, DWORD dwTime)
{

	pAlpha->AlphaPlus();
	pAlpha->SetWindowAlpha(0,pAlpha->GetAlpha(),2);
	MoveWindow(pAlpha->m_hWnd,pAlpha->rcParent.left,pAlpha->rcParent.top+pAlpha->rcParent.Height()-int(pAlpha->GetAlpha()*pAlpha->fMoveStep),pAlpha->rcParent.Width(),pAlpha->rcParent.Height(),false);
	if(pAlpha->bShowOrHide)
	{

		if(pAlpha->GetAlpha()>=255)
		{
			KillTimer(hwnd,idEvent);
			delete pAlpha;
		}
	}
	else
	{
		if(pAlpha->GetAlpha()bCloseWindowLast)
			{
				PostMessage(pAlpha->m_hWnd,WM_CLOSE,0,0);
			}
			else
			{
				ShowWindow(pAlpha->m_hWnd,SW_HIDE);
			}

		//	CString str;
		//	str.Format(_T("%d"),pAlpha->GetAlpha());
		//	AfxMessageBox(str);
			delete pAlpha;

		}
	}

}

使用方法么,也很简单,如下面的例子:

CAlpha *alpha=new CAlpha;
alpha->Create(this->GetSafeHwnd());
alpha->AlphaWindowShow();

吵吵微信朋友圈,请付款实名加入:

吵吵 吵吵

2条回应:“自写的vc窗口渐入渐出动画的类”

  1. 我看不懂的就是代码了

  2. 很不错“弄代码很头疼的~

发表评论

电子邮件地址不会被公开。 必填项已用*标注