📄 textwnd.cpp
字号:
#include "stdafx.h"
#include "TextWnd.h"
//----------------------------------------------------------------------
//Macro define
#define DEFAULT_BKGND_COLOR RGB(128,128,128)
//For the text move
#define DEFAULT_INTERVAL 100 //500ms
#define DEFAULT_MOVE_PIXEL 1 //pixel
//The text information
#define DEFAULT_TEXT_COLOR RGB(255,255,255)
#define DEFAULT_TEXT_POINTSIZE 0
#define DEFAULT_TEXT_WEIGHT 0
//----------------------------------------------------------------------
//----------------------------------------------------------------------
//Description:
// Construction
//-----------------------------------------------------------------------
CTextWnd::CTextWnd(void):
m_iIndexCurTxt(0),
m_pszTxtInfo(NULL),
m_taCurAction(TA_NULL),
m_dwInterval(DEFAULT_INTERVAL),
m_hEventTimer(CreateEvent(NULL,FALSE,FALSE,NULL)),
m_dtValue(DRT_NULL),
m_iWndWidth(0),
m_iWndHeight(0),
m_iTxtInfoX(0),
m_iTxtInfoY(0),
m_iTxtInfoWidth(0),
m_iTxtInfoHeight(0),
m_iMovePixel(DEFAULT_MOVE_PIXEL),
m_iTxtInfoPointSize(DEFAULT_TEXT_POINTSIZE),
m_crTxtInfoColor(DEFAULT_TEXT_COLOR),
m_iTxtInfoWeight(DEFAULT_TEXT_WEIGHT),
m_bInited(FALSE),
m_crBkColor(DEFAULT_BKGND_COLOR)
{
//Create the timer thread to move the text
HANDLE hdThrd = CreateThread(NULL,NULL,TimerThread,this,NULL,NULL);
CloseHandle(hdThrd);
}
//----------------------------------------------------------------------
//Description:
// Destruction
//----------------------------------------------------------------------
CTextWnd::~CTextWnd(void)
{
if(m_hEventTimer != NULL)
{
CloseHandle(m_hEventTimer);
m_hEventTimer = NULL;
}
if(m_DCTxtInfo.IsOK() == TRUE)
{
m_DCTxtInfo.Delete();
}
}
//----------------------------------------------------------------------
//Description:
// Move the window
//----------------------------------------------------------------------
BOOL CTextWnd::Move(const RECT * prcWnd)
{
return MoveWindow(GetWindow(),
prcWnd->left,
prcWnd->top,
prcWnd->right - prcWnd->left,
prcWnd->bottom - prcWnd->top,
FALSE);
}
//----------------------------------------------------------------------
//Description:
// Set the text information.
// It would take effect in next calling Play().
//----------------------------------------------------------------------
BOOL CTextWnd::SetText(const TCHAR * pcszText)
{
if(m_pszTxtInfo != NULL)
{
delete [] m_pszTxtInfo;
m_pszTxtInfo = NULL;
}
m_pszTxtInfo = new TCHAR[_tcslen(pcszText) + 1];
_tcscpy(m_pszTxtInfo,pcszText);
//Set the flag to reinitialize the memory DC
m_bInited = FALSE;
return TRUE;
}
//----------------------------------------------------------------------
//Description:
// Set the text file path
//----------------------------------------------------------------------
BOOL CTextWnd::SetTxtPath(const TCHAR * pcszPath)
{
if(pcszPath == NULL)
{
return FALSE;
}
m_FileStore.DeleteAllData();
WIN32_FIND_DATA fd = {0};
HANDLE hFind = FindFirstFile(pcszPath,&fd);
if(hFind == INVALID_HANDLE_VALUE)
{
return FALSE;
}
if(fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
{
FindFile(pcszPath,&m_FileStore,CheckFile);
}
else
{
m_FileStore.Add(pcszPath);
}
FindClose(hFind);
//Open the first text file
m_iIndexCurTxt = 0;
ReadCurTxt();
return TRUE;
}
//----------------------------------------------------------------------
//Description:
// Play the text. If first using, it would be very slow bacause must initialize the memory DC.
// It would be quickly next time when the setting has not changed since first using
//----------------------------------------------------------------------
BOOL CTextWnd::Play(void)
{
if(m_DCTxtInfo.IsOK() == FALSE || m_bInited == FALSE)
{
//Create the memory DC for text information
InitDCTxtInfo();
m_bInited = TRUE;
}
m_taCurAction = TA_MOVE;
SetEvent(m_hEventTimer);
return TRUE;
}
//----------------------------------------------------------------------
//Description:
// Pause the text
//----------------------------------------------------------------------
BOOL CTextWnd::Pause(void)
{
m_taCurAction = TA_NULL;
SetEvent(m_hEventTimer);
return TRUE;
}
//----------------------------------------------------------------------
//Description:
// Check the file
//----------------------------------------------------------------------
BOOL CTextWnd::CheckFile(const TCHAR * pszFileName)
{
TCHAR szSuffix[MAX_PATH] = {0};
int iLen = _tcslen(pszFileName);
if(iLen == 0)
{
return FALSE;
}
int iPos = iLen - 1;
while(iPos >= 0)
{
if(pszFileName[iPos] == '.')
{
break;
}
iPos --;
}
if(iPos < 0)
{
return FALSE;
}
_tcscpy(szSuffix, pszFileName + iPos);
_tcslwr(szSuffix);
if(_tcscmp(szSuffix,TEXT(".txt")) == 0)
{
return TRUE;
}
return FALSE;
}
//----------------------------------------------------------------------
//Description:
// Read the text information base on the current index
//----------------------------------------------------------------------
BOOL CTextWnd::ReadCurTxt(void)
{
if(m_iIndexCurTxt >= m_FileStore.GetAmount() || m_iIndexCurTxt < 0 || m_FileStore.GetAmount() == 0)
{
return FALSE;
}
if(m_pszTxtInfo != NULL)
{
delete [] m_pszTxtInfo;
m_pszTxtInfo = NULL;
}
TCHAR szFile[MAX_PATH] = {0};
if(m_FileStore.GetData(m_iIndexCurTxt,szFile,MAX_PATH) == TRUE)
{
HANDLE hFile = CreateFile(szFile,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
DWORD dwSize = GetFileSize(hFile,NULL);
BYTE *pReadBuf = (BYTE*)malloc(dwSize);
DWORD dwRead = 0;
ReadFile(hFile,pReadBuf,dwSize,&dwRead,NULL);
#ifdef UNICODE
if(dwSize >= 2 && pReadBuf[0] == 0xFF && pReadBuf[1] == 0xFE)
{
//It must be the UNICODE file
DWORD dwNum = (dwSize - 2) / sizeof(TCHAR) + 1; //Must be end with '\0', so add 1
m_pszTxtInfo = new TCHAR[dwNum];
memset(m_pszTxtInfo,0,sizeof(TCHAR) * dwNum);
if(m_pszTxtInfo != NULL)
{
_tcscpy(m_pszTxtInfo,reinterpret_cast<TCHAR *>(pReadBuf + 2));
}
}
else
{
//It must be the ASCII file
DWORD dwNum = MultiByteToWideChar (CP_ACP, 0, reinterpret_cast<char *>(pReadBuf), -1, NULL, 0);
m_pszTxtInfo = new TCHAR[dwNum];
memset(m_pszTxtInfo,0,sizeof(TCHAR) * dwNum);
if(m_pszTxtInfo != NULL)
{
// Convert string from ASCII to Unicode.
MultiByteToWideChar (CP_ACP, 0, reinterpret_cast<char *>(pReadBuf), -1, m_pszTxtInfo, dwNum);
}
}
#else
#error "There is not the completed code here when the UNICODE macor is not defined. :-("
#endif
free(pReadBuf);
CloseHandle(hFile);
}
return TRUE;
}
//----------------------------------------------------------------------
//Description:
// Actual WndProc
//
//----------------------------------------------------------------------
LRESULT CTextWnd::WndProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
switch(wMsg)
{
case WM_PAINT:
OnPaint(hWnd,wMsg,wParam,lParam);
return 0;
case WM_ERASEBKGND:
return 0;
case WM_WINDOWPOSCHANGED:
OnWindowPosChanged(hWnd,wMsg,wParam,lParam);
break;
}
return CWndBase::WndProc(hWnd,wMsg,wParam,lParam);
}
//----------------------------------------------------------------------
//Description:
// On message WM_PAINT
//
//----------------------------------------------------------------------
void CTextWnd::OnPaint(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd,&ps);
//Create the memory DC
CMemDC memDC;
SIZE size = {m_iWndWidth,m_iWndHeight};
memDC.Create(hdc,&size);
DrawBackground(memDC.GetDC());
//Draw the text
if(m_DCTxtInfo.IsOK() == TRUE)
{
TransparentBlt(memDC.GetDC(),
m_iTxtInfoX,
m_iTxtInfoY,
m_iTxtInfoWidth,
m_iTxtInfoHeight,
m_DCTxtInfo.GetDC(),
0,
0,
m_DCTxtInfo.GetWidth(),
m_DCTxtInfo.GetHeight(),
m_crBkColor);
}
//Draw the device context
BitBlt(hdc,0,0,m_iWndWidth,m_iWndHeight,memDC.GetDC(),0,0,SRCCOPY);
//Delete the memory DC
memDC.Delete();
EndPaint(hWnd,&ps);
}
//----------------------------------------------------------------------
//Description:
// The timer thread
//
//----------------------------------------------------------------------
DWORD CTextWnd::TimerThread(PVOID pArg)
{
CTextWnd *pObject = (CTextWnd *)pArg;
while(TRUE)
{
if(WaitForSingleObject(pObject->m_hEventTimer,pObject->m_dwInterval) == WAIT_TIMEOUT)
{
if(pObject->m_taCurAction == TA_MOVE)
{
switch(pObject->m_dtValue)
{
case DRT_NULL:
{
//Do nothing
break;
}
case DRT_LEFT:
{
pObject->m_iTxtInfoX -= pObject->m_iMovePixel;
if(pObject->m_iTxtInfoX + pObject->m_iTxtInfoWidth <= 0)
{
pObject->m_iTxtInfoX = pObject->m_iWndWidth;
}
break;
}
case DRT_RIGHT:
{
pObject->m_iTxtInfoX += pObject->m_iMovePixel;
if(pObject->m_iTxtInfoX >= pObject->m_iWndWidth)
{
pObject->m_iTxtInfoX = - pObject->m_iTxtInfoWidth;
}
break;
}
case DRT_UP:
{
pObject->m_iTxtInfoY -= pObject->m_iMovePixel;
if(pObject->m_iTxtInfoY + pObject->m_iTxtInfoHeight <= 0)
{
pObject->m_iTxtInfoY = pObject->m_iWndHeight;
}
break;
}
case DRT_DOWN:
{
pObject->m_iTxtInfoY += pObject->m_iMovePixel;
if(pObject->m_iTxtInfoY >= pObject->m_iWndHeight)
{
pObject->m_iTxtInfoY = -pObject->m_iTxtInfoWidth;
}
break;
}
}
//Redraw the text information
InvalidateRect(pObject->GetWindow(),NULL,FALSE);
}
else if(pObject->m_taCurAction == TA_EXIT)
{
break;
}
else if(pObject->m_taCurAction == TA_STOP)
{
switch(pObject->m_dtValue)
{
case DRT_NULL:
{
//Do nothing
break;
}
case DRT_LEFT:
{
pObject->m_iTxtInfoX = pObject->m_iWndWidth;
break;
}
case DRT_RIGHT:
{
pObject->m_iTxtInfoX = - pObject->m_iTxtInfoWidth;
break;
}
case DRT_UP:
{
pObject->m_iTxtInfoY = pObject->m_iWndHeight;
break;
}
case DRT_DOWN:
{
pObject->m_iTxtInfoY = -pObject->m_iTxtInfoWidth;
break;
}
}
pObject->m_taCurAction = TA_NULL;
InvalidateRect(pObject->GetWindow(),NULL,FALSE);
}
}
}
return 0;
}
//----------------------------------------------------------------------
//Description:
// Set the direction for the text information displayed
//
//----------------------------------------------------------------------
void CTextWnd::SetDirection(DirectionValue dtValue)
{
if(m_dtValue == dtValue)
{
//Do nothing
return;
}
m_dtValue = dtValue;
}
//----------------------------------------------------------------------
//Description:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -