StarWarsCtrl.cpp 12 KB

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