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

📄 bcgpmaskedit.cpp

📁 远程网络监视程序的源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:

				// no more group
				else
				{
					MessageBeep((UINT)-1);
					return;
				}
			}

			// ---------------------------------------------------
			// Calc the number of literals with the same mask char
			// ---------------------------------------------------
			ASSERT(nStartPos > 0);
			ASSERT(nEndPos > 0);
			ASSERT(nGroupEnd <= m_strInputTemplate.GetLength());

			int nSameMaskCharsNum = 1;
			int nIndex = nStartPos-1; // an index of the char to delete
			TCHAR chMaskChar = m_strMask[nIndex];
			BOOL bScanMore = TRUE;
			while (bScanMore && (nIndex + nSameMaskCharsNum < nGroupEnd))
			{
				if (m_strMask[nIndex + nSameMaskCharsNum] == chMaskChar)
				{
					nSameMaskCharsNum++;
				}
				else
				{
					bScanMore = FALSE;
				}
			}

			// ---------------------------------
			// Validate new string (dispensable)
			// ---------------------------------
			int i = nIndex;
			for (; (i + nSameMaskCharsNum < nGroupEnd); i++)
			{
				if (m_str[i] != m_chMaskInputTemplate) // allow m_chMaskInputTemplate
				{
					if (!IsMaskedChar(m_str[i], m_strMask[i]))
					{
						MessageBeep((UINT)-1);
						return;
					}
				}
			}
			
			// -----------------------
			// Form the shifted string
			// -----------------------
			ASSERT(nIndex >= nGroupStart);
			ASSERT(nIndex + nSameMaskCharsNum <= nGroupEnd);
			
			CString strReplace = m_str.Mid(nIndex, nSameMaskCharsNum);
			if (nSameMaskCharsNum > 0)
			{
				strReplace = strReplace.Right(nSameMaskCharsNum - 1);
				strReplace += m_chMaskInputTemplate;
			}

			// -------------------------------------------
			// Replace the content with the shifted string
			// -------------------------------------------
			CEdit::SetSel(nIndex, nIndex+nSameMaskCharsNum);
			CEdit::ReplaceSel(strReplace, TRUE);
			CEdit::SetSel(nIndex, nIndex);
			for(i=0; i < strReplace.GetLength(); i++)
			{
				m_str.SetAt(nIndex+i, strReplace[i]);
			}
			
		}
		else // Don't use m_chMaskInputTemplate - delete symbol
		{
			CWnd::OnKeyDown(nChar, nRepCnt, nFlags);
		}
	}
	
	// --------------------------------
	// Have one or more chars selected
	// --------------------------------
	else
	{
		if (!m_strInputTemplate.IsEmpty()) // Use m_strInputTemplate
		{
			// ---------------------------------------------------
			// Calc the number of literals with the same mask char
			// ---------------------------------------------------
			ASSERT(nStartPos >= 0);
			ASSERT(nEndPos > 0);
			ASSERT(nStartPos <= m_strInputTemplate.GetLength());

			int nSameMaskCharsNum = 1;
			int nIndex = nStartPos; // an index of the first selected char
			TCHAR chMaskChar = m_strMask[nIndex];
			BOOL bScanMore = TRUE;
			while (bScanMore && (nIndex + nSameMaskCharsNum < nGroupEnd))
			{
				if (m_strMask[nIndex + nSameMaskCharsNum] == chMaskChar)
				{
					nSameMaskCharsNum++;
				}
				else
				{
					bScanMore = FALSE;
				}
			}
			
			// Make sure the selection has the same mask char
			if (nEndPos - nStartPos > nSameMaskCharsNum)
			{
				MessageBeep((UINT)-1);
				CEdit::SetSel(nIndex, nIndex+nSameMaskCharsNum);
				return;
			}

			// -------------------------------
			// Form the shifted replace string
			// -------------------------------
			ASSERT(nIndex >= nGroupStart);
			ASSERT(nIndex + nSameMaskCharsNum <= nGroupEnd);
			
			CString strReplace = m_str.Mid(nIndex, nSameMaskCharsNum);
			if (nSameMaskCharsNum > 0)
			{
				ASSERT(nStartPos <= m_strInputTemplate.GetLength());
				ASSERT(nEndPos <= m_strInputTemplate.GetLength());
				int nRange = nEndPos - nStartPos;
				ASSERT(nRange>0);
				
				strReplace = strReplace.Right(nSameMaskCharsNum - nRange);
				strReplace += CString(m_chMaskInputTemplate, nRange);
			}

			// -------------------------------------------
			// Replace the content with the shifted string
			// -------------------------------------------
			CEdit::SetSel(nIndex, nIndex+nSameMaskCharsNum);
			CEdit::ReplaceSel(strReplace, TRUE);
			CEdit::SetSel(nIndex, nIndex);
			for(int i=0; i < strReplace.GetLength(); i++)
			{
				m_str.SetAt(nIndex+i, strReplace[i]);
			}
		}
		else
		{
			// Don't use m_chMaskInputTemplate - delete symbols
			CWnd::OnKeyDown(nChar, nRepCnt, nFlags);
		}
	}
}

