StarWarsCtrl.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /****************************************************************/
  2. /* */
  3. /* StarWarsCtrl.cpp */
  4. /* */
  5. /* Implementation of the CStarWarsCtrl.cpp class. */
  6. /* */
  7. /* Programmed by LYFZ van der Meer */
  8. /* Copyright LYFZ Software Solutions 2002 */
  9. /* http://www.LYFZvandermeer.nl */
  10. /* */
  11. /* Last updated: 10 july 2002 */
  12. /* */
  13. /****************************************************************/
  14. #include "stdafx.h"
  15. #include "resource.h"
  16. #include "StarWarsCtrl.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. #define getrandom(min,max) ((rand()%(int)(((max)+1)-(min)))+(min));
  24. CStarWarsCtrl::CStarWarsCtrl()
  25. {
  26. // protected bitmaps to restore the memory DC's
  27. m_pOldBitmap = NULL;
  28. m_Font.CreateFont(22, 0, 0, 0, FW_BOLD,
  29. FALSE, FALSE, 0, ANSI_CHARSET,
  30. OUT_DEFAULT_PRECIS,
  31. CLIP_DEFAULT_PRECIS,
  32. DEFAULT_QUALITY,
  33. DEFAULT_PITCH|FF_SWISS, "Tahoma");
  34. m_nScrollSpeed = 2;
  35. m_nStarsSpeed = 20;
  36. }
  37. CStarWarsCtrl::~CStarWarsCtrl()
  38. {
  39. if (m_pOldBitmap != NULL)
  40. m_MainDC.SelectObject(m_pOldBitmap);
  41. }
  42. BEGIN_MESSAGE_MAP(CStarWarsCtrl, CStatic)
  43. //{{AFX_MSG_MAP(CStarWarsCtrl)
  44. ON_WM_PAINT()
  45. ON_WM_SIZE()
  46. ON_WM_TIMER()
  47. ON_WM_LBUTTONDOWN()
  48. //}}AFX_MSG_MAP
  49. END_MESSAGE_MAP()
  50. /********************************************************************/
  51. /* */
  52. /* Function name : PreSubclassWindow */
  53. /* Description : Initialize some stuff */
  54. /* */
  55. /********************************************************************/
  56. void CStarWarsCtrl::PreSubclassWindow()
  57. {
  58. CClientDC dc(this);
  59. GetClientRect(m_rectClient);
  60. // initialize stars
  61. for (int i = 0; i < NUM_STARS; i++)
  62. {
  63. m_StarArray[i].x = getrandom(0, 1024);
  64. m_StarArray[i].x -= 512;
  65. m_StarArray[i].y = getrandom(0, 1024);
  66. m_StarArray[i].y -= 512;
  67. m_StarArray[i].z = getrandom(0, 512);
  68. m_StarArray[i].z -= 256;
  69. }
  70. m_TextLines.Add("A long time ago");
  71. m_TextLines.Add("");
  72. m_TextLines.Add("in a galaxy far far away");
  73. m_TextLines.Add("");
  74. m_TextLines.Add("this application was programmed by");
  75. m_TextLines.Add("");
  76. m_TextLines.Add("LYFZ van der Meer");
  77. m_TextLines.Add("");
  78. m_TextLines.Add("");
  79. CString strAppName = AfxGetApp()->GetProfileString("Settings", "AppName", "LYFZ FTP Server");
  80. m_TextLines.Add(strAppName);
  81. CString strExeName = AfxGetApp()->m_pszExeName;
  82. strExeName += ".exe";
  83. GetVersionInformation(strExeName);
  84. m_TextLines.Add(GetVersionInformation(strExeName));
  85. m_TextLines.Add("");
  86. m_TextLines.Add("");
  87. m_TextLines.Add("Copyright © 2002");
  88. m_TextLines.Add("LYFZ Software Solutions");
  89. m_TextLines.Add("");
  90. m_TextLines.Add("http://www.LYFZvandermeer.nl");
  91. m_TextLines.Add("");
  92. m_TextLines.Add("");
  93. m_TextLines.Add("Based partially on:");
  94. m_TextLines.Add("FileZilla Server");
  95. m_TextLines.Add("by Tim Kosse");
  96. m_nScrollPos = m_rectClient.Height();
  97. // calculate speed so that it scrolls the same speed on a different machine
  98. DWORD t1 = GetTickCount();
  99. InvalidateCtrl();
  100. DWORD t2 = GetTickCount();
  101. t2 -= t1; // = 50 on my system
  102. m_nScrollSpeed = (m_nScrollSpeed * t2)/50;
  103. SetTimer(1, 75, NULL);
  104. CStatic::PreSubclassWindow();
  105. }
  106. /********************************************************************/
  107. /* */
  108. /* Function name : OnPaint */
  109. /* Description : Called when the application makes a request to */
  110. /* repaint a portion of the window. */
  111. /* */
  112. /********************************************************************/
  113. void CStarWarsCtrl::OnPaint()
  114. {
  115. CPaintDC dc(this); // device context for painting
  116. CDC memDC;
  117. CBitmap memBitmap;
  118. CBitmap* oldBitmap;
  119. // to avoid flicker, establish a memory dc, draw to it
  120. // and then BitBlt it to the client
  121. memDC.CreateCompatibleDC(&dc);
  122. memBitmap.CreateCompatibleBitmap(&dc, m_rectClient.Width(), m_rectClient.Height());
  123. oldBitmap = (CBitmap *)memDC.SelectObject(&memBitmap);
  124. if (memDC.GetSafeHdc() != NULL)
  125. {
  126. // first drop the bitmap on the memory dc
  127. memDC.BitBlt(0, 0, m_rectClient.Width(), m_rectClient.Height(), &m_MainDC, 0, 0, SRCCOPY);
  128. // finally send the result to the display
  129. dc.BitBlt(0, 0, m_rectClient.Width(), m_rectClient.Height(), &memDC, 0, 0, SRCCOPY);
  130. }
  131. memDC.SelectObject(oldBitmap);
  132. }
  133. /********************************************************************/
  134. /* */
  135. /* Function name : OnSize */
  136. /* Description : The framework calls this member function after */
  137. /* the window’s size has changed. */
  138. /* */
  139. /********************************************************************/
  140. void CStarWarsCtrl::OnSize(UINT nType, int cx, int cy)
  141. {
  142. CStatic::OnSize(nType, cx, cy);
  143. // OnSize automatically gets called during the setup of the control
  144. GetClientRect(m_rectClient);
  145. // destroy and recreate the main bitmap
  146. CClientDC dc(this);
  147. if (m_pOldBitmap && m_MainBitmap.GetSafeHandle() && m_MainDC.GetSafeHdc())
  148. {
  149. m_MainDC.SelectObject(m_pOldBitmap);
  150. m_MainBitmap.DeleteObject();
  151. m_MainBitmap.CreateCompatibleBitmap(&dc, m_rectClient.Width(), m_rectClient.Height());
  152. m_pOldBitmap = m_MainDC.SelectObject(&m_MainBitmap);
  153. }
  154. }
  155. /********************************************************************/
  156. /* */
  157. /* Function name : DoStars */
  158. /* Description : Draw stars */
  159. /* */
  160. /********************************************************************/
  161. void CStarWarsCtrl::DoStars(CDC *pDC)
  162. {
  163. m_MainDC.SetBkColor(RGB(0,0,0));
  164. m_MainDC.SetTextColor(RGB(255,255,255));
  165. m_MainDC.FillSolidRect(m_rectClient, RGB(0,0,0));
  166. int nFunFactor = 100;
  167. int x, y, z;
  168. for(int i = 0; i < NUM_STARS; i++)
  169. {
  170. m_StarArray[i].z = m_StarArray[i].z - m_nStarsSpeed;
  171. if (m_StarArray[i].z > 255)
  172. {
  173. m_StarArray[i].z = -255;
  174. }
  175. if (m_StarArray[i].z < -255)
  176. {
  177. m_StarArray[i].z = 255;
  178. }
  179. z = m_StarArray[i].z + 256;
  180. x = (m_StarArray[i].x * nFunFactor / z) + (m_rectClient.Width() / 2);
  181. y = (m_StarArray[i].y * nFunFactor / z) + (m_rectClient.Height() / 2);
  182. CPen myPen;
  183. // create a white pen which luminosity depends on the z position (for 3D effect!)
  184. int nColor = 255 - m_StarArray[i].z;
  185. myPen.CreatePen(PS_COSMETIC, 1, RGB(nColor,nColor,nColor));
  186. CPen *pOldPen = (CPen *)m_MainDC.SelectObject(&myPen);
  187. // draw star
  188. m_MainDC.Ellipse(CRect(x, y, x+3, y+3));
  189. m_MainDC.SelectObject(pOldPen);
  190. }
  191. }
  192. /********************************************************************/
  193. /* */
  194. /* Function name : InvalidateCtrl */
  195. /* Description : Draw the Matrix to a bitmap. */
  196. /* */
  197. /********************************************************************/
  198. void CStarWarsCtrl::InvalidateCtrl()
  199. {
  200. // in case we haven't established the memory dc's
  201. CClientDC dc(this);
  202. // if we don't have one yet, set up a memory dc for the control
  203. if (m_MainDC.GetSafeHdc() == NULL)
  204. {
  205. m_MainDC.CreateCompatibleDC(&dc);
  206. m_MainBitmap.CreateCompatibleBitmap(&dc, m_rectClient.Width(), m_rectClient.Height());
  207. m_pOldBitmap = m_MainDC.SelectObject(&m_MainBitmap);
  208. }
  209. DoStars(&dc);
  210. DoScrollText(&dc);
  211. // finally, force redraw
  212. InvalidateRect(m_rectClient);
  213. }
  214. /********************************************************************/
  215. /* */
  216. /* Function name : OnTimer */
  217. /* Description : Update display */
  218. /* */
  219. /********************************************************************/
  220. void CStarWarsCtrl::OnTimer(UINT nIDEvent)
  221. {
  222. if (nIDEvent == 1)
  223. InvalidateCtrl();
  224. CStatic::OnTimer(nIDEvent);
  225. }
  226. /********************************************************************/
  227. /* */
  228. /* Function name : DoScrollText */
  229. /* Description : Do scrolling text like in the movie 'Star Wars' */
  230. /* */
  231. /********************************************************************/
  232. void CStarWarsCtrl::DoScrollText(CDC *pDC)
  233. {
  234. int nPosX =0;
  235. int nPosY =0;
  236. CDC memDC;
  237. CBitmap memBitmap;
  238. CFont *oldFont;
  239. memDC.CreateCompatibleDC(pDC);
  240. memBitmap.CreateCompatibleBitmap(pDC, m_rectClient.Width(), m_rectClient.Height());
  241. memDC.SelectObject(&memBitmap);
  242. memDC.SetBkColor(RGB(0,0,0));
  243. memDC.SetTextColor(RGB(0,255,0));
  244. memDC.SetBkMode(TRANSPARENT);
  245. oldFont = memDC.SelectObject(&m_Font);
  246. // black
  247. memDC.BitBlt(0, 0, m_rectClient.Width(), m_rectClient.Height(), NULL, 0, 0, BLACKNESS);
  248. // draw Credits on the hidden Picture
  249. for(int i=0; i < m_TextLines.GetSize(); i++)
  250. {
  251. // set position for this line
  252. CSize size = memDC.GetTextExtent(m_TextLines.GetAt(i));
  253. nPosY = m_nScrollPos + (i * size.cy);
  254. if (nPosY > 0)
  255. {
  256. nPosX = (m_rectClient.Width() / 2) - (size.cx / 2);
  257. if (nPosY > 255)
  258. {
  259. memDC.SetTextColor(RGB(255, 255, 255));
  260. }
  261. else
  262. {
  263. // set fade color
  264. memDC.SetTextColor(RGB(nPosY, nPosY, nPosY));
  265. }
  266. // print text
  267. memDC.TextOut(nPosX, nPosY, m_TextLines.GetAt(i));
  268. }
  269. else
  270. {
  271. // start all over ...
  272. if (i == (m_TextLines.GetSize()-1))
  273. {
  274. m_nScrollPos = m_rectClient.Height();
  275. }
  276. }
  277. }
  278. int nWidth = m_rectClient.Width();
  279. int nHeight = m_rectClient.Height();
  280. // shrink text from bottom to top to create Star Wars effect
  281. for (int y=0; y <nHeight; y++)
  282. {
  283. double nScale = (double)y/(double)nHeight;
  284. int nOffset = (int)(nWidth - nWidth*nScale)/2;
  285. m_MainDC.StretchBlt(nOffset, y, (int)(nWidth*nScale), 1, &memDC, 0, y, nWidth, 1, SRCPAINT);
  286. }
  287. // restore the font
  288. memDC.SelectObject(oldFont);
  289. // move text up one pixel
  290. m_nScrollPos = m_nScrollPos - m_nScrollSpeed;
  291. }
  292. /********************************************************************/
  293. /* */
  294. /* Function name : SetScrollSpeed */
  295. /* Description : Set speed of scrolling */
  296. /* */
  297. /********************************************************************/
  298. void CStarWarsCtrl::SetScrollSpeed(int nSpeed)
  299. {
  300. m_nScrollSpeed = nSpeed;
  301. }
  302. /********************************************************************/
  303. /* */
  304. /* Function name : SetStarSpeed */
  305. /* Description : Set speed of the stars */
  306. /* */
  307. /********************************************************************/
  308. void CStarWarsCtrl::SetStarSpeed(int nSpeed)
  309. {
  310. m_nStarsSpeed = nSpeed;
  311. }
  312. /********************************************************************/
  313. /* */
  314. /* Function name : UpdateVersionInformation */
  315. /* Description : Show version information from resource. */
  316. /* */
  317. /********************************************************************/
  318. CString CStarWarsCtrl::GetVersionInformation(LPCTSTR lpszModuleName)
  319. {
  320. CString strResult;
  321. BYTE *pDataBlock = NULL;
  322. DWORD FAR *translation;
  323. DWORD FAR *buffer;
  324. DWORD dwHandle;
  325. UINT nBytes;
  326. // WinAPI wants non-const arguments
  327. LPTSTR lpszExeName = const_cast<LPTSTR>(lpszModuleName);
  328. char szName[512]; // StringFileInfo data block.
  329. // Get the actual size of the information block.
  330. nBytes = (UINT)GetFileVersionInfoSize(lpszExeName, &dwHandle);
  331. if (nBytes)
  332. {
  333. pDataBlock = new BYTE[nBytes];
  334. // Get the actual block for the version information
  335. if (GetFileVersionInfo(lpszExeName, dwHandle, nBytes, pDataBlock))
  336. {
  337. if (VerQueryValue(pDataBlock, "\\VarFileInfo\\Translation", (VOID FAR * FAR *)&translation, (UINT FAR *)&nBytes))
  338. {
  339. // The File Version for this application
  340. wsprintf(szName, "\\StringFileInfo\\%04x%04x\\PrivateBuild", LOWORD(*translation), HIWORD(*translation));
  341. if (VerQueryValue(pDataBlock, szName, (VOID FAR * FAR *)&buffer, (UINT FAR *)&nBytes))
  342. {
  343. strResult.Format((char far *)buffer);
  344. }
  345. }
  346. else
  347. {
  348. // sorry ...
  349. }
  350. }
  351. if (pDataBlock)
  352. delete[] pDataBlock;
  353. }
  354. return strResult;
  355. }
  356. /********************************************************************/
  357. /* */
  358. /* Function name : OnLButtonDown */
  359. /* Description : Go to our website... */
  360. /* */
  361. /********************************************************************/
  362. void CStarWarsCtrl::OnLButtonDown(UINT nFlags, CPoint point)
  363. {
  364. // open URL in the browser.
  365. ShellExecute(NULL, "open", "http://www.LYFZvandermeer.nl", NULL, NULL, SW_SHOWNORMAL);
  366. CStatic::OnLButtonDown(nFlags, point);
  367. }