⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 skillbookview.cpp

📁 vc++的技巧查找方式源代码。
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		FormatTextLines(crCurSel.cpMin, crCurSel.cpMax);
		if (crCurSel.cpMin > m_crOldSel.cpMin) // move after
			FormatTextLines(m_crOldSel.cpMin, m_crOldSel.cpMin);
		else // move before
			FormatTextLines(m_crOldSel.cpMax, m_crOldSel.cpMax);
		break;
	default:
		FormatAll();
		break;
	}

	//undo action does not call OnProtected, so make it default
	m_changeType = ctUndo;
}

void CSkillBookView::FormatTextLines(int nLineStart, int nLineEnd)
{
	long nStart = GetRichEditCtrl().LineIndex(GetRichEditCtrl().LineFromChar(nLineStart));
	long nEnd = GetRichEditCtrl().LineIndex(GetRichEditCtrl().LineFromChar(nLineEnd));
//	nEnd += GetRichEditCtrl().LineLength(nLineEnd);
	//new code
	nEnd = GetRichEditCtrl().GetWindowTextLength();
	FormatTextRange(nStart, nEnd, m_bComment);
}

void CSkillBookView::FormatTextRange(int nStart, int nEnd, BOOL &bComment)
{
	if(nStart >= nEnd)
	{
		return;
	}

	m_bInForcedChange = TRUE;

	CHARRANGE crOldSel;

	GetRichEditCtrl().GetSel(crOldSel);
	LockWindowUpdate();
	GetRichEditCtrl().HideSelection(TRUE, FALSE);

	try
	{
		GetRichEditCtrl().SetSel(nStart, nEnd);
		TCHAR *pBuffer = NULL;
		long nLen = GetRichEditCtrl().GetWindowTextLength();
		pBuffer = new TCHAR[nLen + 1];

		GetRichEditCtrl().GetSelText(pBuffer);
		pBuffer[nLen] = 0;
		
		TCHAR *pStart, *pPtr;
		pStart = pPtr = pBuffer;

		TCHAR* pSymbolStart = NULL;
		SYMBOLCOLOR ic;

		while(*pPtr != 0)
		{
			TCHAR ch = *pPtr;

			if(bComment)
			{
				int nCommentFlag = 0;

				pSymbolStart = pPtr;
				do
				{
					ch = *(++pPtr);

					if(nCommentFlag == 1)
					{
						if(ch == '/')
						{
							nCommentFlag = 2;
							bComment = FALSE;
						}
						else if(ch == '*')
						{
							nCommentFlag = 1;
						}
						else
						{
							nCommentFlag = 0;
						}
					}
					else if(nCommentFlag == 2)
					{
						nCommentFlag = 3;
					}
					else
					{
						if(ch == '*')
						{
							nCommentFlag = 1;
						}
						else
						{
							nCommentFlag = 0;
						}
					}
				}while (ch != 0 && ch != '\r' && nCommentFlag != 3);
				ic = m_icComment;
			}
			else
			{
				if(ch == '/' && (pPtr[1] == '/' || pPtr[1] == '*'))
				{ 
					if(pPtr[1] == '/')
					{
						pSymbolStart = pPtr;
						do
						{
							ch = *(++pPtr);
						} while (ch != 0 && ch != '\r');
						
						ic = m_icComment;
					}
					else if(pPtr[1] == '*')
					{
						bComment = TRUE;
						pSymbolStart = pPtr;
						do
						{
							ch = *(++pPtr);
							//*
							if(ch == '/')
							{
								if(*(--pPtr) == '*')
								{
									bComment = FALSE;
									pPtr++;
									pPtr++;

									break;
								}
							}
							//*/
						}while (ch != 0 && ch != '\r');
						ic = m_icComment;
					}
				}
				else if(IsStringQuote(ch))
				{ // Process strings
					pSymbolStart = pPtr;
					TCHAR ch1 = ch;
					do
					{
						ch = *(++pPtr);
					} while (ch != 0 && ch != ch1 && ch != '\r');

					if(ch == ch1)
					{
						pPtr++;
					}

					ic = m_icString;
				}
				else if(_istdigit(ch))
				{ // Process numbers
					pSymbolStart = pPtr;

					if(ch == '0')
					{
						pPtr++;
						
						if((*pPtr == 'x') || (*pPtr == 'X'))
						{//十六进制
							do
							{
								ch = *(++pPtr);
							}while((ch != 0) && (ch != '\r')
								&& (((ch >= '0') && (ch <= '9'))
								|| ((ch >= 'a') && (ch <= 'f'))
								|| ((ch >= 'A') && (ch <= 'F'))));
						}
						else
						{
							pPtr--;
							_tcstod(pSymbolStart, &pPtr);
						}
					}
					else
					{
						_tcstod(pSymbolStart, &pPtr);
					}

					ic = m_icNumber;
				}
				else if(ch == '\'')
				{//字符的处理
					pSymbolStart = pPtr;

					do
					{
						ch = *(++pPtr);

						if(ch == '\\')
						{
							pPtr++;
						}
						
						if(ch == '\'')
						{
							pPtr++;

							break;
						}
					}while((ch != 0) && (ch != '\r'));
					
					ic = m_icCharacter;
				}
				else if(_istalpha(ch) || ch == '_' || ch == '#')
				{ // Process keywords
					pSymbolStart = pPtr;

					do
					{
						ch = *(++pPtr);
					}while (_istalnum(ch) || ch == '_');
					
					*pPtr = 0;
					int nPos = IsKeyword(pSymbolStart);

					if (nPos >= 0)
					{
						if (_tcsicmp(m_strComment, pSymbolStart) == 0)
						{
							*pPtr = ch;
							*pSymbolStart = m_chComment;
							if (pSymbolStart[1] != 0 && m_chComment2 != 0)
								pSymbolStart[1] = m_chComment2;
							pPtr = pSymbolStart;
							pSymbolStart = NULL;
							continue;
						}
						ic = m_icKeyword;
					}
					else
					{
						nPos = IsConstant(pSymbolStart);
						if (nPos >= 0)
						{
							ic = m_icConstant;
						}
						else
						{
							pSymbolStart = NULL;
						}
					}
					
					*pPtr = ch;
				}
				else
				{
					pPtr++;
				}
			}

			if(pSymbolStart != NULL)
			{
				ASSERT(pSymbolStart < pPtr);
				SetFormatRange(nStart + pStart - pBuffer, nStart + pSymbolStart - pBuffer, FALSE, RGB(0, 0, 0));
				SetFormatRange(nStart + pSymbolStart - pBuffer, nStart + pPtr - pBuffer, ic.bBold, ic.clrColor);
				pStart = pPtr;
				pSymbolStart = 0;
			}
			else if(*pPtr == 0)
			{
				SetFormatRange(nStart + pStart - pBuffer, nStart + pPtr - pBuffer, FALSE, RGB(0, 0, 0));
			}
		}

		delete []pBuffer;
	} 
	catch(...)
	{
	}

	GetRichEditCtrl().SetSel(crOldSel);
	GetRichEditCtrl().HideSelection(FALSE, FALSE);
	UnlockWindowUpdate();

	m_bInForcedChange = FALSE;
}

