123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444 |
- /****************************************************************/
- /* */
- /* StarWarsCtrl.cpp */
- /* */
- /* Implementation of the CStarWarsCtrl.cpp class. */
- /* */
- /* Programmed by LYFZ van der Meer */
- /* Copyright LYFZ Software Solutions 2002 */
- /* http://www.LYFZvandermeer.nl */
- /* */
- /* Last updated: 10 july 2002 */
- /* */
- /****************************************************************/
- #include "stdafx.h"
- #include "resource.h"
- #include "StarWarsCtrl.h"
- #include <math.h>
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- #define getrandom(min,max) ((rand()%(int)(((max)+1)-(min)))+(min));
- CStarWarsCtrl::CStarWarsCtrl()
- {
- // protected bitmaps to restore the memory DC's
- m_pOldBitmap = NULL;
- m_Font.CreateFont(22, 0, 0, 0, FW_BOLD,
- FALSE, FALSE, 0, ANSI_CHARSET,
- OUT_DEFAULT_PRECIS,
- CLIP_DEFAULT_PRECIS,
- DEFAULT_QUALITY,
- DEFAULT_PITCH|FF_SWISS, "Tahoma");
- m_nScrollSpeed = 2;
- m_nStarsSpeed = 20;
- }
- CStarWarsCtrl::~CStarWarsCtrl()
- {
- if (m_pOldBitmap != NULL)
- m_MainDC.SelectObject(m_pOldBitmap);
- }
- BEGIN_MESSAGE_MAP(CStarWarsCtrl, CStatic)
- //{{AFX_MSG_MAP(CStarWarsCtrl)
- ON_WM_PAINT()
- ON_WM_SIZE()
- ON_WM_TIMER()
- ON_WM_LBUTTONDOWN()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /********************************************************************/
- /* */
- /* Function name : PreSubclassWindow */
- /* Description : Initialize some stuff */
- /* */
- /********************************************************************/
- void CStarWarsCtrl::PreSubclassWindow()
- {
- CClientDC dc(this);
- GetClientRect(m_rectClient);
- // initialize stars
- for (int i = 0; i < NUM_STARS; i++)
- {
- m_StarArray[i].x = getrandom(0, 1024);
- m_StarArray[i].x -= 512;
- m_StarArray[i].y = getrandom(0, 1024);
- m_StarArray[i].y -= 512;
- m_StarArray[i].z = getrandom(0, 512);
- m_StarArray[i].z -= 256;
- }
- m_TextLines.Add("A long time ago");
- m_TextLines.Add("");
- m_TextLines.Add("in a galaxy far far away");
- m_TextLines.Add("");
- m_TextLines.Add("this application was programmed by");
- m_TextLines.Add("");
- m_TextLines.Add("LYFZ van der Meer");
- m_TextLines.Add("");
- m_TextLines.Add("");
- CString strAppName = AfxGetApp()->GetProfileString("Settings", "AppName", "LYFZ FTP Server");
- m_TextLines.Add(strAppName);
- CString strExeName = AfxGetApp()->m_pszExeName;
- strExeName += ".exe";
- GetVersionInformation(strExeName);
- m_TextLines.Add(GetVersionInformation(strExeName));
- m_TextLines.Add("");
- m_TextLines.Add("");
- m_TextLines.Add("Copyright © 2002");
- m_TextLines.Add("LYFZ Software Solutions");
- m_TextLines.Add("");
- m_TextLines.Add("http://www.LYFZvandermeer.nl");
- m_TextLines.Add("");
- m_TextLines.Add("");
- m_TextLines.Add("Based partially on:");
- m_TextLines.Add("FileZilla Server");
- m_TextLines.Add("by Tim Kosse");
- m_nScrollPos = m_rectClient.Height();
- // calculate speed so that it scrolls the same speed on a different machine
- DWORD t1 = GetTickCount();
- InvalidateCtrl();
- DWORD t2 = GetTickCount();
- t2 -= t1; // = 50 on my system
- m_nScrollSpeed = (m_nScrollSpeed * t2)/50;
- SetTimer(1, 75, NULL);
- CStatic::PreSubclassWindow();
- }
- /********************************************************************/
- /* */
- /* Function name : OnPaint */
- /* Description : Called when the application makes a request to */
- /* repaint a portion of the window. */
- /* */
- /********************************************************************/
- void CStarWarsCtrl::OnPaint()
- {
- CPaintDC dc(this); // device context for painting
-
- CDC memDC;
- CBitmap memBitmap;
- CBitmap* oldBitmap;
- // to avoid flicker, establish a memory dc, draw to it
- // and then BitBlt it to the client
- memDC.CreateCompatibleDC(&dc);
- memBitmap.CreateCompatibleBitmap(&dc, m_rectClient.Width(), m_rectClient.Height());
- oldBitmap = (CBitmap *)memDC.SelectObject(&memBitmap);
- if (memDC.GetSafeHdc() != NULL)
- {
- // first drop the bitmap on the memory dc
- memDC.BitBlt(0, 0, m_rectClient.Width(), m_rectClient.Height(), &m_MainDC, 0, 0, SRCCOPY);
- // finally send the result to the display
- dc.BitBlt(0, 0, m_rectClient.Width(), m_rectClient.Height(), &memDC, 0, 0, SRCCOPY);
- }
- memDC.SelectObject(oldBitmap);
- }
- /********************************************************************/
- /* */
- /* Function name : OnSize */
- /* Description : The framework calls this member function after */
- /* the window’s size has changed. */
- /* */
- /********************************************************************/
- void CStarWarsCtrl::OnSize(UINT nType, int cx, int cy)
- {
- CStatic::OnSize(nType, cx, cy);
-
- // OnSize automatically gets called during the setup of the control
- GetClientRect(m_rectClient);
- // destroy and recreate the main bitmap
- CClientDC dc(this);
- if (m_pOldBitmap && m_MainBitmap.GetSafeHandle() && m_MainDC.GetSafeHdc())
- {
- m_MainDC.SelectObject(m_pOldBitmap);
- m_MainBitmap.DeleteObject();
- m_MainBitmap.CreateCompatibleBitmap(&dc, m_rectClient.Width(), m_rectClient.Height());
- m_pOldBitmap = m_MainDC.SelectObject(&m_MainBitmap);
- }
- }
- /********************************************************************/
- /* */
- /* Function name : DoStars */
- /* Description : Draw stars */
- /* */
- /********************************************************************/
- void CStarWarsCtrl::DoStars(CDC *pDC)
- {
- m_MainDC.SetBkColor(RGB(0,0,0));
- m_MainDC.SetTextColor(RGB(255,255,255));
- m_MainDC.FillSolidRect(m_rectClient, RGB(0,0,0));
- int nFunFactor = 100;
- int x, y, z;
- for(int i = 0; i < NUM_STARS; i++)
- {
- m_StarArray[i].z = m_StarArray[i].z - m_nStarsSpeed;
- if (m_StarArray[i].z > 255)
- {
- m_StarArray[i].z = -255;
- }
- if (m_StarArray[i].z < -255)
- {
- m_StarArray[i].z = 255;
- }
-
- z = m_StarArray[i].z + 256;
- x = (m_StarArray[i].x * nFunFactor / z) + (m_rectClient.Width() / 2);
- y = (m_StarArray[i].y * nFunFactor / z) + (m_rectClient.Height() / 2);
-
- CPen myPen;
- // create a white pen which luminosity depends on the z position (for 3D effect!)
- int nColor = 255 - m_StarArray[i].z;
- myPen.CreatePen(PS_COSMETIC, 1, RGB(nColor,nColor,nColor));
- CPen *pOldPen = (CPen *)m_MainDC.SelectObject(&myPen);
- // draw star
- m_MainDC.Ellipse(CRect(x, y, x+3, y+3));
- m_MainDC.SelectObject(pOldPen);
- }
- }
- /********************************************************************/
- /* */
- /* Function name : InvalidateCtrl */
- /* Description : Draw the Matrix to a bitmap. */
- /* */
- /********************************************************************/
- void CStarWarsCtrl::InvalidateCtrl()
- {
- // in case we haven't established the memory dc's
- CClientDC dc(this);
- // if we don't have one yet, set up a memory dc for the control
- if (m_MainDC.GetSafeHdc() == NULL)
- {
- m_MainDC.CreateCompatibleDC(&dc);
- m_MainBitmap.CreateCompatibleBitmap(&dc, m_rectClient.Width(), m_rectClient.Height());
- m_pOldBitmap = m_MainDC.SelectObject(&m_MainBitmap);
- }
-
- DoStars(&dc);
- DoScrollText(&dc);
- // finally, force redraw
- InvalidateRect(m_rectClient);
- }
- /********************************************************************/
- /* */
- /* Function name : OnTimer */
- /* Description : Update display */
- /* */
- /********************************************************************/
- void CStarWarsCtrl::OnTimer(UINT nIDEvent)
- {
- if (nIDEvent == 1)
- InvalidateCtrl();
- CStatic::OnTimer(nIDEvent);
- }
- /********************************************************************/
- /* */
- /* Function name : DoScrollText */
- /* Description : Do scrolling text like in the movie 'Star Wars' */
- /* */
- /********************************************************************/
- void CStarWarsCtrl::DoScrollText(CDC *pDC)
- {
- int nPosX =0;
- int nPosY =0;
- CDC memDC;
- CBitmap memBitmap;
- CFont *oldFont;
- memDC.CreateCompatibleDC(pDC);
- memBitmap.CreateCompatibleBitmap(pDC, m_rectClient.Width(), m_rectClient.Height());
- memDC.SelectObject(&memBitmap);
- memDC.SetBkColor(RGB(0,0,0));
- memDC.SetTextColor(RGB(0,255,0));
- memDC.SetBkMode(TRANSPARENT);
- oldFont = memDC.SelectObject(&m_Font);
- // black
- memDC.BitBlt(0, 0, m_rectClient.Width(), m_rectClient.Height(), NULL, 0, 0, BLACKNESS);
-
- // draw Credits on the hidden Picture
- for(int i=0; i < m_TextLines.GetSize(); i++)
- {
- // set position for this line
- CSize size = memDC.GetTextExtent(m_TextLines.GetAt(i));
- nPosY = m_nScrollPos + (i * size.cy);
- if (nPosY > 0)
- {
- nPosX = (m_rectClient.Width() / 2) - (size.cx / 2);
- if (nPosY > 255)
- {
- memDC.SetTextColor(RGB(255, 255, 255));
- }
- else
- {
- // set fade color
- memDC.SetTextColor(RGB(nPosY, nPosY, nPosY));
- }
-
- // print text
- memDC.TextOut(nPosX, nPosY, m_TextLines.GetAt(i));
- }
- else
- {
- // start all over ...
- if (i == (m_TextLines.GetSize()-1))
- {
- m_nScrollPos = m_rectClient.Height();
- }
- }
- }
- int nWidth = m_rectClient.Width();
- int nHeight = m_rectClient.Height();
- // shrink text from bottom to top to create Star Wars effect
- for (int y=0; y <nHeight; y++)
- {
- double nScale = (double)y/(double)nHeight;
- int nOffset = (int)(nWidth - nWidth*nScale)/2;
- m_MainDC.StretchBlt(nOffset, y, (int)(nWidth*nScale), 1, &memDC, 0, y, nWidth, 1, SRCPAINT);
- }
- // restore the font
- memDC.SelectObject(oldFont);
- // move text up one pixel
- m_nScrollPos = m_nScrollPos - m_nScrollSpeed;
- }
- /********************************************************************/
- /* */
- /* Function name : SetScrollSpeed */
- /* Description : Set speed of scrolling */
- /* */
- /********************************************************************/
- void CStarWarsCtrl::SetScrollSpeed(int nSpeed)
- {
- m_nScrollSpeed = nSpeed;
- }
- /********************************************************************/
- /* */
- /* Function name : SetStarSpeed */
- /* Description : Set speed of the stars */
- /* */
- /********************************************************************/
- void CStarWarsCtrl::SetStarSpeed(int nSpeed)
- {
- m_nStarsSpeed = nSpeed;
- }
- /********************************************************************/
- /* */
- /* Function name : UpdateVersionInformation */
- /* Description : Show version information from resource. */
- /* */
- /********************************************************************/
- CString CStarWarsCtrl::GetVersionInformation(LPCTSTR lpszModuleName)
- {
- CString strResult;
- BYTE *pDataBlock = NULL;
- DWORD FAR *translation;
- DWORD FAR *buffer;
- DWORD dwHandle;
- UINT nBytes;
-
- // WinAPI wants non-const arguments
- LPTSTR lpszExeName = const_cast<LPTSTR>(lpszModuleName);
- char szName[512]; // StringFileInfo data block.
- // Get the actual size of the information block.
- nBytes = (UINT)GetFileVersionInfoSize(lpszExeName, &dwHandle);
- if (nBytes)
- {
- pDataBlock = new BYTE[nBytes];
- // Get the actual block for the version information
- if (GetFileVersionInfo(lpszExeName, dwHandle, nBytes, pDataBlock))
- {
- if (VerQueryValue(pDataBlock, "\\VarFileInfo\\Translation", (VOID FAR * FAR *)&translation, (UINT FAR *)&nBytes))
- {
- // The File Version for this application
- wsprintf(szName, "\\StringFileInfo\\%04x%04x\\PrivateBuild", LOWORD(*translation), HIWORD(*translation));
- if (VerQueryValue(pDataBlock, szName, (VOID FAR * FAR *)&buffer, (UINT FAR *)&nBytes))
- {
- strResult.Format((char far *)buffer);
- }
- }
- else
- {
- // sorry ...
- }
- }
- if (pDataBlock)
- delete[] pDataBlock;
- }
- return strResult;
- }
- /********************************************************************/
- /* */
- /* Function name : OnLButtonDown */
- /* Description : Go to our website... */
- /* */
- /********************************************************************/
- void CStarWarsCtrl::OnLButtonDown(UINT nFlags, CPoint point)
- {
- // open URL in the browser.
- ShellExecute(NULL, "open", "http://www.LYFZvandermeer.nl", NULL, NULL, SW_SHOWNORMAL);
-
- CStatic::OnLButtonDown(nFlags, point);
- }
|