// ConsoleBox.cpp
//
#include "stdafx.h"
#include "resource.h"
#include "ConsoleBox.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define CRLF _T("\r\n")
/////////////////////////////////////////////////////////////////////////////
// CConsoleBox
CConsoleBox::CConsoleBox()
{
m_hReceiver = NULL;
m_uCallbackMsg = 0;
m_nNewCmdPos = m_nCurCommand = m_nTotalCommand = 0;
}
CConsoleBox::~CConsoleBox()
{
}
void CConsoleBox::CarryReturn(BOOL bLineFeed)
{
SetCaretPos();
if (!bLineFeed) {
SetSel(LineIndex(), GetWindowTextLength());
ReplaceSel(_T(""));
}
else {
AppendText(CRLF);
}
}
int CConsoleBox::PrintF(LPCTSTR fmt, ...)
{
LPTSTR psz = NULL;
int nSize = 64;
int nPrint = 0;
for (;;) {
psz = new TCHAR[nSize];
va_list va;
va_start(va, fmt);
nPrint = _vsnprintf(psz, nSize, fmt, va);
if (nPrint >= 0) {
break;
}
delete psz;
nSize *= 2;
}
CString str(psz);
str.Replace(_T("\n"), CRLF);
AppendText(str);
delete psz;
return nPrint;
}
void CConsoleBox::SetCaretPos(int pos)
{
AppendText();
if (pos >= 0) {
int li = LineIndex() + pos;
SetSel(li, li);
ReplaceSel(_T(""));
}
}
void CConsoleBox::GetCaretPos(int& nLine, int& nPos)
{
int nStart, nEnd;
GetSel(nStart, nEnd);
nLine = LineFromChar(nEnd);
nPos = nEnd - LineIndex(nLine);
}
void CConsoleBox::SetCurCommand(LPCTSTR lpszCommand)
{
SetSel(LineIndex()+m_strPromptText.GetLength(), GetWindowTextLength());
ReplaceSel(lpszCommand);
SetSel(LineIndex()+m_strPromptText.GetLength(), GetWindowTextLength());
}
void CConsoleBox::AppendText(LPCTSTR lpszText)
{
int len = GetWindowTextLength();
SetSel(len,len);
ReplaceSel(lpszText? lpszText : _T(""));
}
BOOL CConsoleBox::IsEditingLine()
{
BOOL b = LineFromChar() == GetLineCount()-1;
return b;
}
BOOL CConsoleBox::HitPromptArea()
{
int nLine, nPos;
GetCaretPos(nLine, nPos);
BOOL b = IsEditingLine() && nPos < m_strPromptText.GetLength();
return b;
}
void CConsoleBox::DoPaste()
{
if(OpenClipboard()) {
LPCTSTR pc = (LPCTSTR)GetClipboardData(CF_TEXT);
if (pc) {
CString str(pc);
str.Replace(_T("\r\n"), _T("\r"));
str.Replace(_T("\n"), _T("\r"));
while (*pc) {
SendMessage(WM_CHAR, (WPARAM)*pc, 0);
pc++;
}
}
CloseClipboard();
}
}
BEGIN_MESSAGE_MAP(CConsoleBox, CEdit)
//{{AFX_MSG_MAP(CConsoleBox)
ON_WM_CREATE()
ON_WM_KEYDOWN()
ON_WM_CHAR()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CConsoleBox
BOOL CConsoleBox::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO
cs.style |= ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL | ES_WANTRETURN | WS_VSCROLL | WS_HSCROLL;
return CEdit::PreCreateWindow(cs);
}
int CConsoleBox::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CEdit::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO
SetHistorySize(20);
return 0;
}
void CConsoleBox::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO
if (nChar == VK_DELETE) {
if (!IsEditingLine() || HitPromptArea()) {
return;
}
}
else if (nChar==VK_UP) {
if (m_aCommandHistory.GetUpperBound() >= 0) {
SetCurCommand(m_aCommandHistory[m_nCurCommand--]);
if (m_nCurCommand < 0) {
m_nCurCommand = m_nTotalCommand - 1;
}
}
return;
}
else if (nChar==VK_DOWN) {
if (m_aCommandHistory.GetUpperBound() >= 0) {
SetCurCommand(m_aCommandHistory[m_nCurCommand++]);
if (m_nCurCommand >= m_nTotalCommand) {
m_nCurCommand = 0;
}
}
return;
}
CEdit::OnKeyDown(nChar, nRepCnt, nFlags);
if (IsEditingLine()) {
int nStart, nEnd;
GetSel(nStart, nEnd);
if (nStart != nEnd) {
if (nStart - LineIndex() < m_strPromptText.GetLength()) {
SetSel(LineIndex() + m_strPromptText.GetLength(), nEnd);
}
}
else {
if (HitPromptArea()) {
SetCaretPos(m_strPromptText.GetLength());
}
}
}
}
void CConsoleBox::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO
if (nChar == VK_RETURN) {
if (m_hReceiver) {
SetCaretPos();
int nLineNo = GetLineCount() - 1;
int nLength = LineLength();
if (nLength > 0) {
LPTSTR psz = new TCHAR[nLength+1];
nLength = GetLine(nLineNo, psz, nLength+1);
psz[nLength] = 0;
LPTSTR pt = psz + m_strPromptText.GetLength();
::SendMessage(m_hReceiver, m_uCallbackMsg, 0, (LPARAM)(pt));
if (m_nTotalCommand < m_aCommandHistory.GetSize()) {
m_nCurCommand = m_nTotalCommand;
m_aCommandHistory[m_nTotalCommand++] = pt;
}
else {
m_nCurCommand = m_nTotalCommand;
m_aCommandHistory[m_nNewCmdPos++] = pt;
}
if (m_nNewCmdPos >= m_nTotalCommand) {
m_nNewCmdPos = 0;
}
delete psz;
}
}
AppendText(CRLF);
AppendText(m_strPromptText);
return;
}
else if (nChar==VK_BACK) {
if (!IsEditingLine() || HitPromptArea()) {
return;
}
}
else {
if (::GetKeyState(VK_SHIFT) >= 0 && ::GetKeyState(VK_CONTROL) >= 0 && !IsEditingLine()) {
SetCaretPos();
}
}
CEdit::OnChar(nChar, nRepCnt, nFlags);
}
LRESULT CConsoleBox::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// TODO
if ((message == WM_CLEAR || message == WM_CUT) && (!IsEditingLine() || HitPromptArea())) {
return 0;
}
if (message == WM_PASTE) {
DoPaste();
return 0;
}
return CEdit::DefWindowProc(message, wParam, lParam);
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
A console applicaton, such as a telnet emulator, needs to implement a console window for the user to type commands. It is not that easy to code everything that a text editor does.
资源推荐
资源详情
资源评论
收起资源包目录
2004060322094720585.zip (2个子文件)
ConsoleBox.h 2KB
ConsoleBox.cpp 5KB
共 2 条
- 1
资源评论
普通网友
- 粉丝: 882
- 资源: 2万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功