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

📄 afedit.cpp

📁 C++编程实践与技巧一书各章节的源码
💻 CPP
字号:
// AFEdit.cpp : implementation file
//

#include "stdafx.h"
#include "AFEdit.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#define MIN_WORD_LEN	3
#define MAX_WORDS		128

/////////////////////////////////////////////////////////////////////////////
// CAFEdit

CAFEdit::CAFEdit()
{
}

CAFEdit::~CAFEdit()
{
}


BEGIN_MESSAGE_MAP(CAFEdit, CEdit)
	//{{AFX_MSG_MAP(CAFEdit)
	ON_WM_CHAR()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAFEdit message handlers
//添加可自动填充的单词,参数sString为要添加的单词
//正确执行返回TRUE,否则返回FALSE
bool CAFEdit::AddString(CString sString)
{
	bool bRet;

	// I mean it's not useful to store short words
	if( sString.GetLength() < MIN_WORD_LEN )
		return false;

	// cannot add more than 128 strings
	if( m_strList.GetCount() > MAX_WORDS )
		return false;

	if( m_strList.IsEmpty() ) {
		// if list is empty add first string
		try {
			m_strList.AddHead( sString );
			bRet = true;
		} catch( CMemoryException *e ) {
			e->Delete();
			bRet = false;
		}
	} else {
		if( !m_strList.Find( sString ) ) {
			// insert into sorted list searching for a valid position
			int nCount = m_strList.GetCount();
			POSITION pos = m_strList.GetHeadPosition();
			for( int f=0; f<nCount; f++ ) {
				// ascending order
				if( sString < m_strList.GetAt( pos ) ) {
					break;
				} else {
					m_strList.GetNext( pos );
				}
			}
			try {
				if( f == nCount )
					m_strList.AddTail( sString );
				else
					m_strList.InsertBefore( pos, sString );
				bRet = true;
			} catch( CMemoryException *e ) {
				e->Delete();
				bRet = false;
			}
		} else
			bRet = true;
	}

	return bRet;
}

//
void CAFEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	CEdit::OnChar(nChar, nRepCnt, nFlags);

	TRACE("%c %d\n", nChar, nChar );

	// Skip if BackSpace
	if( nChar == VK_BACK )
		return;


	POSITION pos;
	CString sBuffer, sDummy, sWord;
	int nLen, nLastSpace, nWordLen;

	GetWindowText( sBuffer );
	nLen = sBuffer.GetLength();
	sDummy = sBuffer;
	for( int x=nLen; x>0; x-- )
		if( isspace( sBuffer.GetAt(x-1)))
			break;
	nLastSpace = x-1;
	if( nLastSpace > 0 )
		sWord = sBuffer.Right( nLen - nLastSpace - 1 );
	else
		sWord = sBuffer;
	nWordLen = sWord.GetLength();

	if( !sWord.IsEmpty() ) {
		// maybe a fast search method could be appreciated
		for( int f=0; f<m_strList.GetCount(); f++ ) {
			pos = m_strList.FindIndex( f );
			if( sWord == m_strList.GetAt( pos ).Left( nWordLen )) {
				sDummy += m_strList.GetAt( pos ).Right( m_strList.GetAt( pos ).GetLength() - nWordLen );
				SetWindowText( sDummy );
				// select automatically inserted text
				SetSel( nLen, nLen + m_strList.GetAt( pos ).GetLength(), false );
				break;
			}
		}
	}
}

⌨️ 快捷键说明

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