www.pudn.com > XRayImg.rar > AboutCtrl.cpp


/****************************************************************/
/* */
/* AboutCtrl.cpp */
/* */
/* Implementation of the CAboutCtrl.cpp class. */
/* */
/* Programmed by Pablo van der Meer */
/* Copyright Pablo Software Solutions 2002 */
/* http://www.pablovandermeer.nl */
/* */
/* Last updated: 28 june 2002 */
/* */
/****************************************************************/
//修改: 徐卫华 2005.8.3
//修改内容:
// 1) 增加设置的接口 SetCredits()
// 2) 修改 deg2rad() 为static
// 3) 增加 退出功能
/****************************************************************/
#include "stdafx.h"
#include "AboutCtrl.h"
#include <math.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

// convert degrees to radians
static double deg2rad(int angle)
{
// (angle / 180) * 3.14159265359
return angle * 0.0174;
}

CAboutCtrl::CAboutCtrl()
{
m_hCreditsDC = NULL;
m_hLogoDC = NULL;
m_hBackgroundDC = NULL;
m_hMemDC = NULL;

m_cxLogo = 0;
m_cyLogo = 0;
m_xPos = 0;
m_yPos = 0;
m_strCredits=_T("");
}


CAboutCtrl::~CAboutCtrl()
{
// clean up

if (m_hCreditsDC != NULL)
DeleteDC(m_hCreditsDC);
if (m_hLogoDC != NULL)
DeleteDC(m_hLogoDC);
if (m_hBackgroundDC != NULL)
DeleteDC(m_hBackgroundDC);
if (m_hMemDC != NULL)
DeleteDC(m_hMemDC);
}


BEGIN_MESSAGE_MAP(CAboutCtrl, CStatic)
//{{AFX_MSG_MAP(CAboutCtrl)
ON_WM_PAINT()
ON_WM_TIMER()
ON_CONTROL_REFLECT(BN_CLICKED, OnClicked)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/********************************************************************/
/* */
/* Function name : OnPaint */
/* Description : Called when the application makes a request to */
/* repaint a portion of the window. */
/* */
/********************************************************************/
void CAboutCtrl::OnPaint()
{
CPaintDC dc(this); // device context for painting
}

/********************************************************************/
/* */
/* Function name : PreSubclassWindow */
/* Description : Called before the window is subclassed. */
/* */
/********************************************************************/
void CAboutCtrl::PreSubclassWindow()
{
// initialze dc's
DWORD dwStyle = GetStyle();
::SetWindowLong(GetSafeHwnd(), GWL_STYLE, dwStyle | SS_NOTIFY);

Initialize();
// start animation
SetTimer(1, 60, NULL);

CStatic::PreSubclassWindow();
}

/********************************************************************/
/* */
/* Function name : Initialize */
/* Description : Initialize some stuff */
/* */
/********************************************************************/
void CAboutCtrl::Initialize()
{
int cx, cy;

// First time calling , do some init (loading pictures and create's some Hdc
m_xAngle = 180; // logo x angle
m_yAngle = 60; // logo y angle
m_nSpeed = 6; // spin speed

m_cxData = 400;
m_cyData = 500;

CRect rect;
GetClientRect(rect);

CDC *pDC = GetDC();

// create credits dc
LoadCredits(m_hCreditsDC, m_cxData, m_cyData, pDC->m_hDC);
// Load logo and creates logo dc
LoadPicture(m_nBmpLogo, m_hLogoDC, m_cxLogo, m_cyLogo, pDC->m_hDC);
// Load Backgroundpicture and creates background DC
LoadPicture(m_nBmpBkg, m_hBackgroundDC, cx, cy, pDC->m_hDC);
// create work area
LoadPicture(0, m_hMemDC, cx, cy, pDC->m_hDC);

// set scroll counter
m_nCounter = rect.Height();

ReleaseDC(pDC);
}

/********************************************************************/
/* */
/* Function name : OnTimer */
/* Description : Update display */
/* */
/********************************************************************/
void CAboutCtrl::OnTimer(UINT nIDEvent)
{
if (nIDEvent == 1)
{
AnimateLogo();
}

CStatic::OnTimer(nIDEvent);
}

