MyWnd.cpp
上传用户:xsxdsb
上传日期:2009-12-14
资源大小:672k
文件大小:3k
- // MyWnd.cpp : implementation file
- //
- #include "stdafx.h"
- #include "MyScreensaver.h"
- #include "MyWnd.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- /////////////////////////////////////////////////////////////////////////////
- // CMyWnd
- #define ID_TIMER WM_USER+1000
- CMyWnd::CMyWnd()
- {
- m_prePoint=CPoint(-1,-1);
- }
- CMyWnd::~CMyWnd()
- {
- }
- BEGIN_MESSAGE_MAP(CMyWnd, CWnd)
- //{{AFX_MSG_MAP(CMyWnd)
- ON_WM_PAINT()
- ON_WM_KEYDOWN()
- ON_WM_LBUTTONDOWN()
- ON_WM_MBUTTONDOWN()
- ON_WM_MOUSEMOVE()
- ON_WM_RBUTTONDOWN()
- ON_WM_SYSKEYDOWN()
- ON_WM_DESTROY()
- ON_WM_TIMER()
- ON_WM_ACTIVATE()
- ON_WM_ACTIVATEAPP()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- LPCSTR CMyWnd::lpszClassName=NULL;
- BOOL CMyWnd::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, 500, NULL);//计时器,ID_TIMER别忘了定义
- return TRUE;
- }
- //为了防止同时运行两个相同的程序,下面两个函数是必需的
- void CMyWnd::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
- {
- CWnd::OnActivate(nState,pWndOther,bMinimized);
- if(nState==WA_INACTIVE)
- PostMessage(WM_CLOSE);
- }
- void CMyWnd::OnActivateApp(BOOL bActive, HTASK hTask)
- {
- CWnd::OnActivateApp(bActive, hTask);
- if(!bActive) //is being deactivated
- PostMessage(WM_CLOSE);
- }
- //OnPaint()函数将全屏窗口置为黑色
- void CMyWnd::OnPaint()
- {
- CPaintDC dc(this);
- CBrush brush(RGB(0,0,0));
- CRect rect;
- GetClientRect(rect);
- dc.FillRect(&rect,&brush);
- }
- //由计数器调用DrawBitmap()函数,切换图片;
- //注意,下面两个函数中的IDB_BITMAP1, dc.BitBlt(0,0,800,600……以及if(nIndexBit>=5)中的
- //有关数据依据你的bmp图片个数、尺寸、位置不同而不同,我是选择了5张
- //800x600的bmp图片。注意,ID值是连续的,IDB_BITMAP1最小
- void CMyWnd::DrawBitmap(CDC& dc, int nIndexBit)
- {
- CDC dcmem;
- dcmem.CreateCompatibleDC(&dc);
- CBitmap m_Bitmap;
- m_Bitmap.LoadBitmap(IDB_BITMAP1+nIndexBit);
- dcmem.SelectObject(m_Bitmap);
- dc.BitBlt(0,0,800,600,&dcmem,0,0,SRCCOPY);
- }
- void CMyWnd::OnTimer(UINT nIDEvent)
- {
- CClientDC dc(this);
- static nIndexBit=0;
- if(nIndexBit>=5)
- nIndexBit=0;
- DrawBitmap(dc, nIndexBit++);
- CWnd::OnTimer(nIDEvent);
- }
- //响应键盘、鼠标是屏幕保护程序不可缺少的
- void CMyWnd::OnKeyDown(UINT nChar,UINT nRepCnt,UINT nFlags)
- {
- PostMessage(WM_CLOSE);
- }
- void CMyWnd::OnLButtonDown(UINT nFlags,CPoint point)
- {
- PostMessage(WM_CLOSE);
- }
- void CMyWnd::OnMButtonDown(UINT nFlags,CPoint point)
- {
- PostMessage(WM_CLOSE);
- }
- void CMyWnd::OnRButtonDown(UINT nFlags,CPoint point)
- {
- PostMessage(WM_CLOSE);
- }
- void CMyWnd::OnSysKeyDown(UINT nChar,UINT nRepCnt,UINT nFlags)
- {
- PostMessage(WM_CLOSE);
- }
- void CMyWnd::OnMouseMove(UINT nFlags,CPoint point)
- {
- if(m_prePoint == CPoint(-1,-1))
- m_prePoint = point;
- else if(m_prePoint!=point)
- PostMessage(WM_CLOSE);
- }
- void CMyWnd::OnDestroy()
- {
- //删掉计时器
- KillTimer(ID_TIMER);
- }
- void CMyWnd::PostNcDestroy()
- {
- delete this;
- }
- /////////////////////////////////////////////////////////////////////////////
- // CMyWnd message handlers