void CSkillBookView::FormatAll()
{
	m_bComment = FALSE;
	FormatTextRange(0, GetRichEditCtrl().GetTextLength(), m_bComment);
	m_bComment = FALSE;
}

BOOL CSkillBookView::IsStringQuote(TCHAR ch)
{
	return (m_strStringQuotes.Find(ch) >= 0);
}

BOOL CSkillBookView::IsKeyword(LPCTSTR lpszSymbol)
{
	CString strSymbol;
	
	strSymbol.Format(" %s ", lpszSymbol);
				
	return m_strKeywordsLower.Find(strSymbol);
}

int CSkillBookView::IsConstant(LPCTSTR lpszSymbol)
{
	CString strSymbol;
	
	strSymbol.Format(" %s ", lpszSymbol);
				
	return m_strConstantsLower.Find(strSymbol);
}

void CSkillBookView::SetFormatRange(int nStart, int nEnd, BOOL bBold, COLORREF clrColor)
{
	if(nStart >= nEnd)
	{
		return;
	}

	GetRichEditCtrl().SetSel(nStart, nEnd);

	DWORD dwEffects = 0;

	CHARFORMAT cfm;
	cfm.cbSize = sizeof(cfm);
    GetRichEditCtrl().GetSelectionCharFormat(cfm);
	
	if ((cfm.dwMask & CFM_COLOR)  && cfm.crTextColor == clrColor && 
		(cfm.dwMask & CFM_BOLD) && (cfm.dwEffects & CFE_BOLD) == dwEffects)
	{
		return;
	}

	cfm.dwEffects = dwEffects;
	cfm.crTextColor = clrColor;
	cfm.dwMask = CFM_BOLD | CFM_COLOR;

	GetRichEditCtrl().SetSelectionCharFormat(cfm);
}