/********************************************************************/
/* */
/* Function name : AnimateLogo */
/* Description : Animate 'flying' logo */
/* */
/********************************************************************/
void CAboutCtrl::AnimateLogo()
{
CRect rect;
GetClientRect(rect);

// move from left to right
m_xPos = m_xPos + 2;
if (m_xPos > rect.Width())
{
m_xPos = -m_cxLogo / 2;
}

// move from top to bottom
m_yPos = m_yPos + 1;
if (m_yPos > rect.Height())
{
m_yPos = -m_cyLogo;
}

// copy background in memory bitmap
BitBlt(m_hMemDC, 0, 0, rect.Width(), rect.Height(),
m_hBackgroundDC, 0, 0, SRCCOPY);

for (int i = 1; i < m_cxLogo; i++)
{
// copy logo with sinus effect in memory dc
BitBlt(m_hMemDC, (int)(cos(deg2rad(m_xAngle + i)) * (m_cxLogo / 4.25) + m_xPos),
(int)(sin(deg2rad(m_yAngle + i)) * 10 + 2.5 + m_yPos),
1, m_cyLogo, m_hLogoDC, i, 0, SRCAND);
}

int yPos = m_cyData - m_nCounter - 100;

// copy logo with sinus effect in memory dc
if (m_nCounter > (rect.Height() - 30))
{
yPos = m_cyData - m_nCounter - 100;
}

// copy credits in memory dc
BitBlt(m_hMemDC, m_nMemDCStartCx, m_nCounter--, m_cxData, yPos, m_hCreditsDC, 0, 0, SRCAND);

if (m_nCounter< -m_cyData)
{
m_nCounter = rect.Height();
}

CDC *pDC = GetDC();

// and finally, copy memory bitmap to screen
BitBlt(pDC->m_hDC, 0, 0, rect.Width(), rect.Height(), m_hMemDC, 0, 0, SRCCOPY);

ReleaseDC(pDC);

// any calculations follows
m_xAngle = m_xAngle + (int)(m_nSpeed * 0.5); // rotate logo x
m_yAngle = m_yAngle + m_nSpeed * 2; // rotate logo y

// did full rotation ?
if (m_xAngle >= 360)
{
// reset angle
m_xAngle = 0;
}

// did full rotation ?
if (m_xAngle <= -180)
{
m_nSpeed = m_nSpeed * -1;
}

if (m_yAngle >= 360)
{
m_yAngle = 0;
}
}


/********************************************************************/
/* */
/* Function name : LoadPicture */
/* Description : Load picture from resource into device context */
/* */
/********************************************************************/
void CAboutCtrl::LoadPicture(int nResourceID, HDC &amt;hDestinationDC, int &amt;nWidth, int &amt;nHeight, HDC hDC)
{
HDC hMemDC;
HDC hdcCompatible;
HBITMAP hbmScreen;

if (nResourceID != 0)
{
// if resourceid is given, load bitmap
HBITMAP hPicture = LoadBitmap(AfxGetInstanceHandle(), MAKEINTRESOURCE(nResourceID));

BITMAP bm;
GetObject(hPicture, sizeof (BITMAP), (LPSTR)&amt;bm);

hMemDC = CreateCompatibleDC(hDC);

HBITMAP hOldBMP = (HBITMAP)SelectObject(hMemDC, hPicture);

nWidth = bm.bmWidth;
nHeight = bm.bmHeight;

// Create the DC
hdcCompatible = CreateCompatibleDC(hDC);
// Temporary memory bitmap
hbmScreen = CreateCompatibleBitmap(hDC, nWidth, nHeight);

// select bitmap into dc
if (SelectObject(hdcCompatible, hbmScreen) == NULL)
{
// return null
hDestinationDC = NULL;
}
else
{
// return the DC
hDestinationDC = hdcCompatible;
}

if (hDestinationDC)
BitBlt(hDestinationDC, 0, 0, nWidth, nHeight, hMemDC, 0, 0, SRCCOPY);

SelectObject(hMemDC, hOldBMP);

// Release temporary stuff
DeleteDC(hMemDC);
DeleteObject(hbmScreen);
DeleteObject(hPicture);
}
else // if no resourceid is given, create empty DC with specified width and height
{
// create the DC
hdcCompatible = CreateCompatibleDC(hDC);
// temporary memory bitmap
hbmScreen = CreateCompatibleBitmap(hDC, nWidth, nHeight);
// if the function fails
if (SelectObject(hdcCompatible, hbmScreen) == NULL)
{
// return null
hDestinationDC = NULL;
}
else
{
// if it succeeds, return the DC
hDestinationDC = hdcCompatible;
}
DeleteObject(hbmScreen);
}
}


