MyWnd.cpp
上传用户:xsxdsb
上传日期:2009-12-14
资源大小:672k
文件大小:3k
源码类别:

书籍源码

开发平台:

Visual C++

  1. // MyWnd.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "MyScreensaver.h"
  5. #include "MyWnd.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CMyWnd
  13. #define ID_TIMER WM_USER+1000
  14. CMyWnd::CMyWnd()
  15. {
  16. m_prePoint=CPoint(-1,-1);
  17. }
  18. CMyWnd::~CMyWnd()
  19. {
  20. }
  21. BEGIN_MESSAGE_MAP(CMyWnd, CWnd)
  22. //{{AFX_MSG_MAP(CMyWnd)
  23. ON_WM_PAINT()
  24. ON_WM_KEYDOWN()
  25. ON_WM_LBUTTONDOWN()
  26. ON_WM_MBUTTONDOWN()
  27. ON_WM_MOUSEMOVE()
  28. ON_WM_RBUTTONDOWN()
  29. ON_WM_SYSKEYDOWN()
  30. ON_WM_DESTROY()
  31. ON_WM_TIMER()
  32. ON_WM_ACTIVATE()
  33. ON_WM_ACTIVATEAPP()
  34. //}}AFX_MSG_MAP
  35. END_MESSAGE_MAP()
  36. LPCSTR CMyWnd::lpszClassName=NULL;
  37. BOOL CMyWnd::Create()
  38. {
  39. if(lpszClassName==NULL)
  40. {
  41. lpszClassName=AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW,
  42. ::LoadCursor(AfxGetResourceHandle(),MAKEINTRESOURCE(IDC_NOCURSOR)));
  43. //注册类;IDC_NOCURSOR为新建光标的ID,这个光标没有任何图案
  44. }
  45. CRect rect(0, 0, ::GetSystemMetrics(SM_CXSCREEN),
  46. ::GetSystemMetrics(SM_CYSCREEN));
  47. CreateEx(WS_EX_TOPMOST, lpszClassName, _T(""), WS_VISIBLE|WS_POPUP,
  48. rect.left,rect.top,rect.right - rect.left, rect.bottom - rect.top,
  49. GetSafeHwnd(), NULL, NULL); //创建一个全屏窗口
  50. SetTimer(ID_TIMER, 500, NULL);//计时器,ID_TIMER别忘了定义
  51. return TRUE;
  52. }
  53. //为了防止同时运行两个相同的程序,下面两个函数是必需的
  54. void CMyWnd::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
  55. {
  56. CWnd::OnActivate(nState,pWndOther,bMinimized);
  57. if(nState==WA_INACTIVE)
  58. PostMessage(WM_CLOSE);
  59. }
  60. void CMyWnd::OnActivateApp(BOOL bActive, HTASK hTask)
  61. {
  62. CWnd::OnActivateApp(bActive, hTask);
  63. if(!bActive) //is being deactivated
  64. PostMessage(WM_CLOSE);
  65. }
  66. //OnPaint()函数将全屏窗口置为黑色
  67. void CMyWnd::OnPaint()
  68. {
  69. CPaintDC dc(this);
  70. CBrush brush(RGB(0,0,0));
  71. CRect rect;
  72. GetClientRect(rect);
  73. dc.FillRect(&rect,&brush);
  74. }
  75. //由计数器调用DrawBitmap()函数,切换图片;
  76. //注意,下面两个函数中的IDB_BITMAP1, dc.BitBlt(0,0,800,600……以及if(nIndexBit>=5)中的
  77. //有关数据依据你的bmp图片个数、尺寸、位置不同而不同,我是选择了5张
  78. //800x600的bmp图片。注意,ID值是连续的,IDB_BITMAP1最小
  79. void CMyWnd::DrawBitmap(CDC& dc, int nIndexBit)
  80. {
  81. CDC dcmem;
  82. dcmem.CreateCompatibleDC(&dc);
  83. CBitmap m_Bitmap;
  84. m_Bitmap.LoadBitmap(IDB_BITMAP1+nIndexBit);
  85. dcmem.SelectObject(m_Bitmap);
  86. dc.BitBlt(0,0,800,600,&dcmem,0,0,SRCCOPY);
  87. }
  88. void CMyWnd::OnTimer(UINT nIDEvent)
  89. {
  90. CClientDC dc(this);
  91. static nIndexBit=0;
  92. if(nIndexBit>=5)
  93. nIndexBit=0;
  94. DrawBitmap(dc, nIndexBit++);
  95. CWnd::OnTimer(nIDEvent);
  96. }
  97. //响应键盘、鼠标是屏幕保护程序不可缺少的
  98. void CMyWnd::OnKeyDown(UINT nChar,UINT nRepCnt,UINT nFlags)
  99. {
  100. PostMessage(WM_CLOSE);
  101. }
  102. void CMyWnd::OnLButtonDown(UINT nFlags,CPoint point)
  103. {
  104. PostMessage(WM_CLOSE);
  105. }
  106. void CMyWnd::OnMButtonDown(UINT nFlags,CPoint point)
  107. {
  108. PostMessage(WM_CLOSE);
  109. }
  110. void CMyWnd::OnRButtonDown(UINT nFlags,CPoint point)
  111. {
  112. PostMessage(WM_CLOSE);
  113. }
  114. void CMyWnd::OnSysKeyDown(UINT nChar,UINT nRepCnt,UINT nFlags)
  115. {
  116. PostMessage(WM_CLOSE);
  117. }
  118. void CMyWnd::OnMouseMove(UINT nFlags,CPoint point)
  119. {
  120. if(m_prePoint == CPoint(-1,-1))
  121. m_prePoint = point;
  122. else if(m_prePoint!=point)
  123. PostMessage(WM_CLOSE);
  124. }
  125. void CMyWnd::OnDestroy()
  126. {
  127. //删掉计时器
  128. KillTimer(ID_TIMER);
  129. }
  130. void CMyWnd::PostNcDestroy()
  131. {
  132. delete this;
  133. }
  134. /////////////////////////////////////////////////////////////////////////////
  135. // CMyWnd message handlers