void CSkillBookView::SetStringQuotes(LPCTSTR lpszStrQ)
{
	m_strStringQuotes = lpszStrQ;
}

void CSkillBookView::SetSLComment(LPCTSTR lpszComment)
{
	m_strComment = lpszComment;
}

void CSkillBookView::SetSLComment(TCHAR chComment, TCHAR chComment2)
{
	m_chComment = chComment;
	m_chComment2 = chComment2;
}

void CSkillBookView::AddKeywords(LPCTSTR lpszKwd)
{
	m_strKeywords = m_strKeywords + lpszKwd;
	m_strKeywordsLower = m_strKeywords;
}

void CSkillBookView::AddConstants(LPCTSTR lpszConst)
{
	m_strConstants = m_strConstants + lpszConst;
	m_strConstantsLower = m_strConstants;
}

void CSkillBookView::SetKeywordColor(COLORREF clrColor, BOOL bBold)
{
	m_icKeyword.clrColor = clrColor;
	m_icKeyword.bBold = bBold;
}

void CSkillBookView::SetConstantColor(COLORREF clrColor, BOOL bBold)
{
	m_icConstant.clrColor = clrColor;
	m_icConstant.bBold = bBold;
}

void CSkillBookView::SetCommentColor(COLORREF clrColor, BOOL bBold)
{
	m_icComment.clrColor = clrColor;
	m_icComment.bBold = bBold;
}

void CSkillBookView::SetNumberColor(COLORREF clrColor, BOOL bBold)
{
	m_icNumber.clrColor = clrColor;
	m_icNumber.bBold = bBold;
}

void CSkillBookView::SetCharacterColor(COLORREF clrColor, BOOL bBold)
{
	m_icCharacter.clrColor = clrColor;
	m_icCharacter.bBold = bBold;
}

void CSkillBookView::SetStringColor(COLORREF clrColor, BOOL bBold)
{
	m_icString.clrColor = clrColor;
	m_icString.bBold = bBold;
}

void CSkillBookView::FormatOneTextLine(const int nLineIndex, BOOL &bComment)
{
	int nLen = 0;
	int nGetLen = 0;

	nLen = GetRichEditCtrl().LineLength(nLineIndex);

	if(nLen < 1)
	{
		return;
	}

	TCHAR *pszBuff = new TCHAR[nLen + 1];

	nGetLen = GetRichEditCtrl().GetLine(0, pszBuff);

	if(nGetLen != nLen)
	{
		delete []pszBuff;

		return;
	}

	delete []pszBuff;
}

int CSkillBookView::GetLineLength(int nIndex)
{
	int i = 0;
	int nLineCount = 0;
	int nLineLen = 0;
	int nSum = 0;

	nLineCount = GetRichEditCtrl().GetLineCount();

	if(nIndex >= nLineCount)
	{
		return -1;
	}

	GetRichEditCtrl().SetSel(0, 0);
	for(i=0; i<=nIndex; i++)
	{
		nSum += nLineLen;
		GetRichEditCtrl().SetSel(nSum, nSum);
		nLineLen = GetRichEditCtrl().LineLength(-1);
		nSum += 2;
	}
	
	GetRichEditCtrl().SetSel(nSum - 2, nSum + nLineLen - 2);

	return nLineLen;
}

CString CSkillBookView::GetLineText(const int nIndex)
{
	int nLineLen;
	CString szRet("");
	TCHAR *pszBuff = NULL;

	nLineLen = GetLineLength(nIndex);
	
	if(nLineLen > 0)
	{
		pszBuff = new TCHAR[nLineLen + 1];
		memset(pszBuff, 0, sizeof(TCHAR) * (nLineLen + 1));
		GetRichEditCtrl().GetLine(nIndex, pszBuff);
		pszBuff[nLineLen] = 0;
		szRet.Format(_T("%s"), pszBuff);

		delete []pszBuff;
	}
		
	return szRet;
}

void CSkillBookView::OnFilePrintPreview() 
{
	// TODO: Add your command handler code here
	CPrintPreviewState* pState = new CPrintPreviewState;

	if(!DoPrintPreview(IDD_PREVIEW, this, RUNTIME_CLASS(CMyPreviewView), pState))
	{
		delete pState;      // preview failed to initialize, delete State now
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -