BOOL CMyScreenWnd::Create()
{
if (lpszClassName==NULL)
{
lpszClassName=AfxRegisterWndClass(CS_HREDRAW CS_VREDRAW,
::LoadCursor(AfxGetResourceHandle(),MAKEINTRESOURCE(IDC_NOCURSOR)));
//注册类,IDC_NOCURSOR为新建光标的ID(需要用户自己绘制)
//这个光标没有任何图案
}
CRect rect(0,0,::GetSystemMetrics(SM_CXSCREEN),
::GetSystemMetrics(SM_CYSCREEN)); //得到显示屏的长度和宽度
CreateEx(WS_EX_TOPMOST,lpszClassName,_T(""),WS_VISIBLE WS_POPUP,
rect.left,rect.top,rect.right-rect.left,rect.bottom-rect.top,
GetSafeHwnd(),NULL,NULL); //创建一个全屏窗口
SetTimer(ID_TIMER,2000,NULL); //设置定时器,2秒换一幅图
return TRUE;
}
void CMyScreenWnd::DrawBitmap(CDC& dc,int nIndex)
{
CDC dcMem;
dcMem.CreateCompatibleDC(&dc);
CBitmap m_Bitmap;
m_Bitmap.LoadBitmap(IDB_BITMAP1+nIndex);
dcMem.SelectObject(m_Bitmap);
//假如你的图片大小是1024×768,请把下面的800,600分别
//替换为1024*768
dc.BitBlt(0,0,800,600,&dcMem,0,0,SRCCOPY);
}
14. 加入虚函数PostNcDestroy()的声明//{{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()
//{{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 should remove from the following
// the specific initialization routines you do not need.
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
CMyScreenWnd* pWnd=new CMyScreenWnd;
pWnd->Create();
m_pMainWnd=pWnd;
return TRUE;
}
新闻热点
疑难解答