📄 rollupstatic.cpp
字号:
// RollupStatic.cpp : implementation file
//
#include "stdafx.h"
#include "StaticRollupText.h"
#include "RollupStatic.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define DISPLAY_TIMER_ID 150 // timer id
/////////////////////////////////////////////////////////////////////////////
// CRollupStatic
CRollupStatic::CRollupStatic()
{
m_Colors[0] = RGB(0,0,0); // Black
m_Colors[1] = RGB(230,230,0); // Red,修改标题字颜色, jingzhou xu
m_Colors[2] = RGB(255,255,0); // Yellow
m_Colors[3] = RGB(0,255,255); // Turquoise
m_Colors[4] = RGB(255,255,255); // White
m_TextHeights[0] = 21;
m_TextHeights[1] = 19;
m_TextHeights[2] = 17;
m_TextHeights[3] = 15;
m_nCurrentFontHeight = m_TextHeights[NORMAL_TEXT_HEIGHT];
m_Escapes[0] = '\t';
m_Escapes[1] = '\n';
m_Escapes[2] = '\r';
m_Escapes[3] = '^';
m_DisplaySpeed[0] = 70;
m_DisplaySpeed[1] = 40;
m_DisplaySpeed[2] = 10;
m_nCurrentSpeed = 1;
m_ScrollAmount = -1;
m_bProcessingBitmap = FALSE;
m_ArrIndex = NULL;
m_nCounter = 1;
m_nClip = 0;
m_bFirstTime = TRUE;
m_bDrawText = FALSE;
m_bFirstTurn = TRUE;
m_Gradient = GRADIENT_NONE;
m_bTransparent = FALSE;
m_nMaxWidth = 0;
TimerOn = 0;
}
CRollupStatic::~CRollupStatic()
{
}
BEGIN_MESSAGE_MAP(CRollupStatic, CStatic)
//{{AFX_MSG_MAP(CRollupStatic)
ON_WM_TIMER()
ON_WM_ERASEBKGND()
ON_WM_DESTROY()
ON_WM_PAINT()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CRollupStatic message handlers
BOOL CRollupStatic::StartScrolling()
{
if (m_ArrCredit.IsEmpty())
return FALSE;
if (m_BmpMain.m_hObject != NULL) {
m_BmpMain.DeleteObject();
m_BmpMain.m_hObject = NULL;
}
TimerOn = SetTimer(DISPLAY_TIMER_ID,
m_DisplaySpeed[m_nCurrentSpeed],
NULL);
ASSERT(TimerOn != 0);
m_ArrIndex = m_ArrCredit.GetHeadPosition();
m_nCounter = 0;
m_nClip = 0;
m_bFirstTime = TRUE;
m_bDrawText = FALSE;
return TRUE;
}
void CRollupStatic::EndScrolling()
{
KillTimer(DISPLAY_TIMER_ID);
TimerOn = 0;
if (m_BmpMain.m_hObject != NULL)
{
m_BmpMain.DeleteObject();
m_BmpMain.m_hObject = NULL;
}
}
void CRollupStatic::SetRollupText(LPCTSTR lpRollupText,char delimiter)
{
char *str;
char *ptr1;
char *ptr2;
ASSERT(lpRollupText);
if ((str = strdup(lpRollupText)) == NULL)
{
return;
}
m_ArrCredit.RemoveAll();
ptr1 = str;
while ((ptr2 = strchr(ptr1,delimiter)) != NULL)
{
*ptr2 = '\0';
m_ArrCredit.AddTail(ptr1);
ptr1 = ptr2+1;
}
m_ArrCredit.AddTail(ptr1);
free(str);
m_ArrIndex = m_ArrCredit.GetHeadPosition();
m_nCounter = 1;
m_nClip = 0;
m_bFirstTime = TRUE;
m_bDrawText = FALSE;
}
void CRollupStatic::SetRollupText(UINT nID,char delimiter)
{
CString csRollupText = _T("");
if (!csRollupText.LoadString(nID))
{
return;
}
SetRollupText((LPCTSTR)csRollupText, delimiter);
}
void CRollupStatic::SetSpeed(UINT index, int speed)
{
ASSERT(index <= DISPLAY_FAST);
if (speed)
{
m_DisplaySpeed[index] = speed;
}
m_nCurrentSpeed = index;
}
void CRollupStatic::SetColor(UINT index, COLORREF col)
{
ASSERT(index <= NORMAL_TEXT_COLOR);
m_Colors[index] = col;
}
void CRollupStatic::SetTextHeight(UINT index, int height)
{
ASSERT(index <= NORMAL_TEXT_HEIGHT);
m_TextHeights[index] = height;
}
void CRollupStatic::SetEscape(UINT index, char escape)
{
ASSERT(index <= DISPLAY_BITMAP);
m_Escapes[index] = escape;
}
void CRollupStatic::SetGradient(UINT value)
{
ASSERT(value <= GRADIENT_LEFT_LIGHT);
m_Gradient = value;
}
void CRollupStatic::SetTransparent(BOOL bTransparent)
{
m_bTransparent = bTransparent;
}
void CRollupStatic::OnTimer(UINT nIDEvent)
{
if (nIDEvent != DISPLAY_TIMER_ID)
{
CStatic::OnTimer(nIDEvent);
return;
}
BOOL bCheck = FALSE;
if (!m_bProcessingBitmap)
{
if (m_nCounter++ % m_nCurrentFontHeight == 0)
{
m_nCounter=1;
m_szWork = m_ArrCredit.GetNext(m_ArrIndex);
if (m_bFirstTurn)
{
bCheck = TRUE;
}
if (m_ArrIndex == NULL)
{
m_bFirstTurn = FALSE;
m_ArrIndex = m_ArrCredit.GetHeadPosition();
}
m_nClip = 0;
m_bDrawText=TRUE;
}
}
CClientDC dc(this);
CRect m_ScrollRect;
GetClientRect(&m_ScrollRect);
CRect m_ClientRect(m_ScrollRect);
m_ClientRect.left = (m_ClientRect.Width() - m_nMaxWidth)/2;
m_ClientRect.right = m_ClientRect.left + m_nMaxWidth;
MoveCredit(&dc, m_ScrollRect, m_ClientRect, bCheck);
AddBackGround(&dc, m_ScrollRect, m_ClientRect);
CStatic::OnTimer(nIDEvent);
}
void CRollupStatic::AddBackGround(CDC* pDC,
CRect& m_ScrollRect,
CRect& m_ClientRect)
{
CDC memDC;
memDC.CreateCompatibleDC( pDC );
if( m_bitmap.m_hObject == NULL )
{
CBitmap* pOldBitmap = memDC.SelectObject( &m_BmpMain );
pDC->BitBlt( 0, 0, m_ScrollRect.Width(), m_ScrollRect.Height(),
&memDC, 0, 0, SRCCOPY );
memDC.SelectObject(pOldBitmap);
return;
}
// Draw bitmap in the background if one has been set
// Now create a mask
CBitmap bitmap;
bitmap.CreateCompatibleBitmap( pDC, m_ClientRect.Width(),
m_ClientRect.Height() );
CBitmap* pOldMemDCBitmap = memDC.SelectObject( &bitmap );
CDC tempDC;
tempDC.CreateCompatibleDC(pDC);
CBitmap* pOldTempDCBitmap = tempDC.SelectObject( &m_BmpMain );
memDC.BitBlt( 0, 0, m_ClientRect.Width(), m_ClientRect.Height(), &tempDC,
m_ClientRect.left, m_ClientRect.top, SRCCOPY );
CDC maskDC;
maskDC.CreateCompatibleDC(pDC);
CBitmap maskBitmap;
// Create monochrome bitmap for the mask
maskBitmap.CreateBitmap( m_ClientRect.Width(), m_ClientRect.Height(),
1, 1, NULL );
CBitmap* pOldMaskDCBitmap = maskDC.SelectObject( &maskBitmap );
memDC.SetBkColor(m_bTransparent? RGB(192,192,192): m_Colors[BACKGROUND_COLOR]);
// Create the mask from the memory DC
maskDC.BitBlt( 0, 0, m_ClientRect.Width(), m_ClientRect.Height(),
&memDC, 0, 0, SRCCOPY );
tempDC.SelectObject(pOldTempDCBitmap);
pOldTempDCBitmap = tempDC.SelectObject( &m_bitmap );
CDC imageDC;
CBitmap bmpImage;
imageDC.CreateCompatibleDC( pDC );
bmpImage.CreateCompatibleBitmap( pDC, m_ScrollRect.Width(), m_ScrollRect.Height() );
CBitmap* pOldImageDCBitmap = imageDC.SelectObject( &bmpImage );
if ( pDC->GetDeviceCaps(RASTERCAPS) & RC_PALETTE && m_pal.m_hObject != NULL )
{
pDC->SelectPalette( &m_pal, FALSE );
pDC->RealizePalette();
imageDC.SelectPalette( &m_pal, FALSE );
}
// Get x and y offset
// Draw bitmap in tiled manner to imageDC
for ( int i = 0; i < m_ScrollRect.right; i += m_cxBitmap )
for( int j = 0; j < m_ScrollRect.bottom; j += m_cyBitmap )
imageDC.BitBlt( i, j, m_cxBitmap, m_cyBitmap, &tempDC, 0, 0, SRCCOPY );
// Set the background in memDC to black. Using SRCPAINT with black and any other
// color results in the other color, thus making black the transparent color
memDC.SetBkColor(RGB(0,0,0));
memDC.SetTextColor(RGB(255,255,255));
memDC.BitBlt(0, 0, m_ClientRect.Width(), m_ClientRect.Height(), &maskDC, 0, 0, SRCAND);
// Set the foreground to black. See comment above.
imageDC.SetBkColor(RGB(255,255,255));
imageDC.SetTextColor(RGB(0,0,0));
imageDC.BitBlt(m_ClientRect.left, m_ClientRect.top,
m_ClientRect.Width(), m_ClientRect.Height(),
&maskDC, 0, 0, SRCAND);
// Combine the foreground with the background
imageDC.BitBlt(m_ClientRect.left, m_ClientRect.top, m_ClientRect.Width(),
m_ClientRect.Height(),
&memDC, 0, 0,SRCPAINT);
// Draw the final image to the screen
pDC->BitBlt( 0, 0, m_ScrollRect.Width(), m_ScrollRect.Height(),
&imageDC, 0, 0, SRCCOPY );
imageDC.SelectObject(pOldImageDCBitmap);
maskDC.SelectObject(pOldMaskDCBitmap);
tempDC.SelectObject(pOldTempDCBitmap);
memDC.SelectObject(pOldMemDCBitmap);
}
void CRollupStatic::DrawBitmap(CDC* pDC, CDC* pDC2, CRect *rBitmap)
{
if(!m_bTransparent || m_bitmap.m_hObject != NULL)
{
pDC->BitBlt( rBitmap->left, rBitmap->top,
rBitmap->Width(), rBitmap->Height(),
pDC2, 0, 0, SRCCOPY );
return;
}
CDC memDC;
memDC.CreateCompatibleDC( pDC );
// Now create a mask
CBitmap bitmap;
bitmap.CreateCompatibleBitmap( pDC, rBitmap->Width(), rBitmap->Height() );
CBitmap* pOldMemDCBitmap = memDC.SelectObject( &bitmap );
memDC.BitBlt( 0, 0, rBitmap->Width(), rBitmap->Height(), pDC2, 0, 0, SRCCOPY );
CDC maskDC;
maskDC.CreateCompatibleDC(pDC);
// Create monochrome bitmap for the mask
CBitmap maskBitmap;
maskBitmap.CreateBitmap( rBitmap->Width(), rBitmap->Height(), 1, 1, NULL );
CBitmap* pOldMaskDCBitmap = maskDC.SelectObject( &maskBitmap );
memDC.SetBkColor(RGB(192,192,192));
// Create the mask from the memory DC
maskDC.BitBlt( 0, 0, rBitmap->Width(), rBitmap->Height(), &memDC, 0, 0, SRCCOPY );
CDC imageDC;
CBitmap bmpImage;
imageDC.CreateCompatibleDC( pDC );
bmpImage.CreateCompatibleBitmap( pDC, rBitmap->Width(), rBitmap->Height() );
CBitmap* pOldImageDCBitmap = imageDC.SelectObject( &bmpImage );
imageDC.BitBlt(0, 0, rBitmap->Width(), rBitmap->Height(), pDC,
rBitmap->left, rBitmap->top, SRCCOPY);
// Set the background in memDC to black. Using SRCPAINT with black and any other
// color results in the other color, thus making black the transparent color
memDC.SetBkColor(RGB(0,0,0));
memDC.SetTextColor(RGB(255,255,255));
memDC.BitBlt(0, 0, rBitmap->Width(), rBitmap->Height(), &maskDC, 0, 0, SRCAND);
// Set the foreground to black. See comment above.
imageDC.SetBkColor(RGB(255,255,255));
imageDC.SetTextColor(RGB(0,0,0));
imageDC.BitBlt(0, 0, rBitmap->Width(), rBitmap->Height(), &maskDC, 0, 0, SRCAND);
// Combine the foreground with the background
imageDC.BitBlt(0, 0, rBitmap->Width(), rBitmap->Height(), &memDC, 0, 0,SRCPAINT);
// Draw the final image to the screen
pDC->BitBlt( rBitmap->left, rBitmap->top, rBitmap->Width(), rBitmap->Height(),
&imageDC, 0, 0, SRCCOPY );
imageDC.SelectObject(pOldImageDCBitmap);
maskDC.SelectObject(pOldMaskDCBitmap);
memDC.SelectObject(pOldMemDCBitmap);
}
void CRollupStatic::FillGradient(CDC *pDC, CRect *m_ScrollRect,
CRect *m_FillRect, COLORREF color)
{
float fStep,fRStep,fGStep,fBStep; // How large is each band?
int iOnBand; // Loop index
WORD R = GetRValue(color);
WORD G = GetGValue(color);
WORD B = GetBValue(color);
// Determine how large each band should be in order to cover the
// client with 256 bands (one for every color intensity level)
if (m_Gradient % 2)
{
fRStep = (float)R / 255.0f;
fGStep = (float)G / 255.0f;
fBStep = (float)B / 255.0f;
}
else
{
fRStep = (float)(255-R) / 255.0f;
fGStep = (float)(255-G) / 255.0f;
fBStep = (float)(255-B) / 255.0f;
}
COLORREF OldCol = pDC->GetBkColor();
// Start filling bands
fStep = (float)m_ScrollRect->Width() / 256.0f;
for (iOnBand = (256*m_FillRect->left)/m_ScrollRect->Width();
(int)(iOnBand*fStep) < m_FillRect->right && iOnBand < 256; iOnBand++)
{
CRect r((int)(iOnBand * fStep), m_FillRect->top,
(int)((iOnBand+1) * fStep), m_FillRect->bottom+1);
COLORREF col;
switch(m_Gradient) {
case GRADIENT_RIGHT_DARK:
col = RGB((int)(R-iOnBand*fRStep),(int)(G-iOnBand*fGStep),
(int)(B-iOnBand*fBStep));
break;
case GRADIENT_RIGHT_LIGHT:
col = RGB((int)(R+iOnBand*fRStep),(int)(G+iOnBand*fGStep),
(int)(B+iOnBand*fBStep));
break;
case GRADIENT_LEFT_DARK:
col = RGB((int)(iOnBand*fRStep),(int)(iOnBand*fGStep),
(int)(iOnBand*fBStep));
break;
case GRADIENT_LEFT_LIGHT:
col = RGB(255-(int)(iOnBand*fRStep),255-(int)(iOnBand*fGStep),
255-(int)(iOnBand*fBStep));
break;
default:
return;
}
pDC->FillSolidRect(&r, col);
}
pDC->SetBkColor(OldCol);
}
#define SCROLLDC
void CRollupStatic::MoveCredit(CDC* pDC, CRect& m_ScrollRect,
CRect& m_ClientRect, BOOL bCheck)
{
CDC memDC,memDC2;
memDC.CreateCompatibleDC(pDC);
memDC2.CreateCompatibleDC(pDC);
COLORREF BackColor = (m_bTransparent && m_bitmap.m_hObject != NULL)? RGB(192,192,192) : m_Colors[BACKGROUND_COLOR];
CBitmap *pOldMemDCBitmap = NULL;
CBitmap *pOldMemDC2Bitmap = NULL;
#ifdef SCROLLDC
CRect r1;
#endif
if(m_BmpMain.m_hObject == NULL) {
m_BmpMain.CreateCompatibleBitmap( pDC, m_ScrollRect.Width(),
m_ScrollRect.Height() );
pOldMemDCBitmap = (CBitmap*)memDC.SelectObject(&m_BmpMain);
if(m_Gradient && m_bitmap.m_hObject == NULL)
FillGradient(&memDC, &m_ScrollRect, &m_ScrollRect,
m_Colors[BACKGROUND_COLOR]);
else
memDC.FillSolidRect(&m_ScrollRect,BackColor);
}
else
pOldMemDCBitmap = (CBitmap*)memDC.SelectObject(&m_BmpMain);
if(m_ClientRect.Width() > 0)
{
#ifndef SCROLLDC
CBitmap bitmap;
bitmap.CreateCompatibleBitmap( pDC, m_ClientRect.Width(), m_ClientRect.Height() );
pOldMemDC2Bitmap = memDC2.SelectObject(&bitmap);
memDC2.BitBlt( 0, 0, m_ClientRect.Width(), m_ClientRect.Height()-abs(m_ScrollAmount),
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -