add CEdit control's command history

前言

看到人家输入命令的Edit控件可以用箭头选择历史命令输入, 我也加上.
我的CEdit是在状态栏上嵌入的.

UI

这里写图片描述

代码片段

// MainFrame.h : interface of the CMainFrame class
//
/////////////////////////////////////////////////////////////////////////////

#if !defined(AFX_MAINFRAME_H__44679B1F_8808_42C9_8227_5C7F57309F53__INCLUDED_)
#define AFX_MAINFRAME_H__44679B1F_8808_42C9_8227_5C7F57309F53__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#include <list>
#include "MyToolBar.h"

class CMainFrame : public CFrameWnd
{

protected: // create from serialization only
    CMainFrame();
    DECLARE_DYNCREATE(CMainFrame)

// Attributes
public:

// Operations
public:

// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CMainFrame)
    public:
    virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
    virtual BOOL PreTranslateMessage(MSG* pMsg);
    protected:
    virtual BOOL OnCommand(WPARAM wParam, LPARAM lParam);
    //}}AFX_VIRTUAL

// Implementation
public:
    virtual ~CMainFrame();
#ifdef _DEBUG
    virtual void AssertValid() const;
    virtual void Dump(CDumpContext& dc) const;
#endif

public:
    void SetFocus();

protected:  // control bar embedded members
    CStatusBar  m_wndStatusBar;
    CToolBar    m_wndToolBar;
    CMyToolBar    m_MyToolBar;

// Generated message map functions
protected:
    //{{AFX_MSG(CMainFrame)
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
    afx_msg void OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized);
    afx_msg void OnClose();
    afx_msg void OnDebugStepIn();
    afx_msg void OnDebugStepOver();
    //}}AFX_MSG
    afx_msg LRESULT OnUpdateAllViewsSafe(WPARAM wParam, LPARAM lParam);
    DECLARE_MESSAGE_MAP()

private:
    std::list<CString> m_listCmdHistory; ///< 嵌入的命令输入框的历史命令列表
    std::list<CString>::reverse_iterator m_rit_listCmdHistory; ///< 反向迭代器
};

/

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_MAINFRAME_H__44679B1F_8808_42C9_8227_5C7F57309F53__INCLUDED_)
// MainFrame.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "MyWinDebugger.h"

#include "MainFrame.h"
#include "MainDoc.h"

#include "MyDbgHelper_IF.h"
#include "UtilityHelper.h"

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

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

#define WM_MY_SETFOCUS (WM_APP + 1000)

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
    //{{AFX_MSG_MAP(CMainFrame)
    ON_WM_CREATE()
    ON_WM_ACTIVATE()
    ON_WM_CLOSE()
    ON_COMMAND(IDR_DEBUG_STEP_IN, OnDebugStepIn)
    ON_COMMAND(IDR_DEBUG_STEP_OVER, OnDebugStepOver)
    //}}AFX_MSG_MAP
    ON_MESSAGE(WM_UPDATEALLVIEWSSAFE, OnUpdateAllViewsSafe)
END_MESSAGE_MAP()

static UINT indicators[] =
{
    ID_SEPARATOR,           // status line indicator
    ID_INDICATOR_CAPS,
    ID_INDICATOR_NUM,
    ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
    // TODO: add member initialization code here
    m_rit_listCmdHistory = m_listCmdHistory.rbegin();
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    RECT rtMe;
    if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
        return -1;

    GetClientRect(&rtMe);

    if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
        /*| CBRS_GRIPPER*/ | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
        !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
    {
        TRACE0("Failed to create toolbar\n");
        return -1;      // fail to create
    }

    if (!m_MyToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_BOTTOM
        /*| CBRS_GRIPPER*/ | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
        !m_MyToolBar.LoadToolBar(IDR_MY_TOOLBAR))
    {
        TRACE0("Failed to create toolbar\n");
        return -1;      // fail to create
    }

    int index = 0;
    RECT rt;
    while (m_MyToolBar.GetItemID(index) != IDT_CMDLINE_EDIT) {
        index++;
    }

    if (m_MyToolBar.GetItemID(index) == IDT_CMDLINE_EDIT) {
        m_MyToolBar.GetItemRect(index, &rt);
        m_MyToolBar.SetButtonInfo(index, IDT_CMDLINE_EDIT, TBBS_SEPARATOR, (rtMe.right - rtMe.left) / 2);
        m_MyToolBar.GetItemRect(index, &rt);
        m_MyToolBar.m_MyEdit.MoveWindow(&rt, TRUE);
    }

    if (!m_wndStatusBar.Create(this) ||
        !m_wndStatusBar.SetIndicators(indicators,
          sizeof(indicators)/sizeof(UINT)))
    {
        TRACE0("Failed to create status bar\n");
        return -1;      // fail to create
    }

    // TODO: Delete these three lines if you don't want the toolbar to
    //  be dockable
    m_wndToolBar.EnableDocking(CBRS_ALIGN_TOP);
    m_MyToolBar.EnableDocking(CBRS_ALIGN_BOTTOM);
    EnableDocking(CBRS_ALIGN_ANY);
    DockControlBar(&m_wndToolBar);
    DockControlBar(&m_MyToolBar);

    CenterWindow();

    return 0;
}

