AboutCtrl.cpp
上传用户:dengkfang
上传日期:2008-12-30
资源大小:5233k
文件大小:12k
源码类别:

CA认证

开发平台:

Visual C++

  1. /****************************************************************/
  2. /* */
  3. /*  AboutCtrl.cpp */
  4. /* */
  5. /*  Implementation of the CAboutCtrl.cpp class. */
  6. /* */
  7. /*  Programmed by Pablo van der Meer */
  8. /*  Copyright Pablo Software Solutions 2002 */
  9. /*  http://www.pablovandermeer.nl */
  10. /* */
  11. /*  Last updated: 28 june 2002 */
  12. /* */
  13. /****************************************************************/
  14. #include "stdafx.h"
  15. #include "....resource.h"
  16. #include "AboutCtrl.h"
  17. #include <math.h>
  18. #ifdef _DEBUG
  19. #define new DEBUG_NEW
  20. #undef THIS_FILE
  21. static char THIS_FILE[] = __FILE__;
  22. #endif
  23. // convert degrees to radians
  24. double deg2rad(int angle) 
  25. {
  26. // (angle / 180) * 3.14159265359
  27. return angle * 0.0174;
  28. }
  29. CAboutCtrl::CAboutCtrl()
  30. {
  31. m_hCreditsDC = NULL;
  32. m_hLogoDC = NULL;
  33. m_hBackgroundDC = NULL;
  34. m_hMemDC = NULL;
  35. m_cxLogo = 0;
  36. m_cyLogo = 0;
  37. m_xPos = 0;
  38. m_yPos = 0; 
  39. m_strCredits = "";
  40. }
  41. CAboutCtrl::~CAboutCtrl()
  42. {
  43. // clean up
  44. if (m_hCreditsDC != NULL)
  45. DeleteDC(m_hCreditsDC);
  46. if (m_hLogoDC != NULL)
  47. DeleteDC(m_hLogoDC);
  48. if (m_hBackgroundDC != NULL)
  49. DeleteDC(m_hBackgroundDC);
  50. if (m_hMemDC != NULL)
  51. DeleteDC(m_hMemDC);
  52. }
  53. BEGIN_MESSAGE_MAP(CAboutCtrl, CStatic)
  54. //{{AFX_MSG_MAP(CAboutCtrl)
  55. ON_WM_PAINT()
  56. ON_WM_TIMER()
  57. //}}AFX_MSG_MAP
  58. END_MESSAGE_MAP()
  59. /********************************************************************/
  60. /* */
  61. /* Function name : OnPaint */
  62. /* Description   : Called when the application makes a request to */
  63. /*    repaint a portion of the window. */
  64. /* */
  65. /********************************************************************/
  66. void CAboutCtrl::OnPaint() 
  67. {
  68. CPaintDC dc(this); // device context for painting
  69. }
  70. /********************************************************************/
  71. /* */
  72. /* Function name : Initialize */
  73. /* Description   : Initialize some stuff */
  74. /* */
  75. /********************************************************************/
  76. void CAboutCtrl::Initialize()
  77. {
  78. int cx, cy;
  79. // First time calling , do some init (loading pictures and create's some Hdc
  80. m_xAngle = 180; // logo x angle
  81. m_yAngle = 60; // logo y angle
  82. m_nSpeed = 6; // spin speed
  83. CRect rect;
  84. GetClientRect(rect);
  85. CDC *pDC = GetDC();
  86. // create credits dc
  87. LoadCredits(m_hCreditsDC, m_cxData , m_cyData, pDC->m_hDC);
  88. // Load logo and creates logo dc
  89. // LoadPicture(IDB_HPXS, m_hLogoDC, m_cxLogo, m_cyLogo, pDC->m_hDC);
  90. // Load Backgroundpicture and creates background DC
  91. LoadPicture(IDB_BACKGROUND, m_hBackgroundDC, cx, cy, pDC->m_hDC); 
  92. // create work area
  93. LoadPicture(0, m_hMemDC, cx, cy, pDC->m_hDC);
  94. // set scroll counter
  95. m_nCounter = rect.Height();
  96. ReleaseDC(pDC);
  97. }
  98. /********************************************************************/
  99. /* */
  100. /* Function name : PreSubclassWindow */
  101. /* Description   : Called before the window is subclassed. */
  102. /* */
  103. /********************************************************************/
  104. void CAboutCtrl::PreSubclassWindow() 
  105. {
  106. // initialze dc's
  107. Initialize();
  108. // start animation
  109. SetTimer(1, 40, NULL);
  110. CStatic::PreSubclassWindow();
  111. }
  112. /********************************************************************/
  113. /* */
  114. /* Function name : OnTimer */
  115. /* Description   : Update display */
  116. /* */
  117. /********************************************************************/
  118. void CAboutCtrl::OnTimer(UINT nIDEvent) 
  119. {
  120. if (nIDEvent == 1)
  121. {
  122. AnimateLogo();
  123. }
  124. CStatic::OnTimer(nIDEvent);
  125. }
  126. /********************************************************************/
  127. /* */
  128. /* Function name : AnimateLogo */
  129. /* Description   : Animate 'flying' logo */
  130. /* */
  131. /********************************************************************/
  132. void CAboutCtrl::AnimateLogo()
  133. {
  134. CRect rect;
  135. GetClientRect(rect);
  136. // move from left to right
  137. m_xPos = m_xPos + 2;
  138. if (m_xPos > rect.Width())
  139. {
  140. m_xPos = - m_cxLogo / 2;
  141. }
  142. // move from top to bottom
  143. m_yPos = m_yPos + 1;
  144. if (m_yPos > rect.Height())
  145. {
  146. m_yPos = -m_cyLogo;
  147. }
  148. // copy background in memory bitmap
  149. BitBlt(m_hMemDC, 0, 0, rect.Width(), rect.Height(), m_hBackgroundDC, 0, 0, SRCCOPY);
  150. for (int i = 1; i < m_cxLogo; i++)
  151. {
  152. // copy logo with sinus effect in memory dc
  153. BitBlt(m_hMemDC, (int)(cos(deg2rad(m_xAngle + i)) * (m_cxLogo / 4.25) + m_xPos), 
  154.  (int)(sin(deg2rad(m_yAngle + i)) * 10 + 2.5 + m_yPos), 
  155.  1, m_cyLogo, m_hLogoDC, i, 0, SRCAND);
  156. }
  157.         
  158. int yPos = m_cyData - m_nCounter - 100;
  159. // copy logo with sinus effect in memory dc
  160. if (m_nCounter > (rect.Height() - 30))
  161. {
  162. yPos = m_cyData - m_nCounter - 100;
  163. }
  164. // copy credits in memory dc
  165. BitBlt(m_hMemDC, m_uScrollTextX, m_nCounter--, m_cxData, yPos, m_hCreditsDC, 0, 0, SRCAND);
  166. if (m_nCounter< -m_cyData)
  167. {
  168. m_nCounter = rect.Height();
  169. }
  170. CDC *pDC = GetDC();
  171. // and finally, copy memory bitmap to screen
  172. BitBlt(pDC->m_hDC, 0, 0, rect.Width(), rect.Height(), m_hMemDC, 0, 0, SRCCOPY);
  173. ReleaseDC(pDC);
  174. // any calculations follows
  175. m_xAngle = m_xAngle + (int)(m_nSpeed * 0.5); // rotate logo x
  176. m_yAngle = m_yAngle + m_nSpeed * 2; // rotate logo y
  177.        
  178. // did full rotation ?
  179. if (m_xAngle >= 360)
  180. {
  181. // reset angle
  182. m_xAngle = 0;
  183. }
  184. // did full rotation ?
  185. if (m_xAngle <= -180) 
  186. {
  187. m_nSpeed = m_nSpeed * -1;
  188. }
  189. if (m_yAngle >= 360)
  190. {
  191. m_yAngle = 0;
  192. }
  193. }
  194. /********************************************************************/
  195. /* */
  196. /* Function name : LoadPicture */
  197. /* Description   : Load picture from resource into device context */
  198. /* */
  199. /********************************************************************/
  200. void CAboutCtrl::LoadPicture(int nResourceID, HDC &hDestinationDC, int &nWidth, int &nHeight, HDC hDC)
  201. {
  202. HDC hMemDC;
  203. HDC hdcCompatible;
  204. HBITMAP hbmScreen;
  205.  
  206. if (nResourceID != 0)
  207. {
  208. // if resourceid is given, load bitmap
  209. HBITMAP hPicture = LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(nResourceID));
  210. BITMAP bm;
  211. GetObject(hPicture, sizeof (BITMAP), (LPSTR)&bm);
  212. hMemDC = CreateCompatibleDC(hDC);
  213. HBITMAP hOldBMP = (HBITMAP)SelectObject(hMemDC, hPicture);
  214. nWidth = bm.bmWidth;
  215. nHeight = bm.bmHeight;
  216. // Create the DC
  217. hdcCompatible = CreateCompatibleDC(hDC);    
  218. // Temporary memory bitmap
  219. hbmScreen = CreateCompatibleBitmap(hDC, nWidth, nHeight);
  220. // select bitmap into dc
  221. if (SelectObject(hdcCompatible, hbmScreen) == NULL)
  222. {
  223. // return null
  224. hDestinationDC = NULL;                                        
  225. }
  226. else 
  227. {
  228. // return the DC
  229. hDestinationDC = hdcCompatible;
  230. }
  231. if (hDestinationDC)
  232. BitBlt(hDestinationDC, 0, 0, nWidth, nHeight, hMemDC, 0, 0, SRCCOPY);
  233. SelectObject(hMemDC, hOldBMP);
  234. // Release temporary stuff
  235. DeleteDC(hMemDC);
  236. DeleteObject(hbmScreen);
  237. DeleteObject(hPicture);
  238. }
  239. else // if no resourceid is given, create empty DC with specified width and height
  240. {
  241. // create the DC
  242. hdcCompatible = CreateCompatibleDC(hDC);
  243. // temporary memory bitmap
  244. hbmScreen = CreateCompatibleBitmap(hDC, nWidth, nHeight);
  245. // if the function fails
  246. if (SelectObject(hdcCompatible, hbmScreen) == NULL)
  247. {
  248. // return null
  249. hDestinationDC = NULL;
  250. }
  251. else
  252. {
  253.  // if it succeeds, return the DC
  254. hDestinationDC = hdcCompatible;                                     
  255. }
  256. DeleteObject(hbmScreen);
  257. }
  258. }
  259. /********************************************************************/
  260. /* */
  261. /* Function name : LoadCredits */
  262. /* Description   : Create credits picture into device context */
  263. /* */
  264. /********************************************************************/
  265. void CAboutCtrl::LoadCredits(HDC &hDestinationDC, int nWidth, int nHeight, HDC hDC)
  266. {
  267. HDC hdcCompatible;
  268. HBITMAP hbmScreen;
  269.  
  270. // Create the DC
  271. hdcCompatible = CreateCompatibleDC(hDC);
  272. // Temporary bitmap
  273. hbmScreen = CreateCompatibleBitmap(hDC, nWidth, nHeight);
  274. // if the function fails
  275. if (SelectObject(hdcCompatible, hbmScreen) == NULL)
  276. {
  277. // return null
  278. hDC = NULL;
  279. }
  280. else
  281. {
  282.  // if it succeeds, return the DC
  283. hDestinationDC = hdcCompatible;
  284. RECT rc;
  285. rc.top = 0;
  286. rc.left = 0;
  287. rc.bottom = nHeight;
  288. rc.right = nWidth;
  289. HFONT pOldFont;
  290. HFONT hFontTahoma, hFontBold, hFontNormal;
  291. FillRect(hDestinationDC, &rc, (HBRUSH)GetStockObject(WHITE_BRUSH));
  292. // create a bunch of fonts
  293. hFontTahoma = CreateFont(20, 0, 0, 0, 
  294.   FW_BOLD, FALSE, FALSE, 0, 
  295.     ANSI_CHARSET,
  296.                     OUT_DEFAULT_PRECIS,
  297.                     CLIP_DEFAULT_PRECIS,
  298.                     PROOF_QUALITY,
  299.                     VARIABLE_PITCH | 0x04 | FF_DONTCARE,
  300.                     (LPSTR)"Arial");
  301. hFontBold = CreateFont(14, 0, 0, 0, 
  302.   FW_BOLD, FALSE, FALSE, 0, 
  303.     ANSI_CHARSET,
  304.                     OUT_DEFAULT_PRECIS,
  305.                     CLIP_DEFAULT_PRECIS,
  306.                     PROOF_QUALITY,
  307.                     VARIABLE_PITCH | 0x04 | FF_DONTCARE,
  308.                     (LPSTR)"Arial");
  309. hFontNormal = CreateFont(14, 0, 0, 0, 
  310.   FALSE, FALSE, FALSE, 0, 
  311.     ANSI_CHARSET,
  312.                     OUT_DEFAULT_PRECIS,
  313.                     CLIP_DEFAULT_PRECIS,
  314.                     PROOF_QUALITY,
  315.                     VARIABLE_PITCH | 0x04 | FF_DONTCARE,
  316.                     (LPSTR)"Arial");
  317. CString strSub;
  318. int nCount=0;
  319. // SetBkMode(hDestinationDC, TRANSPARENT);
  320. // draw each line, based on specified type
  321. while(AfxExtractSubString(strSub, m_strCredits, nCount++, 'n'))
  322. {
  323. TCHAR nType = 0;
  324. COLORREF oldColor;
  325. if (!strSub.IsEmpty())
  326. nType = strSub.GetAt(0);
  327. switch(nType)
  328. {
  329. case 't': // title
  330. oldColor = SetTextColor(hDestinationDC, RGB(16,140,231));
  331. pOldFont = (HFONT)SelectObject(hDestinationDC, hFontTahoma);
  332. strSub.TrimLeft('t');
  333. DrawText(hDestinationDC, strSub, strSub.GetLength(), &rc, DT_TOP|DT_LEFT|DT_NOPREFIX | DT_SINGLELINE);
  334. break;
  335. case 'r': // bold
  336. oldColor = SetTextColor(hDestinationDC, RGB(0,0,0));
  337. pOldFont = (HFONT)SelectObject(hDestinationDC, hFontBold);
  338. strSub.TrimLeft('r');
  339. DrawText(hDestinationDC, strSub, strSub.GetLength(), &rc, DT_TOP|DT_LEFT|DT_NOPREFIX | DT_SINGLELINE);
  340. break;
  341. default: // normal
  342. oldColor = SetTextColor(hDestinationDC, RGB(0,0,0));
  343. pOldFont = (HFONT)SelectObject(hDestinationDC, hFontNormal);
  344. DrawText(hDestinationDC, strSub, strSub.GetLength(), &rc, DT_TOP|DT_LEFT|DT_NOPREFIX | DT_SINGLELINE);
  345. break;
  346. }
  347. // next line
  348. TEXTMETRIC tm;  
  349. GetTextMetrics(hDestinationDC, &tm);
  350. rc.top += tm.tmHeight;
  351. // set back old values
  352. SetTextColor(hDestinationDC, oldColor);
  353. SelectObject(hDestinationDC, pOldFont);
  354. }
  355. // clean up
  356. DeleteObject(hFontBold);
  357. DeleteObject(hFontNormal);
  358. DeleteObject(hFontTahoma);
  359. }
  360. DeleteObject(hbmScreen);
  361. }
  362. /********************************************************************/
  363. /* */
  364. /* Function name : SetCredits */
  365. /* Description   : Member function to set credits text. */
  366. /* */
  367. /********************************************************************/
  368. void CAboutCtrl::SetCredits(LPCTSTR lpszCredits, UINT uScrollTextX, 
  369. UINT uScrollTextWidth, UINT uScrollTextHeight)
  370. {
  371. m_strCredits = lpszCredits;
  372. m_uScrollTextX = uScrollTextX;
  373. m_cxData = uScrollTextWidth;    //滚动文字框宽度 文字过长可以增加
  374. m_cyData = uScrollTextHeight; ////滚动文字框高度度 文字行数过多可以增加
  375. }