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

📄 mytext.cpp

📁 设计模式:工厂模式、单例模式的基本实现
💻 CPP
字号:
#include <iostream>
#include <conio.h>
using namespace std;
#include "MyStdIn.h"

HANDLE CMyText::m_nConHdl = GetStdHandle(STD_OUTPUT_HANDLE);

CMyText::CMyText(int nTop, int nLeft, int nRight, bool bPass) : m_strText("")
{
	m_nLeft = nLeft;
	m_nRight = nRight;
	m_nTop = nTop;
	m_nCurYPos = 0;
	m_bEnable = true; // 是否可以修改
	m_bVisible = false; // 是否可见
	m_bPass = bPass;
}

CMyText::~CMyText()
{
}

string	CMyText::GetText()
{
	return m_strText;
}

void CMyText::SetText(string strText)
{
	m_strText = strText;
	Show();
}

void CMyText::Enable()
{
	m_bEnable = true;
}

void CMyText::Disable()
{
	m_bEnable = false;
}

void CMyText::Show()
{
	ShowText();
	m_bVisible = true;
}

void CMyText::Hide()
{
	m_bVisible = false;
}

void CMyText::DrawBk()
{
	// 移到最前面
	GotoXY(m_nLeft, m_nTop);
	int nLen = m_strText.length();
	for(int i=m_nLeft; i<=m_nRight; i++)
	{
		putchar(' ');
	}
}

int	CMyText::Handle()
{
	GotoXY(m_nLeft+m_nCurYPos, m_nTop);
	int chIn = 0;
	char szHan[2]=""; // 保存用户输入的汉字
	while(1)
	{
		chIn = GetKey(szHan);

		if(chIn >= VKEY_UNKNOWN && chIn < VKEY_LEFT || chIn == VKEY_TAB)
		{
			break;
		}
		else if(chIn == VKEY_LEFT)
		{
			if(m_nCurYPos > 0)
			{
				if(m_strText[m_nCurYPos-1]<0)
				{
					-- m_nCurYPos;
				}
				-- m_nCurYPos;
				GotoXY(m_nLeft+m_nCurYPos, m_nTop);
			}
		}
		else if(chIn == VKEY_RIGHT)
		{
			int nLen = m_strText.length();
			if(m_nCurYPos < nLen)
			{
				if(m_strText[m_nCurYPos]<0)
				{
					++ m_nCurYPos;
				}
				++ m_nCurYPos;
				GotoXY(m_nLeft+m_nCurYPos, m_nTop);
			}
		}
		else if(chIn == VKEY_HAN && m_bEnable)
		{
			int nLen = m_strText.length();
			if(nLen<m_nRight-m_nLeft)
			{
				AddChar(szHan[0]);
				AddChar(szHan[1]);
				ShowText();
			}
			if(nLen>= m_nRight-m_nLeft)
			{// 达到数目限制
				chIn = VKEY_ENTER; // 自动返回回车键
				break;
			}
		}
		else if(chIn == VKEY_ESC)
		{
			break;
		}
		else if(chIn == VKEY_ENTER)
		{// 回车
			break;
		}
		else if(chIn == VKEY_BACK && m_bEnable)
		{// 回格
			if(m_nCurYPos > 0)
			{
				DelChar();
				ShowText();
			}
		}
		else
		{
			if(m_bEnable)
			{
				int nLen = m_strText.length();
				if(nLen<=m_nRight-m_nLeft)
				{
					AddChar(chIn);
					ShowText();
				}
/*				if(nLen>= m_nRight-m_nLeft)
				{// 达到数目限制
					chIn = VKEY_ENTER; // 自动返回回车键
					break;
				}
*/			}
		}
	}

	return chIn;
}

void CMyText::ShowText()
{
	// 移到最前面
	GotoXY(m_nLeft, m_nTop);
	// 输出文本
	int nLen = m_strText.length();
	if(!m_bPass)
	{
		cout << m_strText;
	}
	else
	{
		for(int i=0; i<nLen; ++i)
		{
			cout << "*";
		}
	}
	for(int i=m_nLeft + nLen; i<=m_nRight; i++)
	{
		putchar(' ');
	}
	// 光标位置复原
	GotoXY(m_nLeft + m_nCurYPos, m_nTop);
}

void CMyText::GotoXY(int _nX, int _nY)
{
	COORD loc;
	loc.X = _nX;
	loc.Y = _nY;
	SetConsoleCursorPosition(m_nConHdl, loc);
}

void CMyText::GetXY(int &nX, int &nY)
{
	CONSOLE_SCREEN_BUFFER_INFO csInfo;
	GetConsoleScreenBufferInfo(m_nConHdl, &csInfo);
	nX = csInfo.dwCursorPosition.X;
	nY = csInfo.dwCursorPosition.Y;
}

int CMyText::GetKey(char* pszHan)
{
	unsigned char chKey = 0;
	unsigned char chKey2 = -1; 
	bool bNeedNextKey = false; 

	do
	{
		chKey = _getch();
		if( bNeedNextKey ) 
		{
			if( chKey2 == 0)
			{
				return chKey-59+VKEY_F1;
			}
			else if( chKey2 == 224 ) 
			{
				switch(chKey)
				{
				case 75: //left
				{
					return VKEY_LEFT;
				} 
				case 77: //right 
				{
					return VKEY_RIGHT;
				}
				case 72: // Up
				{
					return VKEY_UP;
				}
				case 80: // Down
				{
					return VKEY_DOWN;
				}
				case 133: // F11
				{
					return VKEY_F11;
				}
				case 134: // F12
				{
					return VKEY_F12;
				}
				default:
				{
					if(chKey >= 161 && chKey <= 254)
					{
						pszHan[0] = chKey2;
						pszHan[1] = chKey;
						return VKEY_HAN;
					}
					else
					{
						return VKEY_UNKNOWN;
					}
					break;
				}
				}
			}
			else if(chKey >= 161 && chKey <= 254)
			{
				pszHan[0] = chKey2;
				pszHan[1] = chKey;
				return VKEY_HAN;
			}
			bNeedNextKey = false; 
		}
		if( chKey == 0 || chKey >= 127) 
		{
			chKey2 = chKey; 
			bNeedNextKey = true; 
		}
	}while(bNeedNextKey);
	return chKey;
}

void CMyText::DelChar()
{
	if(m_strText[m_nCurYPos-1]<0)
	{
		-- m_nCurYPos;
		m_strText.erase(m_nCurYPos, 1);
	}
	-- m_nCurYPos;
	m_strText.erase(m_nCurYPos, 1);
}

void CMyText::AddChar(char chIn)
{
	m_strText.insert(m_nCurYPos, 1, chIn);
	++ m_nCurYPos;
}

⌨️ 快捷键说明

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