texteditbox.cpp

来自「24点游戏,用c++实现」· C++ 代码 · 共 289 行

CPP
289
字号
// TextEditBox.cpp : implementation file
//

#include "stdafx.h"
#include "math.h"
//#include "EditEx.h"

#include "TextEditBox.h"


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

/////////////////////////////////////////////////////////////////////////////
// CTextEditBox

CTextEditBox::CTextEditBox()
{
	m_pText=NULL;
	m_brush.CreateSolidBrush(RGB(255,255,255));
}

CTextEditBox::~CTextEditBox()
{
	m_font.DeleteObject();
	m_brush.DeleteObject();
}


BEGIN_MESSAGE_MAP(CTextEditBox, CEdit)
	//{{AFX_MSG_MAP(CTextEditBox)
	ON_WM_KEYDOWN()
	ON_WM_CTLCOLOR_REFLECT()
	ON_WM_KILLFOCUS()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CTextEditBox message handlers


BOOL CTextEditBox::Create(DWORD dwStyle, CRect &rect, CWnd *pParentWnd)
{
	if(!CEdit::Create(WS_VISIBLE|ES_AUTOHSCROLL |ES_AUTOVSCROLL |ES_MULTILINE|ES_LEFT| WS_CHILD | WS_BORDER|WS_TABSTOP,rect,pParentWnd,0))
	{
		TRACE0("Failed to create text edit wnd!\n");
		return -1;
	}
	return 1;
}

void CTextEditBox::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
	
	CDC* pDC=GetDC();
	CFont* pFont=pDC->SelectObject(&m_font);
	int nIndex=0;
	CString strLine=MaxLenLine(nIndex);
	if(nIndex==GetCurLine())strLine+=(char)nChar;
	m_sizeFont=pDC->GetTextExtent(strLine);
	pDC->SelectObject(pFont);
	ReleaseDC(pDC);	

	if(nChar==10||nChar==13)
	{
		if((GetLineCount()+1)*m_sizeFont.cy>=m_rectPos.Height())
			m_rectPos.bottom+=m_sizeFont.cy;	
		SetWindowPos(NULL,m_rectPos.left, m_rectPos.top, m_rectPos.Width(),
			m_rectPos.Height()+m_sizeFont.cy/2, SWP_NOACTIVATE|SWP_SHOWWINDOW);
	}
	else
	{
		if(m_rectPos.Width()<m_sizeFont.cx+10)
			m_rectPos.right=m_rectPos.left+m_sizeFont.cx+10;
		SetWindowPos(NULL,m_rectPos.left, m_rectPos.top, m_rectPos.Width(),
			m_rectPos.Height()+m_sizeFont.cy/2, SWP_NOACTIVATE|SWP_SHOWWINDOW);
	}
}

void CTextEditBox::ShowWindow(const CRect &rect, CString &string,LPLOGFONT lpfont,COLORREF color)
{
	if(IsWindowVisible()&&m_pText!=&string)
	{
		//GetWindowText(*m_pText);
		//CEdit::ShowWindow(SW_HIDE);
		HideWindow();
		return;
	}

	m_color=color;
	if (lpfont!=NULL)
	{
		m_font.DeleteObject();
		m_font.CreateFontIndirect(lpfont);
		SetFont(&m_font,FALSE);

		CDC* pDC=GetDC();
		CFont* pFont=pDC->SelectObject(&m_font);
		m_sizeFont=pDC->GetTextExtent("A");
		pDC->SelectObject(pFont);
		ReleaseDC(pDC);
	}
	m_pText=&string;
	SetMargins(0,0);
	SetWindowText(string);
	m_rectPos=rect;
	if(m_rectPos.Height()<m_sizeFont.cy) m_rectPos.bottom=m_rectPos.top+m_sizeFont.cy;
//	m_rectPos.OffsetRect(25,2);
	m_rectPos.right+=2;
	m_rectPos.NormalizeRect();
	SetWindowPos(NULL,m_rectPos.left, m_rectPos.top,m_rectPos.Width(),
		m_rectPos.Height()+m_sizeFont.cy/2, SWP_NOACTIVATE|SWP_SHOWWINDOW);
	CEdit::ShowWindow(SW_SHOW);	
	SetSel(string.GetLength(),string.GetLength());
	SetFocus();	
	
}

void CTextEditBox::HideWindow()
{
	if(m_pText!=NULL)
	{
		GetWindowText(*m_pText);
		m_pText=NULL;
	}
	CEdit::ShowWindow(SW_HIDE);
	
}

HBRUSH CTextEditBox::CtlColor(CDC* pDC, UINT nCtlColor) 
{
	// TODO: Change any attributes of the DC here
	pDC->SetTextColor(m_color);
	// TODO: Return a non-NULL brush if the parent's handler should not be called
//	return NULL;
	return (HBRUSH)m_brush.GetSafeHandle();
}