void CBCGPMaskEdit::OnCharDelete(UINT nChar, UINT nRepCnt, UINT nFlags)
{
	ASSERT(m_strMask.IsEmpty() == m_strInputTemplate.IsEmpty());
	ASSERT(m_strMask.GetLength() == m_strInputTemplate.GetLength());

	int nStartPos, nEndPos;
	CEdit::GetSel(nStartPos, nEndPos);
	ASSERT(nStartPos>=0);
	ASSERT(nEndPos>=0);
	ASSERT(nEndPos>=nStartPos);

	// -----------------
	// Calc group bounds
	// -----------------
	int nGroupStart, nGroupEnd;
	CEdit::GetSel(nGroupStart, nGroupEnd);
	GetGroupBounds(nGroupStart, nGroupEnd, nGroupStart);

	// ------------
	// Out of range
	// ------------
	if ((nStartPos<0) && (nEndPos > m_str.GetLength()) ||
		(nStartPos < nGroupStart) || (nStartPos > nGroupEnd) ||
		(nEndPos < nGroupStart) || (nEndPos > nGroupEnd))
	{
		MessageBeep((UINT)-1);
		CEdit::SetSel(nGroupStart, nGroupEnd);
		return;
	}

	// -----------------
	// No selected chars
	// -----------------
	if (nStartPos == nEndPos)
	{
		if (!m_strMask.IsEmpty()) // Don't use m_strMask
		{
			// -----------------------------------------------
			// Make sure the cursor is not at the end of group
			// -----------------------------------------------
			if (nEndPos==nGroupEnd)
			{
				MessageBeep((UINT)-1);
				return;
			}

			// ---------------------------------------------------
			// Calc the number of literals with the same mask char
			// ---------------------------------------------------
			ASSERT(nStartPos >= 0);
			ASSERT(nEndPos >= 0);
			ASSERT(nGroupEnd <= m_strInputTemplate.GetLength());

			int nSameMaskCharsNum = 1;
			int nIndex = nStartPos; // an index of the char to delete
			TCHAR chMaskChar = m_strMask[nIndex];
			BOOL bScanMore = TRUE;
			while (bScanMore && (nIndex + nSameMaskCharsNum < nGroupEnd))
			{
				if (m_strMask[nIndex + nSameMaskCharsNum] == chMaskChar)
				{
					nSameMaskCharsNum++;
				}
				else
				{
					bScanMore = FALSE;
				}
			}

			// ---------------------------------
			// Validate new string (dispensable)
			// ---------------------------------
			int i = nIndex;
			for (; (i + nSameMaskCharsNum < nGroupEnd); i++)
			{
				if (m_str[i] != m_chMaskInputTemplate) // allow m_chMaskInputTemplate
				{
					if (!IsMaskedChar(m_str[i], m_strMask[i]))
					{
						MessageBeep((UINT)-1);
						return;
					}
				}
			}
			
			// -----------------------
			// Form the shifted string
			// -----------------------
			ASSERT(nIndex >= nGroupStart);
			ASSERT(nIndex + nSameMaskCharsNum <= nGroupEnd);
			
			CString strReplace = m_str.Mid(nIndex, nSameMaskCharsNum);
			if (nSameMaskCharsNum > 0)
			{
				strReplace = strReplace.Right(nSameMaskCharsNum - 1);
				strReplace += m_chMaskInputTemplate;
			}

			// -------------------------------------------
			// Replace the content with the shifted string
			// -------------------------------------------
			CEdit::SetSel(nIndex, nIndex+nSameMaskCharsNum);
			CEdit::ReplaceSel(strReplace, TRUE);
			CEdit::SetSel(nIndex, nIndex);
			for(i=0; i < strReplace.GetLength(); i++)
			{
				m_str.SetAt(nIndex+i, strReplace[i]);
			}

		}
		else // Don't use m_chMaskInputTemplate - delete symbol
		{
			CWnd::OnKeyDown(nChar, nRepCnt, nFlags);
		}
	}

	// --------------------------------
	// Have one or more chars selected
	// --------------------------------
	else
	{
		if (!m_strInputTemplate.IsEmpty()) // Use m_strInputTemplate
		{
			// ---------------------------------------------------
			// Calc the number of literals with the same mask char
			// ---------------------------------------------------
			ASSERT(nStartPos >= 0);
			ASSERT(nEndPos > 0);
			ASSERT(nStartPos <= m_strInputTemplate.GetLength());

			int nSameMaskCharsNum = 1;
			int nIndex = nStartPos; // an index of the first selected char
			TCHAR chMaskChar = m_strMask[nIndex];
			BOOL bScanMore = TRUE;
			while (bScanMore && (nIndex + nSameMaskCharsNum < nGroupEnd))
			{
				if (m_strMask[nIndex + nSameMaskCharsNum] == chMaskChar)
				{
					nSameMaskCharsNum++;
				}
				else
				{
					bScanMore = FALSE;
				}
			}

			// Make sure the selection has the same mask char
			if (nEndPos - nStartPos > nSameMaskCharsNum)
			{
				MessageBeep((UINT)-1);
				CEdit::SetSel(nIndex, nIndex+nSameMaskCharsNum);
				return;
			}

			// -------------------------------
			// Form the shifted replace string
			// -------------------------------
			ASSERT(nIndex >= nGroupStart);
			ASSERT(nIndex + nSameMaskCharsNum <= nGroupEnd);
			
			CString strReplace = m_str.Mid(nIndex, nSameMaskCharsNum);
			if (nSameMaskCharsNum > 0)
			{
				ASSERT(nStartPos <= m_strInputTemplate.GetLength());
				ASSERT(nEndPos <= m_strInputTemplate.GetLength());
				int nRange = nEndPos - nStartPos;
				ASSERT(nRange>0);
				
				strReplace = strReplace.Right(nSameMaskCharsNum - nRange);
				strReplace += CString(m_chMaskInputTemplate, nRange);
			}

			// -------------------------------------------
			// Replace the content with the shifted string
			// -------------------------------------------
			CEdit::SetSel(nIndex, nIndex+nSameMaskCharsNum);
			CEdit::ReplaceSel(strReplace, TRUE);
			CEdit::SetSel(nIndex, nIndex);
			for(int i=0; i < strReplace.GetLength(); i++)
			{
				m_str.SetAt(nIndex+i, strReplace[i]);
			}
		}
		else
		{
			// Don't use m_chMaskInputTemplate - delete symbols
			CWnd::OnKeyDown(nChar, nRepCnt, nFlags);
		}
	}
}

