TransparentWnd.cpp
上传用户:gaonas
上传日期:2007-01-06
资源大小:53k
文件大小:6k
源码类别:

行业应用

开发平台:

Visual C++

  1. //********************************************************************************
  2. //* TransparentWindow.CPP
  3. //*
  4. //* A transparent window class.
  5. //*
  6. //* Based on the idea of Jason Wylie
  7. //*
  8. //* (C) 1998 by Franz Polzer
  9. //*
  10. //* Visit me at: stud1.tuwien.ac.at/~e9225140
  11. //* Write to me: e9225140@student.tuwien.ac.at
  12. //********************************************************************************
  13. /*
  14. modified by wenyy 闻怡洋
  15. add timer switcher to deal with current event
  16. visit me at : http://vchelp.163.net
  17. write to me : wyy_cq@21cn.com
  18. */
  19. #include "stdafx.h"
  20. #include "TransparentWnd.h"
  21. #include <assert.h>
  22. #include "ScrGenius.h"
  23. #include "ScrGeniusDlg.h"
  24. #ifdef _DEBUG
  25. #define new DEBUG_NEW
  26. #undef THIS_FILE
  27. static char THIS_FILE[] = __FILE__;
  28. #endif
  29. #define TIMER_SET 1
  30. #define TIMER_WALK 2
  31. #define TIMER_IDLE 3
  32. #define TIMER_RUN 4
  33. #define TIMER_HIDE 5
  34. #define TIMER_SHOW 6
  35. #define TIMER_WAITSHOW 7
  36. //********************************************************************************
  37. //* Constructor
  38. //********************************************************************************
  39. CTransparentWnd::CTransparentWnd()
  40. {
  41. m_iAniSeq=0;
  42. m_fontLogo.CreateFont(13, 0, 0, 0, FW_NORMAL, 0, 0,0,0,0,0,0,0, "宋体");
  43. }
  44. //********************************************************************************
  45. //* Destructor
  46. //********************************************************************************
  47. CTransparentWnd::~CTransparentWnd()
  48. {
  49. }
  50. BEGIN_MESSAGE_MAP(CTransparentWnd, CWnd)
  51. //{{AFX_MSG_MAP(CTransparentWnd)
  52. ON_WM_PAINT()
  53. ON_WM_ERASEBKGND()
  54. ON_WM_LBUTTONDOWN()
  55. ON_WM_CREATE()
  56. ON_WM_TIMER()
  57. ON_WM_DESTROY()
  58. ON_WM_RBUTTONDOWN()
  59. //}}AFX_MSG_MAP
  60. END_MESSAGE_MAP()
  61. //********************************************************************************
  62. //* CreateTransparent()
  63. //*
  64. //* Creates the main application window transparent
  65. //********************************************************************************
  66. void CTransparentWnd::CreateTransparent(LPCTSTR pTitle, RECT &rect)
  67. {
  68. CreateEx( 0,
  69. AfxRegisterWndClass(0,AfxGetApp()->LoadStandardCursor(IDC_ARROW)),
  70. pTitle,
  71. WS_POPUP | WS_SYSMENU,
  72. rect,
  73. NULL,
  74. NULL,
  75. NULL );
  76. Reset();
  77. // m_bmpDraw.LoadBitmap(MaskID);
  78. // SetupRegion(GetWindowDC(), MaskID);
  79. }
  80. //********************************************************************************
  81. //* SetupRegion()
  82. //*
  83. //* Set the Window Region for transparancy outside the mask region
  84. //********************************************************************************
  85. void CTransparentWnd::SetupRegion(CDC *pDC)
  86. {
  87. CDC memDC;
  88. CBitmap &cBitmap=m_bmpDraw;
  89. CBitmap* pOldMemBmp = NULL;
  90. COLORREF col,colMask;
  91. CRect cRect;
  92. int x, y;
  93. CRgn wndRgn, rgnTemp;
  94. GetWindowRect(&cRect);
  95. CPoint ptOrg=cRect.TopLeft();
  96. BITMAP bmInfo;
  97. cBitmap.GetObject(sizeof(bmInfo),&bmInfo);
  98. CRect rcNewWnd=CRect(ptOrg,CSize(bmInfo.bmWidth,bmInfo.bmHeight));
  99. memDC.CreateCompatibleDC(pDC);
  100. pOldMemBmp = memDC.SelectObject(&cBitmap);
  101. colMask=memDC.GetPixel(0,0);
  102. wndRgn.CreateRectRgn(0, 0, rcNewWnd.Width(), rcNewWnd.Height());
  103. for(x=0; x<=rcNewWnd.Width(); x++)
  104. {
  105. for(y=0; y<=rcNewWnd.Height(); y++)
  106. {
  107. col = memDC.GetPixel(x, y);
  108. if(col == colMask)
  109. {
  110. rgnTemp.CreateRectRgn(x, y, x+1, y+1);
  111. wndRgn.CombineRgn(&wndRgn, &rgnTemp, RGN_XOR);
  112. rgnTemp.DeleteObject();
  113. }
  114. }
  115. }
  116. if (pOldMemBmp) memDC.SelectObject(pOldMemBmp);
  117. SetWindowRgn((HRGN)wndRgn, TRUE);
  118. MoveWindow(rcNewWnd);
  119. }
  120. void CTransparentWnd::Reset(void)
  121. {
  122. char szBmp[20];
  123. sprintf(szBmp,"VCHELP");
  124. m_bmpDraw.DeleteObject();
  125. m_bmpDraw.LoadBitmap(szBmp);
  126. CWindowDC dc(this);
  127. SetupRegion(&dc);
  128. Invalidate();
  129. }
  130. void CTransparentWnd::DoSet(void)
  131. {
  132. int iSW=GetSystemMetrics(SM_CXFULLSCREEN);
  133. int iSH=GetSystemMetrics(SM_CYFULLSCREEN);
  134. KillTimer(1);
  135. SetTimer(2,1000,NULL);
  136. }
  137. void CTransparentWnd::DoWalk(void)
  138. {
  139. CRect rcW;
  140. GetWindowRect(rcW);
  141. if(rcW.left<200) xcounter=5;
  142. if(rcW.top<200) ycounter=5;
  143. if(rcW.left>600) xcounter=-5;
  144. if(rcW.top>400) ycounter=-5;
  145. CPoint ptOffset(xcounter,ycounter);
  146.   rcW+=ptOffset;
  147. MoveWindow(rcW);
  148. char szBmp[20];
  149. sprintf(szBmp,"WALK%d",m_iAniSeq%5+1);
  150. m_iLastDirection=1;
  151. m_bmpDraw.DeleteObject();
  152. m_bmpDraw.LoadBitmap(szBmp);
  153. CWindowDC dc(this);
  154. SetupRegion(&dc);
  155. Invalidate();
  156. }
  157. //********************************************************************************
  158. //* CTransparentWnd message handlers
  159. //********************************************************************************
  160. void CTransparentWnd::OnLButtonDown(UINT nFlags, CPoint point) 
  161. {
  162. CWnd::OnLButtonDown(nFlags, point);
  163. PostMessage(WM_NCLBUTTONDOWN, HTCAPTION, MAKELPARAM(point.x,point.y));
  164. }
  165. int CTransparentWnd::OnCreate(LPCREATESTRUCT lpCreateStruct) 
  166. {
  167. if (CWnd::OnCreate(lpCreateStruct) == -1)
  168. return -1;
  169. xcounter=5,ycounter=5;
  170. SetTimer(1,300,NULL);
  171. SetWindowPos(&wndTopMost,0,0,0,0,SWP_NOSIZE|SWP_NOMOVE);
  172. return 0;
  173. }
  174. BOOL CTransparentWnd::OnEraseBkgnd(CDC* pDC) 
  175. {
  176. CRect rect;
  177. GetWindowRect(&rect);
  178. CDC memDC;
  179. CBitmap &cBitmap=m_bmpDraw;;
  180. CBitmap* pOldMemBmp = NULL;
  181. CFont* pOldMemFont=NULL;
  182. memDC.CreateCompatibleDC(pDC);
  183. pOldMemBmp = memDC.SelectObject(&cBitmap);
  184. pDC->BitBlt(0, 0, rect.Width(), rect.Height(), &memDC, 0, 0, SRCCOPY);
  185. if (pOldMemBmp) memDC.SelectObject( pOldMemBmp );
  186. return TRUE;
  187. }
  188. void CTransparentWnd::OnPaint()
  189. {
  190. CPaintDC dc(this);
  191. // Add your drawing code here!
  192. }
  193. void CTransparentWnd::OnTimer(UINT nIDEvent) 
  194. {
  195. switch(nIDEvent)
  196. {
  197. case(1)://judge
  198. DoSet();
  199. break;
  200. case(2)://walk
  201. DoWalk();
  202. break;
  203. default:
  204. break;
  205. }
  206. m_iAniSeq++;
  207. CWnd::OnTimer(nIDEvent);
  208. }
  209. void CTransparentWnd::OnDestroy() 
  210. {
  211. CWnd::OnDestroy();
  212. }
  213. void CTransparentWnd::OnRButtonDown(UINT nFlags, CPoint point) 
  214. {
  215. CWnd::OnRButtonDown(nFlags, point);
  216. CScrGeniusDlg dlg;
  217. if(IDOK!=dlg.DoModal())
  218. DestroyWindow();
  219. }