//DEL CSize CTextEditBox::GetTextExtent()
//DEL {
//DEL 	int nPos;
//DEL 	int len=0;
//DEL 	CSize sizeText(0,0),tmpSize;
//DEL 	CString text;
//DEL 	GetWindowText(text);
//DEL 	text+="A";
//DEL 	nPos=text.Find('\n',0);
//DEL 	CDC* pDC=GetDC();
//DEL 	CFont* pFont=pDC->SelectObject(&m_font);
//DEL 	if(nPos==-1)
//DEL 	{
//DEL 		sizeText=pDC->GetTextExtent(text);
//DEL 		return sizeText;
//DEL 	}
//DEL 	CString strSub;
//DEL 	while(nPos!=-1)
//DEL 	{
//DEL 		strSub=text.Left(nPos+1);
//DEL //		TRACE("strSub:"+strSub);
//DEL 		tmpSize=pDC->GetTextExtent(strSub);
//DEL 		if(sizeText.cx<tmpSize.cx)
//DEL 			sizeText=tmpSize;
//DEL //		TRACE("sizeText:%d,tmpSize:%d\n",sizeText.cx,tmpSize.cx);
//DEL 		text=text.Mid(nPos+1);
//DEL //		TRACE("Text:"+text+'\n');
//DEL 		nPos=text.Find('\n',0);
//DEL 	}
//DEL 	if (!text.IsEmpty())
//DEL 	{
//DEL 		tmpSize=pDC->GetTextExtent(text);
//DEL 		if(sizeText.cx<tmpSize.cx)
//DEL 			sizeText=tmpSize;
//DEL 	}
//DEL //	TRACE(strNeed+"\n");
//DEL 	pDC->SelectObject(pFont);
//DEL 	ReleaseDC(pDC);
//DEL 	return sizeText;
//DEL }


CString CTextEditBox::MaxLenLine(int& nIndex)
{
	int nPos;
	int len=0;
	nIndex=0;
	CSize tmpSize;
	CString text;
	CString strSub,strResult;
	GetWindowText(text);
	nPos=text.Find('\n',0);
	CDC* pDC=GetDC();
	CFont* pFont=pDC->SelectObject(&m_font);
	if(nPos==-1)
		return text;
	int curIndex=-1;
	while(nPos!=-1)
	{		
		strSub=text.Left(nPos+1);		
		tmpSize=pDC->GetTextExtent(strSub);
		curIndex++;
		if(len<tmpSize.cx)
		{			
			nIndex=curIndex;
			len=tmpSize.cx;
			strResult=strSub;
		}
		text=text.Mid(nPos+1);
		nPos=text.Find('\n',0);
	}
	if (!text.IsEmpty())
	{
		curIndex++;
		tmpSize=pDC->GetTextExtent(text);
		if(len<tmpSize.cx)
		{
			strResult=text;
			nIndex=curIndex;
		}
	}
	pDC->SelectObject(pFont);
	ReleaseDC(pDC);
	return strResult;

}

int CTextEditBox::GetCurLine()
{
	CString text;
	GetWindowText(text);
	int nPos=text.Find('\n',0);
	if(nPos==-1)
		return 0;
	int curPos=LineIndex(-1);
	int nIndex=0;
	while(nPos!=-1)
	{		
		if(curPos>=nPos) nIndex++;
		nPos=text.Find('\n',nPos+1);
	}
	return nIndex;
}

void CTextEditBox::OnKillFocus(CWnd* pNewWnd) 
{
	CEdit::OnKillFocus(pNewWnd);
	HideWindow();
//	AfxMessageBox("abc");
	// TODO: Add your message handler code here
	
}

//DEL BOOL CTextEditBox::PreTranslateMessage(MSG* pMsg) 
//DEL {
//DEL 	// TODO: Add your specialized code here and/or call the base class
//DEL 	
//DEL 	if(pMsg->message==WM_KEYDOWN)
//DEL 	{
//DEL 		if(pMsg->wParam==VK_DELETE)
//DEL 		{
//DEL 			Delete();
//DEL 			return TRUE;
//DEL 		}
//DEL 	}
//DEL 
//DEL 	return CEdit::PreTranslateMessage(pMsg);
//DEL }

//DEL void CTextEditBox::Delete()
//DEL {
//DEL 	int s,e;
//DEL 	GetSel(s, e);
//DEL 	CString strTmp;
//DEL 	GetWindowText(strTmp);
//DEL 	if(strTmp.Mid(s,1)=="\r") 
//DEL 	{		
//DEL 		if(s==e) SetSel(s,e+2);
//DEL 	}
//DEL 	else
//DEL 		if(s==e) SetSel(s,e+1);
//DEL 	Clear();
//DEL }


⌨️ 快捷键说明

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