void CBCGPMaskEdit::GetGroupBounds(int &nBegin, int &nEnd, int nStartPos, BOOL bForward)
{
	ASSERT(m_strMask.IsEmpty() == m_strInputTemplate.IsEmpty());
	ASSERT(m_strMask.GetLength() == m_strInputTemplate.GetLength());

	if (!m_strInputTemplate.IsEmpty()) // use mask
	{
		ASSERT(m_str.GetLength() == m_strMask.GetLength());
		ASSERT(nStartPos >= 0);
		ASSERT(nStartPos <= m_strInputTemplate.GetLength());

		if (bForward)
		{
			// ----------------------------------------
			// If nStartPos is in the middle of a group
			// Reverse search for the begin of a group
			// ----------------------------------------
			int i = nStartPos;
			if (nStartPos>0)
			{
				if (m_strInputTemplate[nStartPos-1] == _T('_'))
				{
					do
					{
						i--;
					}
					while ((i>0) && (m_strInputTemplate[i]==_T('_')));
				}
			}
			if (i == m_strInputTemplate.GetLength())
			{
				nBegin = -1; // no group
				nEnd = 0;
				return;
			}

			// --------------------------------------------------
			// i points between groups or to the begin of a group
			// Search for the begin of a group
			// --------------------------------------------------
			if (m_strInputTemplate[i] != _T('_'))
			{
				i = m_strInputTemplate.Find(_T('_'), i);
				if (i == -1)
				{
					nBegin = -1; // no group
					nEnd = 0;
					return;
				}
			}
			nBegin = i;

			// -----------------------------
			// Search for the end of a group
			// -----------------------------
			while((i < m_strInputTemplate.GetLength()) && (m_strInputTemplate[i]==_T('_')))
			{
				i++;
			}
			nEnd = i;
		}

		else // backward
		{
			// ----------------------------------------
			// If nStartPos is in the middle of a group
			// Search for the end of a group
			// ----------------------------------------
			int i = nStartPos;
			while ((i < m_str.GetLength()) && (m_strInputTemplate[i] == _T('_')))
			{
				i++;
			}
			if (i==0)
			{
				nBegin = -1; // no group
				nEnd = 0;
				return;
			}

			// ------------------------------------------------
			// i points between groups or to the end of a group
			// Reverse search for the end of a group
			// ------------------------------------------------
			if (m_strInputTemplate[i-1] != _T('_'))
			{
				do
				{
					i--;
				}
				while ((i>0) && (m_strInputTemplate[i-1] != _T('_')));
				if (i==0)
				{
					nBegin = -1; // no group
					nEnd = 0;
					return;
				}
			}
			nEnd = i;

			// -------------------------------
			// Search for the begin of a group
			// -------------------------------
			do
			{
				i--;
			}
			while ((i>0) && (m_strInputTemplate[i-1]==_T('_')));
			nBegin = i;
		}
	}

	else // don't use mask
	{
		// nStartPos ignored
		nBegin = 0; 
		nEnd = m_str.GetLength();
	}
}