/********************************************************************/
/* */
/* Function name : LoadCredits */
/* Description : Create credits picture into device context */
/* */
/********************************************************************/
void CAboutCtrl::LoadCredits(HDC &amt;hDestinationDC, int nWidth, int nHeight, HDC hDC)
{
HDC hdcCompatible;
HBITMAP hbmScreen;

// Create the DC
hdcCompatible = CreateCompatibleDC(hDC);
// Temporary bitmap
hbmScreen = CreateCompatibleBitmap(hDC, nWidth, nHeight);
// if the function fails
if (SelectObject(hdcCompatible, hbmScreen) == NULL)
{
// return null
hDC = NULL;
}
else
{
// if it succeeds, return the DC
hDestinationDC = hdcCompatible;
RECT rc;

rc.top = 0;
rc.left = 0;
rc.bottom = nHeight;
rc.right = nWidth;

HFONT pOldFont;
HFONT hFontTahoma, hFontBold, hFontNormal;
FillRect(hDestinationDC, &amt;rc, (HBRUSH)GetStockObject(WHITE_BRUSH));

// create a bunch of fonts
hFontTahoma = CreateFont(20, 0, 0, 0,
FW_BOLD, FALSE, FALSE, 0,
ANSI_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
PROOF_QUALITY,
VARIABLE_PITCH | 0x04 | FF_DONTCARE,
(LPSTR)"Arial");

hFontBold = CreateFont(14, 0, 0, 0,
FW_BOLD, FALSE, FALSE, 0,
ANSI_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
PROOF_QUALITY,
VARIABLE_PITCH | 0x04 | FF_DONTCARE,
(LPSTR)"Arial");

hFontNormal = CreateFont(14, 0, 0, 0,
FALSE, FALSE, FALSE, 0,
ANSI_CHARSET,
OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,
PROOF_QUALITY,
VARIABLE_PITCH | 0x04 | FF_DONTCARE,
(LPSTR)"Arial");


CString strSub;
int nCount=0;
// draw each line, based on specified type
while(AfxExtractSubString(strSub, m_strCredits, nCount++, '\n'))
{
TCHAR nType = 0;
COLORREF oldColor;

if (!strSub.IsEmpty())
nType = strSub.GetAt(0);

switch(nType)
{
case '\t': // title
oldColor = SetTextColor(hDestinationDC, RGB(16,140,231));
pOldFont = (HFONT)SelectObject(hDestinationDC, hFontTahoma);
strSub.TrimLeft('\t');
DrawText(hDestinationDC, strSub, strSub.GetLength(), &amt;rc, DT_TOP|DT_LEFT|DT_NOPREFIX | DT_SINGLELINE);
break;
case '\r': // bold
oldColor = SetTextColor(hDestinationDC, RGB(0,0,0));
pOldFont = (HFONT)SelectObject(hDestinationDC, hFontBold);
strSub.TrimLeft('\r');
DrawText(hDestinationDC, strSub, strSub.GetLength(), &amt;rc, DT_TOP|DT_LEFT|DT_NOPREFIX | DT_SINGLELINE);
break;
default: // normal
oldColor = SetTextColor(hDestinationDC, RGB(0,0,0));
pOldFont = (HFONT)SelectObject(hDestinationDC, hFontNormal);
DrawText(hDestinationDC, strSub, strSub.GetLength(), &amt;rc, DT_TOP|DT_LEFT|DT_NOPREFIX | DT_SINGLELINE);
break;
}
// next line
TEXTMETRIC tm;
GetTextMetrics(hDestinationDC, &amt;tm);
rc.top += tm.tmHeight;

// set back old values
SetTextColor(hDestinationDC, oldColor);
SelectObject(hDestinationDC, pOldFont);
}
// clean up
DeleteObject(hFontBold);
DeleteObject(hFontNormal);
DeleteObject(hFontTahoma);
}
DeleteObject(hbmScreen);
}


/********************************************************************/
/* */
/* Function name : SetCredits */
/* Description : Member function to set credits text,bmp */
/* */
/********************************************************************/
void CAboutCtrl::SetCredits(LPCTSTR lpszCredits,UINT nLogo,UINT nBkg,UINT nStartCx)
{
m_strCredits = lpszCredits;
m_nBmpLogo=nLogo;
m_nBmpBkg=nBkg;
m_nMemDCStartCx=nStartCx;
}

//***************************************************************
//函数: 单击关闭
// 1)
// 2)
//
//说明:
//***************************************************************
void CAboutCtrl::OnClicked()
{
// TODO: Add your control notification handler code here
CWnd *mWnd=this->GetParent();
mWnd->PostMessage(WM_CLOSE,0,0);
}