声明
//{{AFX_VIRTUAL(CMyWnd)
protected:
virtual void PostNcDestroy();
//}}AFX_VIRTUAL
15. 接下来就是要处理键盘、鼠标消息以及,WM_PAINT消息,由于ClassWizard没有我们新定义的类,我们必须手动加入映射代码,把下面的代码加入到MyScreenSaver.h文件的CMyScreenWnd类定义中
//{{AFX_MSG(CMyScreenWnd)
afx_msg void OnPaint();
afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnMButtonDown(UINT nFlags, CPoint point);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
afx_msg void OnSysKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
afx_msg void OnDestroy();
afx_msg void OnTimer(UINT nIDEvent);
afx_msg void OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized);
afx_msg void OnActivateApp(BOOL bActive, HTASK hTask);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
16. 在MyScreenSaver.cpp文件中加入映射代码(可以加在该文件中的任何地方,只要放在所有的函数外就好了 BEGIN_MESSAGE_MAP(CMyScreenWnd, CWnd)
//{{AFX_MSG_MAP
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_KEYDOWN()
ON_WM_LBUTTONDOWN()
ON_WM_RBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_WM_DESTROY()
ON_WM_TIMER()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
17. 在MyScreenWnd.cpp文件中加入这些函数的实现代码
//OnPaint()函数将全屏窗口置成黑色
void CMyScreenWnd::OnPaint()
{
CPaintDC dc(this);
CBrush brush(RGB(0,0,0));
CRect rect;
GetClientRect(rect);
dc.FillRect(&rect,&brush);
}
void CMyScreenWnd::OnTimer(UINT nIDEvent)
{
CClientDC dc(this);
int n=2; //你有几张
图片就把n换成几,因为我
//做的屏保中只有两张
图片因而n=2
static int m_nIndex=0;
m_nIndex%=n-1;
DrawBitmap(dc,m_nIndex++);
CWnd::OnTimer(nIDEvent);
}
void CMyScreenWnd::OnKeyDown(UINT nChar,UINT nRepCnt,UINT nFlags)
{
PostMessage(WM_CLOSE);
}
void CMyScreenWnd::OnLButtonDown(UINT nFlags,CPoint point)
{
PostMessage(WM_CLOSE);
}
void CMyScreenWnd::OnRButtonDown(UINT nFlags,CPoint point)
{
PostMessage(WM_CLOSE);
}
void CMyScreenWnd::OnSysKeyDown(UINT nChar,UINT nRepCnt,UINT nFlags)
{
PostMessage(WM_CLOSE);
}
void CMyScreenWnd::OnMouseMove(UINT nFlags,CPoint point)
{
if (m_Point==CPoint(-1,-1))
m_Point=point;
else if (m_Point!=point)
PostMessage(WM_CLOSE);
}
void CMyScreenWnd::OnDestroy()
{
KillTimer(ID_TIMER);
}
void CMyScreenWnd::PostNcDestroy()
{
delete this;
}
//为了防止同时运行两个相同的
程序,下面两个函数是必需的
//如果你不理解这段代码,不要管他们,只要把他们拷贝到你
//的工程中就行了
void CMyScreenWnd::OnActivate(UINT nState,CWnd* pWndOther,BOOL bMinimized)
{
CWnd::OnActivate(nState,pWndOther,bMinimized);
if (nState==WA_INACTIVE)
PostMessage(WM_CLOSE);
}
void CMyScreenWnd::OnActivateApp(BOOL bActive,HTASK hTask)
{
CWnd::OnActivateApp(bActive,hTask);
if (!bActive) //is being deactivated
PostMessage(WM_CLOSE);
}
18. 马上就要完工了,修改CMyScreenSaverApp的InitInstance()函数如下:(注意须在MyScreenSaver.h中加入语句:#include "MyScreenWnd.h")
BOOL CMyScreenSaverApp::InitInstance()
{
AfxEnableControlContainer();
// Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you shou