BOOL CBCGPMaskEdit::OnUpdateR() 
{
	// TODO: If this is a RICHEDIT control, the control will not
	// send this notification unless you override the CEdit::OnInitDialog()
	// function to send the EM_SETEVENTMASK message to the control
	// with the ENM_UPDATE flag ORed into the lParam mask.
	
	// TODO: Add your control notification handler code here
	CString str;
	CWnd::GetWindowText(str);

	if (m_str != str)
	{
		// work incorrect for Paste when m_bSetMaskedCharsOnly==TRUE
		if (!SetValue(str, TRUE/*!m_bSetMaskedCharsOnly*/))
		{
			MessageBeep((UINT)-1);
			return FALSE;
		}
		if (str != m_str) // str may be empty
		{
			CWnd::SetWindowText(m_str);

			if (m_bSelectByGroup)
			{
				int nBegin, nEnd;
				GetGroupBounds(nBegin, nEnd, 0, TRUE);

				CEdit::SetSel(nBegin, nEnd);
			}
			else
			{
				CEdit::SetSel(0, -1);
			}
		}
	}
	return FALSE;
}

void CBCGPMaskEdit::OnSetFocus() 
{
	if (m_bSelectByGroup)
	{
		int nBegin, nEnd;
		GetGroupBounds(nBegin, nEnd, 0, TRUE);
		if (nBegin == -1)
		{
		}
		CEdit::SetSel(nBegin, nEnd);
	}
	else
	{
		CEdit::SetSel(0, -1);
	}
}

⌨️ 快捷键说明

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