From 0b65a24ac2164dab5c0e0bb113aadeb0204ae1c0 Mon Sep 17 00:00:00 2001 From: yuanyuanxiang <962914132@qq.com> Date: Sun, 18 Jan 2026 11:02:56 +0100 Subject: [PATCH] fix: #288 Command line issues --- client/ShellManager.cpp | 6 ++++-- server/2015Remote/ShellDlg.cpp | 33 ++++++++++++++++++++++++++++----- server/2015Remote/ShellDlg.h | 4 +++- 3 files changed, 35 insertions(+), 8 deletions(-) diff --git a/client/ShellManager.cpp b/client/ShellManager.cpp index 47e7751..8ca2973 100644 --- a/client/ShellManager.cpp +++ b/client/ShellManager.cpp @@ -124,8 +124,10 @@ DWORD WINAPI CShellManager::ReadPipeThread(LPVOID lParam) #ifdef _DEBUG Mprintf("===> Input length= %d \n", This->m_nCmdLength); #endif - const char *pStart = (char*)szTotalBuffer + This->m_nCmdLength; - int length = int(dwReturn) - This->m_nCmdLength; + int skipBytes = min(This->m_nCmdLength, (int)dwReturn); + const char *pStart = (char*)szTotalBuffer + skipBytes; + int length = (int)dwReturn - skipBytes; + This->m_nCmdLength -= skipBytes; if (length > 0) This->m_ClientObject->Send2Server(pStart, length); diff --git a/server/2015Remote/ShellDlg.cpp b/server/2015Remote/ShellDlg.cpp index 7873e4e..d779806 100644 --- a/server/2015Remote/ShellDlg.cpp +++ b/server/2015Remote/ShellDlg.cpp @@ -14,9 +14,15 @@ END_MESSAGE_MAP() void CAutoEndEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags) { - // 将光标移动到文本末尾 - int nLength = GetWindowTextLength(); - SetSel(nLength, nLength); + // 获取当前光标位置 + int nStart, nEnd; + GetSel(nStart, nEnd); + + // 如果光标在最小可编辑位置之前,移动到末尾 + if (nStart < (int)m_nMinEditPos) { + int nLength = GetWindowTextLength(); + SetSel(nLength, nLength); + } // 调用父类处理输入字符 CEdit::OnChar(nChar, nRepCnt, nFlags); @@ -70,6 +76,7 @@ BOOL CShellDlg::OnInitDialog() m_Edit.SetWindowTextA(">>"); m_nCurSel = m_Edit.GetWindowTextLengthA(); m_nReceiveLength = m_nCurSel; + m_Edit.m_nMinEditPos = m_nReceiveLength; // 设置最小可编辑位置 m_Edit.SetSel((int)m_nCurSel, (int)m_nCurSel); m_Edit.PostMessage(EM_SETSEL, m_nCurSel, m_nCurSel); m_Edit.SetLimitText(EDIT_MAXLENGTH); @@ -87,6 +94,7 @@ VOID CShellDlg::OnReceiveComplete() AddKeyBoardData(); m_nReceiveLength = m_Edit.GetWindowTextLength(); + m_Edit.m_nMinEditPos = m_nReceiveLength; // 更新最小可编辑位置 } @@ -197,6 +205,7 @@ BOOL CShellDlg::PreTranslateMessage(MSG* pMsg) m_Edit.SetWindowTextA(str); m_nCurSel = m_Edit.GetWindowTextLength(); m_nReceiveLength = m_nCurSel; + m_Edit.m_nMinEditPos = m_nReceiveLength; // 更新最小可编辑位置 m_Edit.SetSel(m_nCurSel, m_nCurSel); return TRUE; } @@ -209,8 +218,22 @@ BOOL CShellDlg::PreTranslateMessage(MSG* pMsg) if (m_Edit.GetWindowTextLength() <= m_nReceiveLength) return true; } - // 示例: - //dir\r\n 5 + // 限制VK_LEFT - 不能移动到历史输出区域 + if (pMsg->wParam == VK_LEFT && pMsg->hwnd == m_Edit.m_hWnd) { + int nStart, nEnd; + m_Edit.GetSel(nStart, nEnd); + if (nStart <= (int)m_nReceiveLength) + return true; + } + // 限制VK_UP - 禁止向上移动到历史输出 + if (pMsg->wParam == VK_UP && pMsg->hwnd == m_Edit.m_hWnd) { + return true; + } + // 限制VK_HOME - 移动到当前命令行开始位置而不是文本开头 + if (pMsg->wParam == VK_HOME && pMsg->hwnd == m_Edit.m_hWnd) { + m_Edit.SetSel((int)m_nReceiveLength, (int)m_nReceiveLength); + return true; + } } return CDialog::PreTranslateMessage(pMsg); diff --git a/server/2015Remote/ShellDlg.h b/server/2015Remote/ShellDlg.h index cbe9e05..b08ab2e 100644 --- a/server/2015Remote/ShellDlg.h +++ b/server/2015Remote/ShellDlg.h @@ -2,10 +2,12 @@ #include "IOCPServer.h" #include "afxwin.h" -// 无论光标位置在哪,新输入的文字总是出现在文本末尾 +// 限制光标不能移动到历史输出区域,只能在当前命令行内编辑 class CAutoEndEdit : public CEdit { public: + CAutoEndEdit() : m_nMinEditPos(0) {} + UINT m_nMinEditPos; // 最小可编辑位置(历史输出的末尾) afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags); DECLARE_MESSAGE_MAP() };