void CMainFrame::SetFocus() {
    PostMessage(WM_MY_SETFOCUS, 0, 0);
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
    if( !CFrameWnd::PreCreateWindow(cs) )
        return FALSE;
    // TODO: Modify the Window class or styles here by modifying
    //  the CREATESTRUCT cs

    cs.cx = 800;
    cs.cy = 600;
    cs.style &= ~FWS_ADDTOTITLE;

    return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
    CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
    CFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers

BOOL CMainFrame::PreTranslateMessage(MSG* pMsg) 
{
    CString str;

    // 处理在状态栏上动态嵌入命令输入编辑框
    if (pMsg->message == WM_KEYDOWN) {   
        switch (GetFocus()->GetDlgCtrlID()) {   
            case IDT_CMDLINE_EDIT:
                if (VK_RETURN == pMsg->wParam) {
                    // 命令输入
                    m_MyToolBar.GetDlgItem(IDT_CMDLINE_EDIT)->GetWindowText(str);
                    m_MyToolBar.GetDlgItem(IDT_CMDLINE_EDIT)->SetWindowText("");
                    if (!UtilityHelper::FindInListCmdHistory(m_listCmdHistory, str)) {
                        m_listCmdHistory.push_back(str); ///< 只加入历史命令中没有的命令
                    }
                    m_rit_listCmdHistory = m_listCmdHistory.rbegin();
                    ((CMainDoc*)GetActiveDocument())->AddUserCmd(str);
                } else if (VK_DOWN == pMsg->wParam) {
                    // 命令历史的翻阅(向下)
                    if (m_rit_listCmdHistory != m_listCmdHistory.rend()) {
                        m_rit_listCmdHistory--;
                    }

                    if (m_rit_listCmdHistory == m_listCmdHistory.rend()) {
                        m_rit_listCmdHistory = m_listCmdHistory.rbegin();
                    }

                    if (m_rit_listCmdHistory != m_listCmdHistory.rend()) {
                        str = *m_rit_listCmdHistory;
                        m_MyToolBar.GetDlgItem(IDT_CMDLINE_EDIT)->SetWindowText(str);
                    }
                } else if (VK_UP == pMsg->wParam) {
                    // 命令历史的翻阅(向上)
                    if (m_rit_listCmdHistory != m_listCmdHistory.rend()) {
                        m_rit_listCmdHistory++;
                    }

                    if (m_rit_listCmdHistory == m_listCmdHistory.rend()) {
                        m_rit_listCmdHistory = m_listCmdHistory.rbegin();
                    }

                    if (m_rit_listCmdHistory != m_listCmdHistory.rend()) {
                        str = *m_rit_listCmdHistory;
                        m_MyToolBar.GetDlgItem(IDT_CMDLINE_EDIT)->SetWindowText(str);
                    }
                }
                break;
            default:
                break;
        }
    } else if (pMsg->message == WM_MY_SETFOCUS) {
        m_MyToolBar.m_MyEdit.SetFocus();
    }

    return CFrameWnd::PreTranslateMessage(pMsg);
}

BOOL CMainFrame::OnCommand(WPARAM wParam, LPARAM lParam) 
{
    // TODO: Add your specialized code here and/or call the base class

    return CFrameWnd::OnCommand(wParam, lParam);
}

void CMainFrame::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized) 
{
    CFrameWnd::OnActivate(nState, pWndOther, bMinimized);

    // TODO: Add your message handler code here

}

void CMainFrame::OnClose() 
{
    // TODO: Add your message handler code here and/or call default
    GetActiveDocument()->SetModifiedFlag(FALSE);
    CFrameWnd::OnClose();
}

afx_msg LRESULT CMainFrame::OnUpdateAllViewsSafe(WPARAM wParam, LPARAM lParam) {
    CDocument* pDoc = GetActiveDocument();
    if (NULL != pDoc) {
        ((CMainDoc*)pDoc)->UpdateAllViews(NULL);
    }

    return S_OK;
}

void CMainFrame::OnDebugStepIn() 
{
    ((CMainDoc*)GetActiveDocument())->AddUserCmd(DBG_CMD_STEP_IN);
}

void CMainFrame::OnDebugStepOver() 
{
    ((CMainDoc*)GetActiveDocument())->AddUserCmd(DBG_CMD_STEP_OVER);
}
// UtilityHelper.h: interface for the CUtilityHelper class.
//
//

#if !defined(AFX_UTILITYHELPER_H__96920D57_7E53_40E8_A868_16F4853F3A26__INCLUDED_)
#define AFX_UTILITYHELPER_H__96920D57_7E53_40E8_A868_16F4853F3A26__INCLUDED_

#include "stdafx.h"
#include <string>
#include <list>

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

class CUtilityHelper
{
public:
    CUtilityHelper();
    virtual ~CUtilityHelper();

    static CString GetModuleDir();
    static DWORD HexAlign(DWORD dwSrc, DWORD dwBase);
    static CString GetFileNamePrefixFromFilePathName(CString strFilePathName);
    static CString GetFileNameFromFilePathName(CString strFilePathName);
    static int GetStringCopyCnt(int iLenString, int iLenDstBuf);
    static std::string WToA(const WCHAR* pSrc);
    static std::wstring AToW(const char* pSrc);
    static std::string GetLastErrorMsg();
    static std::string GetErrorMsg(DWORD dwErrorValue);
};

namespace UtilityHelper {
    template<typename T>
    BOOL FindInListCmdHistory(std::list<T>& List, T ObjToFind) {
        std::list<T>::iterator it;

        for (it = List.begin(); it != List.end(); it++) {
            if (*it == ObjToFind) {
                return TRUE;
            }
        }

        return FALSE;
    }
};

#endif // !defined(AFX_UTILITYHELPER_H__96920D57_7E53_40E8_A868_16F4853F3A26__INCLUDED_)
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值