前言
看到人家输入命令的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_)