📄 textwnd.cpp
字号:
// Set the interval for text moving
//
//----------------------------------------------------------------------
void CTextWnd::SetInterval(DWORD dwInterval)
{
m_dwInterval = dwInterval;
}
//----------------------------------------------------------------------
//Description:
// Create the window
//
//Parameters:
// You should see the explanation of the parameters in CWndBase
//
//----------------------------------------------------------------------
BOOL CTextWnd::Create(HINSTANCE hInst, HWND hWndParent, const TCHAR *pcszWndClass, const TCHAR *pcszWndName,BOOL bMsgThrdInside)
{
if(CWndBase::Create(hInst,hWndParent,pcszWndClass,pcszWndName,bMsgThrdInside) == FALSE)
{
return FALSE;
}
//Get the window area
RECT rcWnd = {0};
GetWindowRect(GetWindow(),&rcWnd);
m_iWndWidth = rcWnd.right - rcWnd.left;
m_iWndHeight = rcWnd.bottom - rcWnd.top;
return TRUE;
}
//----------------------------------------------------------------------
//Description:
// On message WM_WINDOWPOSCHANGED
//
//----------------------------------------------------------------------
void CTextWnd::OnWindowPosChanged(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
{
//Get the window area
WINDOWPOS wp = *((WINDOWPOS *) lParam);
if(m_iWndWidth != wp.cx || m_iWndHeight != wp.cy)
{
m_iWndWidth = wp.cx;
m_iWndHeight = wp.cy;
//Set the flag to reinitialize the memory DC
m_bInited = FALSE;
}
}
//----------------------------------------------------------------------
//Description:
// Caculate the text informat rectangle.
//
//----------------------------------------------------------------------
BOOL CTextWnd::ResetTxtInfoRect(void)
{
HWND hWnd = GetWindow();
if(hWnd == NULL || m_pszTxtInfo == NULL)
{
return FALSE;
}
HDC hdc = GetDC(hWnd);
//Set the font to caculate
LOGFONT lf = {0};
HFONT hFontNew, hFontOld;
lf.lfQuality = CLEARTYPE_QUALITY;
lf.lfCharSet = DEFAULT_CHARSET;
lf.lfHeight = -1 * (m_iTxtInfoPointSize * GetDeviceCaps(hdc,LOGPIXELSY) / 72);
lf.lfWeight = m_iTxtInfoWeight;
//Create the new font
hFontNew = CreateFontIndirect(&lf);
hFontOld = (HFONT) SelectObject(hdc, hFontNew);
//Get the size of the string
SIZE size = {0};
GetTextExtentPoint(hdc,m_pszTxtInfo,_tcslen(m_pszTxtInfo),&size);
//Restore the font
SelectObject(hdc, hFontOld);
DeleteObject(hFontNew);
ReleaseDC(hWnd,hdc);
if(m_dtValue == DRT_NULL)
{
m_iTxtInfoWidth = m_iWndWidth;
m_iTxtInfoHeight = m_iWndHeight;
}
else if(m_dtValue == DRT_LEFT || m_dtValue == DRT_RIGHT)
{
m_iTxtInfoWidth = size.cx;
m_iTxtInfoHeight = size.cy;
}
else if(m_dtValue == DRT_UP || m_dtValue == DRT_DOWN)
{
//Get the amount of the "\n"
TCHAR *pFind = m_pszTxtInfo;
int iCountLine = 1;
while(TRUE)
{
pFind = _tcsstr(pFind,TEXT("\n"));
if(pFind != NULL)
{
pFind += 1;
iCountLine ++;
}
else
{
break;
}
}
m_iTxtInfoWidth = m_iWndWidth;
m_iTxtInfoHeight = size.cy * iCountLine;
}
//The begin position of X and Y
switch(m_dtValue)
{
case DRT_NULL:
m_iTxtInfoX = (m_iWndWidth - m_iTxtInfoWidth) / 2;
m_iTxtInfoY = (m_iWndHeight - m_iTxtInfoHeight) / 2;
break;
case DRT_LEFT:
m_iTxtInfoX = m_iWndWidth;
m_iTxtInfoY = (m_iWndHeight - m_iTxtInfoHeight) / 2;
break;
case DRT_RIGHT:
m_iTxtInfoX = -m_iTxtInfoWidth;
m_iTxtInfoY = (m_iWndHeight - m_iTxtInfoHeight) / 2;
break;
case DRT_UP:
m_iTxtInfoX = (m_iWndWidth - m_iTxtInfoWidth) / 2;
m_iTxtInfoY = m_iWndHeight;
break;
case DRT_DOWN:
m_iTxtInfoX = (m_iWndWidth - m_iTxtInfoWidth) / 2;
m_iTxtInfoY = -m_iTxtInfoHeight;
break;
}
return TRUE;
}
//----------------------------------------------------------------------
//Description:
// Draw the background
//
//----------------------------------------------------------------------
void CTextWnd::DrawBackground(HDC hdc)
{
//Create the pen
HPEN hPen = CreatePen(PS_SOLID,1,m_crBkColor);
HPEN hOldPen = NULL;
hOldPen = (HPEN)SelectObject(hdc,hPen);
//the rect color
HBRUSH hBrush = CreateSolidBrush(m_crBkColor);
HGDIOBJ hOldBrush = SelectObject(hdc,hBrush);
//Draw
Rectangle(hdc,0,0,m_iWndWidth + 1, m_iWndHeight + 1);
//Realse the resource
SelectObject(hdc,hOldBrush);
DeleteObject(hBrush);
SelectObject(hdc,hOldPen);
DeleteObject(hPen);
}
//----------------------------------------------------------------------
//Description:
// Initialize the memory DC for storing the current text information
//
//----------------------------------------------------------------------
BOOL CTextWnd::InitDCTxtInfo(void)
{
if(ResetTxtInfoRect() == FALSE)
{
return FALSE;
}
HDC hdc = GetDC(GetWindow());
//If existing the memory DC, delete it.
m_DCTxtInfo.Delete();
//Create the memory DC
SIZE size = {m_iTxtInfoWidth,m_iTxtInfoHeight};
m_DCTxtInfo.Create(hdc,&size);
//Draw the background with the transparent color
HPEN hPen = CreatePen(PS_SOLID,1,m_crBkColor);
HPEN hOldPen = NULL;
hOldPen = (HPEN)SelectObject(m_DCTxtInfo.GetDC(),hPen);
//the rect color
HBRUSH hBrush = CreateSolidBrush(m_crBkColor);
HGDIOBJ hOldBrush = SelectObject(m_DCTxtInfo.GetDC(),hBrush);
//Draw
Rectangle(m_DCTxtInfo.GetDC(),0,0,m_iTxtInfoWidth + 1, m_iTxtInfoHeight + 1);
//Realse the resource
SelectObject(m_DCTxtInfo.GetDC(),hOldBrush);
DeleteObject(hBrush);
SelectObject(m_DCTxtInfo.GetDC(),hOldPen);
DeleteObject(hPen);
//Set the background mode
::SetBkMode(m_DCTxtInfo.GetDC(),TRANSPARENT);
//Draw the text
RECT rcDraw = {0, 0, m_iTxtInfoWidth, m_iTxtInfoHeight};
UINT uFormat = 0;
switch(m_dtValue)
{
case DRT_NULL:
uFormat = DT_LEFT | DT_TOP | DT_WORDBREAK;
break;
case DRT_LEFT:
uFormat = DT_VCENTER | DT_SINGLELINE | DT_LEFT;
break;
case DRT_RIGHT:
uFormat = DT_VCENTER | DT_SINGLELINE | DT_RIGHT;
break;
case DRT_UP:
case DRT_DOWN:
uFormat = DT_CENTER | DT_WORDBREAK;
break;
}
//Set the text color
COLORREF crOldTextColor = ::SetTextColor(m_DCTxtInfo.GetDC(),m_crTxtInfoColor);
//Set the font to caculate
LOGFONT lf = {0};
HFONT hFontNew, hFontOld;
lf.lfQuality = CLEARTYPE_QUALITY;
lf.lfCharSet = DEFAULT_CHARSET;
lf.lfHeight = -1 * (m_iTxtInfoPointSize * GetDeviceCaps(m_DCTxtInfo.GetDC(),LOGPIXELSY) / 72);
lf.lfWeight = m_iTxtInfoWeight;
//Create the new font
hFontNew = CreateFontIndirect(&lf);
hFontOld = (HFONT) SelectObject(m_DCTxtInfo.GetDC(), hFontNew);
if(m_dtValue == DRT_RIGHT)
{
//Reverse the string
int iLen = _tcslen(m_pszTxtInfo);
TCHAR *pszReverse = new TCHAR[iLen + 1];
for(int i = 0; i < iLen; i ++)
{
pszReverse[i] = m_pszTxtInfo[iLen - i - 1];
}
pszReverse[iLen] = '\0';
DrawText(m_DCTxtInfo.GetDC(),pszReverse,-1,&rcDraw,uFormat);
delete []pszReverse;
}
else
{
DrawText(m_DCTxtInfo.GetDC(),m_pszTxtInfo,-1,&rcDraw,uFormat);
}
//Restore the font
SelectObject(m_DCTxtInfo.GetDC(), hFontOld);
DeleteObject(hFontNew);
//Restore the color
::SetTextColor(m_DCTxtInfo.GetDC(),crOldTextColor);
ReleaseDC(GetWindow(),hdc);
return TRUE;
}
//----------------------------------------------------------------------
//Description:
// Set the move distance once, base on pixel.
//
//----------------------------------------------------------------------
void CTextWnd::SetMovePixel(int iPixel)
{
m_iMovePixel = iPixel;
}
//----------------------------------------------------------------------
//Description:
// Set the text color. Don't set the same color with the background.
// It would take effect in next calling Play().
//
//----------------------------------------------------------------------
void CTextWnd::SetTxtColor(COLORREF crColor)
{
m_crTxtInfoColor = crColor;
//Set the flag to reinitialize the memory DC
m_bInited = FALSE;
}
//----------------------------------------------------------------------
//Description:
// Set the text point size.
// It would take effect in next calling Play().
//
//----------------------------------------------------------------------
void CTextWnd::SetTxtPointSize(int iPointSize)
{
m_iTxtInfoPointSize = iPointSize;
//Set the flag to reinitialize the memory DC
m_bInited = FALSE;
}
//----------------------------------------------------------------------
//Description:
// Set the text weight. See LOGFONT for a list of value.
// It would take effect in next calling Play().
//
//----------------------------------------------------------------------
void CTextWnd::SetTxtWeight(int iWeight)
{
m_iTxtInfoWeight = iWeight;
//Set the flag to reinitialize the memory DC
m_bInited = FALSE;
}
//----------------------------------------------------------------------
//Description:
// Set the color for the background of window
//
//----------------------------------------------------------------------
void CTextWnd::SetBkColor(COLORREF crColor)
{
m_crBkColor = crColor;
}
//----------------------------------------------------------------------
//Description:
// Switch to next image
//----------------------------------------------------------------------
BOOL CTextWnd::SwitchNext(void)
{
if(m_FileStore.GetAmount() == 0)
{
return FALSE;
}
m_iIndexCurTxt ++;
if(m_iIndexCurTxt >= m_FileStore.GetAmount())
{
m_iIndexCurTxt = 0;
}
ReadCurTxt();
return TRUE;
}
//----------------------------------------------------------------------
//Description:
// Switch to previous image
//----------------------------------------------------------------------
BOOL CTextWnd::SwitchPrevious(void)
{
if(m_FileStore.GetAmount() == 0)
{
return FALSE;
}
m_iIndexCurTxt --;
if(m_iIndexCurTxt < 0 )
{
m_iIndexCurTxt = m_FileStore.GetAmount() - 1;
}
ReadCurTxt();
return TRUE;
}
//---------------------------------------------------------------------
//Description:
// Find the file in the specified directory
//
//Parameters:
// pszPath : [in] The path to find file
// pStore : [in] The buffer for storing the file path
// pCheckFunc : [in] Pointer to the function for checking the file
//---------------------------------------------------------------------
BOOL CTextWnd::FindFile(const TCHAR *pszPath,CStrStore *pStore,BOOL (*pCheckFunc)(const TCHAR *pcszPath))
{
if(_tcslen(pszPath) >= MAX_PATH || pszPath == NULL || pStore == NULL || pCheckFunc == NULL)
{
return FALSE;
}
TCHAR szFindDir[MAX_PATH] = {0};
_tcscpy(szFindDir,pszPath);
//the last 4 elememts must be like as "\*.*"
ULONG ulLen = _tcslen(szFindDir);
if(ulLen != 0 && szFindDir[ulLen - 1]=='\\')
{
_tcscat(szFindDir,TEXT("*.*"));
}
else
{
_tcscat(szFindDir,TEXT("\\*.*"));
}
WIN32_FIND_DATA fd;
HANDLE hFind;
hFind=FindFirstFile(szFindDir,&fd);
if(hFind != INVALID_HANDLE_VALUE)
{
do{
if(fd.dwFileAttributes==FILE_ATTRIBUTE_DIRECTORY)
{
//it must be directory
TCHAR szNextDir[MAX_PATH] = {0};
_tcscpy(szNextDir,pszPath);
ULONG ulLenNext = _tcslen(pszPath);
if(ulLenNext != 0 && szNextDir[ulLenNext - 1] != '\\')
{
_tcscat(szNextDir,TEXT("\\"));
}
_tcscat(szNextDir,fd.cFileName);
FindFile(szNextDir,pStore,pCheckFunc);
}
else
{
//it is file
if((*pCheckFunc)(fd.cFileName) == TRUE)
{
TCHAR szPathFile[MAX_PATH] = {0};
_tcscpy(szPathFile,pszPath);
ULONG ulLenFile = _tcslen(szPathFile);
if(ulLenFile != 0 && szPathFile[ulLenFile - 1] != '\\')
{
_tcscat(szPathFile,TEXT("\\"));
}
_tcscat(szPathFile,fd.cFileName);
pStore->Add(szPathFile);
}
}
}while(FindNextFile(hFind,&fd));
}
FindClose(hFind);
return TRUE;
}
//---------------------------------------------------------------------
//Description:
// Stop playing
//
//---------------------------------------------------------------------
BOOL CTextWnd::Stop(void)
{
m_taCurAction = TA_STOP;
SetEvent(m_hEventTimer);
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -