这个是比较容易实现的,处理右键消息即可
去:http://www.codeguru.com/editctrl/index.shtml看看有没有源程序

解决方案 »

  1.   

    我这里有段代码,是重写编辑框的右键菜单的,当然因为该编辑框是属于一个CComboBoxEx控件内,而且位于主框架的一个控制栏内,所以对于消息的处理有点烦琐.但是我想很相似的.
    class CMyAddressBar : public CComboBoxEx
    {
    // Construction
    public:
    CMyAddressBar();// Attributes
    public:// Operations
    public:
    afx_msg void OnEditUrlRedo();
    afx_msg void OnUpdateEditUrlRedo(CCmdUI* pCmdUI);
    afx_msg void OnEditCopy();
    afx_msg void OnEditCut();
    afx_msg void OnEditPaste();
    afx_msg void OnEditSelectAll();
    afx_msg void OnEditUndo();
    afx_msg void OnUpdateEditUndo(CCmdUI* pCmdUI);
    afx_msg void OnUpdateEditPaste(CCmdUI* pCmdUI);
    afx_msg void OnUpdateEditCut(CCmdUI* pCmdUI);
    afx_msg void OnUpdateEditCopy(CCmdUI* pCmdUI);
    afx_msg void OnUpdateEditUrlSelectAll(CCmdUI* pCmdUI);// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CMyAddressBar)
    public:
    virtual BOOL PreTranslateMessage(MSG* pMsg);
    //}}AFX_VIRTUAL// Implementation
    public:
    virtual ~CMyAddressBar(); // Generated message map functions
    protected:
    BOOL IsEditFocus(CWnd* pWnd);
    BOOL OnNewContextMenu(MSG* pMsg);
    //{{AFX_MSG(CMyAddressBar)
    //}}AFX_MSG DECLARE_MESSAGE_MAP()
    }CMyAddressBar::CMyAddressBar()
    {
    }CMyAddressBar::~CMyAddressBar()
    {
    }
    BEGIN_MESSAGE_MAP(CMyAddressBar, CComboBoxEx)
    //{{AFX_MSG_MAP(CMyAddressBar)
    //}}AFX_MSG_MAP
    // ON_CONTROL
    END_MESSAGE_MAP()/////////////////////////////////////////////////////////////////////////////
    // CMyAddressBar message handlersvoid CMyAddressBar::OnEditCopy() 
    {
    // TODO: Add your command handler code here
    // CEdit* pEdit = GetEditCtrl();
    CEdit* pEdit = (CEdit*)GetFocus();
    ASSERT(pEdit);
    ASSERT_VALID(pEdit);
    pEdit->Copy();

    }void CMyAddressBar::OnEditCut() 
    {
    // TODO: Add your command handler code here
    //CEdit* pEdit = GetEditCtrl();
    CEdit* pEdit = (CEdit*)GetFocus();
    ASSERT(pEdit);
    ASSERT_VALID(pEdit);
    pEdit->Cut();
    }void CMyAddressBar::OnEditPaste() 
    {
    // TODO: Add your command handler code here
    // CEdit* pEdit = GetEditCtrl();
    CEdit* pEdit = (CEdit*)GetFocus();
    ASSERT(pEdit);
    ASSERT_VALID(pEdit);
    ASSERT(::IsClipboardFormatAvailable(CF_TEXT));
    pEdit->Paste();

    }void CMyAddressBar::OnEditSelectAll() 
    {
    // TODO: Add your command handler code here
    // CEdit* pEdit = GetEditCtrl();
    CEdit* pEdit = (CEdit*)GetFocus();
    ASSERT(pEdit);
    ASSERT_VALID(pEdit); pEdit->SetSel(0,-1);
    }void CMyAddressBar::OnEditUndo() 
    {
    // TODO: Add your command handler code here
    CEdit* pEdit = (CEdit*)GetFocus();
    ASSERT(pEdit);
    ASSERT_VALID(pEdit);
    pEdit->Undo();

    }BOOL CMyAddressBar::PreTranslateMessage(MSG* pMsg) 
    {
    // TODO: Add your specialized code here and/or call the base class
    switch(pMsg->message)
    {
    case WM_RBUTTONDOWN:
    return OnNewContextMenu(pMsg);
    }
    return CComboBoxEx::PreTranslateMessage(pMsg);
    }BOOL CMyAddressBar::OnNewContextMenu(MSG* pMsg)
    {
    CPoint point =pMsg->pt;
    {
    if (point.x == -1 && point.y == -1){
    //keystroke invocation
    CRect rect;
    GetClientRect(rect);
    ClientToScreen(rect); point = rect.TopLeft();
    point.Offset(5, 5);
    } CMenu menu;
    VERIFY(menu.LoadMenu(IDR_ADDRESSBARPOPUP)); CMenu* pPopup = menu.GetSubMenu(0);
    ASSERT(pPopup != NULL);
    CWnd* pWndPopupOwner = this; while (pWndPopupOwner->GetStyle() & WS_CHILD)
    pWndPopupOwner = pWndPopupOwner->GetParent(); VERIFY(pPopup->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y,
    pWndPopupOwner));
    }
    return TRUE;
    }void CMyAddressBar::OnUpdateEditUndo(CCmdUI* pCmdUI) 
    {
    // TODO: Add your command update UI handler code here
    CWnd* pWnd = GetFocus();
        if (  NULL == pWnd ||  !IsEditFocus( pWnd ) ||
                (pWnd->GetStyle() & ES_READONLY) != 0 )
        {
             pCmdUI->Enable( FALSE );
        }
        else
    {
    BOOL bResult = (BOOL)::SendMessage(pWnd->GetSafeHwnd(),EM_CANUNDO,0,0);
    pCmdUI->Enable(bResult);
    // pCmdUI->Enable(::IsClipboardFormatAvailable(CF_TEXT));
    }
    }void CMyAddressBar::OnUpdateEditPaste(CCmdUI* pCmdUI) 
    {
    // TODO: Add your command update UI handler code here
    CWnd* pWnd = GetFocus();
        if (  NULL == pWnd ||  !IsEditFocus( pWnd ) ||
                (pWnd->GetStyle() & ES_READONLY) != 0 )
        {
             pCmdUI->Enable( FALSE );
        }
        else
             pCmdUI->Enable(::IsClipboardFormatAvailable(CF_TEXT));
       
    //pCmdUI->Enable(::IsClipboardFormatAvailable(CF_TEXT));
       
    }void CMyAddressBar::OnUpdateEditCut(CCmdUI* pCmdUI) 
    {
    // TODO: Add your command update UI handler code here CWnd* pWnd = GetFocus();
        if (NULL == pWnd || !IsEditFocus( pWnd ))
        {
             pCmdUI->Enable( FALSE );
        }
        else
        {
             CEdit* pEdit = (CEdit*)pWnd;
             int nBeg, nEnd;         pEdit->GetSel( nBeg, nEnd );
             pCmdUI->Enable( nBeg != nEnd );
        }
       
    }void CMyAddressBar::OnUpdateEditCopy(CCmdUI* pCmdUI) 
    {
    // TODO: Add your command update UI handler code here
    CWnd* pWnd = GetFocus();
        if (NULL == pWnd || !IsEditFocus( pWnd ))
        {
             pCmdUI->Enable( FALSE );
        }
        else
        {
             CEdit* pEdit = (CEdit*)pWnd;
             int nBeg, nEnd;         pEdit->GetSel( nBeg, nEnd );
             pCmdUI->Enable( nBeg != nEnd );
        }
    }BOOL CMyAddressBar::IsEditFocus(CWnd* pWnd)
    {
    ASSERT( pWnd != NULL );
        HWND hWnd = pWnd->GetSafeHwnd();
        if (hWnd == NULL)
    return FALSE;    TCHAR szClassName[6];
        return ::GetClassName(hWnd, szClassName, 6) &&
                 _tcsicmp(szClassName, _T("Edit")) == 0;
       
    }void CMyAddressBar::OnEditUrlRedo() 
    {
    // TODO: Add your command handler code here
    CEdit* pEdit = (CEdit*)GetFocus();
    ASSERT(pEdit);
    ASSERT_VALID(pEdit); ::SendMessage(pEdit->GetSafeHwnd(),EM_REDO,0,0);
    }void CMyAddressBar::OnUpdateEditUrlRedo(CCmdUI* pCmdUI) 
    {
    // TODO: Add your command update UI handler code here
    CWnd* pWnd = GetFocus();
        if (  NULL == pWnd ||  !IsEditFocus( pWnd ) ||
                (pWnd->GetStyle() & ES_READONLY) != 0 )
        {
             pCmdUI->Enable( FALSE );
        }
        else
    {
    BOOL bResult = (BOOL)::SendMessage(pWnd->GetSafeHwnd(),EM_CANREDO,0,0);
    pCmdUI->Enable(bResult);
    }

    }void CMyAddressBar::OnUpdateEditUrlSelectAll(CCmdUI* pCmdUI) 
    {
    // TODO: Add your command update UI handler code here
    CWnd* pWnd = GetFocus();
        if (  NULL == pWnd ||  !IsEditFocus( pWnd ) ||
                (pWnd->GetStyle() & ES_READONLY) != 0 )
        {
             pCmdUI->Enable( FALSE );
        }
        else
    {
    CEdit* pEdit = (CEdit*)pWnd;
    CString str;
    pEdit->GetWindowText(str);
    pCmdUI->Enable(!str.IsEmpty());
    }
    }在主框架实现程序中有
    BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWnd)
    ON_COMMAND(ID_EDIT_URL_COPY, OnEditCopy)
    ON_COMMAND(ID_EDIT_URL_CUT, OnEditCut)
    ON_COMMAND(ID_EDIT_URL_PASTE, OnEditPaste)
    ON_COMMAND(ID_EDIT_URL_SELECT_ALL, OnEditSelectAll)
    ON_COMMAND(ID_EDIT_URL_UNDO, OnEditUndo)
    ON_UPDATE_COMMAND_UI(ID_EDIT_URL_UNDO, OnUpdateEditUndo)
    ON_UPDATE_COMMAND_UI(ID_EDIT_URL_PASTE, OnUpdateEditPaste)
    ON_UPDATE_COMMAND_UI(ID_EDIT_URL_CUT, OnUpdateEditCut)
    ON_UPDATE_COMMAND_UI(ID_EDIT_URL_COPY, OnUpdateEditCopy)
    END_MESSAGE_MAP()class CMainFrame : public CMDIFrameWnd
    {
    DECLARE_DYNAMIC(CMainFrame)
    public:
    CMainFrame();// Attributes
    public:// Operations
    public:// Implementation
    public:
    virtual ~CMainFrame();
    #ifdef _DEBUG
    virtual void AssertValid() const;
    virtual void Dump(CDumpContext& dc) const;
    #endif
    protected:  // control bar embedded members
    CMyAddressBar m_wndURLBar;
    protected:
    afx_msg void OnEditCopy();
    afx_msg void OnEditCut();
    afx_msg void OnEditPaste();
    afx_msg void OnEditSelectAll();
    afx_msg void OnEditUndo();
    afx_msg void OnEditUrlRedo();
    afx_msg void OnUpdateEditUrlRedo(CCmdUI* pCmdUI);
    afx_msg void OnUpdateEditUrlSelectAll(CCmdUI* pCmdUI);
    afx_msg void OnUpdateEditCopy(CCmdUI* pCmdUI);
    afx_msg void OnUpdateEditCut(CCmdUI* pCmdUI);
    afx_msg void OnUpdateEditPaste(CCmdUI* pCmdUI);
    afx_msg void OnUpdateEditUndo(CCmdUI* pCmdUI); //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
    };void CMainFrame::OnEditCopy()
    {
    m_wndURLBar.OnEditCopy();
    }void CMainFrame::OnEditPaste()
    {
    m_wndURLBar.OnEditPaste();
    }void CMainFrame::OnEditCut()
    {
    m_wndURLBar.OnEditCut();
    }
    void CMainFrame::OnEditSelectAll()
    {
    m_wndURLBar.OnEditSelectAll();
    }void CMainFrame::OnEditUndo()
    {
    m_wndURLBar.OnEditUndo();
    }
    void CMainFrame::OnUpdateEditCopy(CCmdUI* pCmdUI)
    {
    m_wndURLBar.OnUpdateEditCopy(pCmdUI);
    }
    void CMainFrame::OnUpdateEditCut(CCmdUI* pCmdUI)
    {
    m_wndURLBar.OnUpdateEditCut(pCmdUI);
    }
    void CMainFrame::OnUpdateEditPaste(CCmdUI* pCmdUI)
    {
    m_wndURLBar.OnUpdateEditPaste(pCmdUI);
    }
    void CMainFrame::OnUpdateEditUndo(CCmdUI* pCmdUI)
    {
    m_wndURLBar.OnUpdateEditUndo(pCmdUI);
    }void CMainFrame::OnEditUrlRedo() 
    {
    // TODO: Add your command handler code here
    m_wndURLBar.OnEditUrlRedo();
    }void CMainFrame::OnUpdateEditUrlRedo(CCmdUI* pCmdUI) 
    {
    // TODO: Add your command update UI handler code here
    m_wndURLBar.OnUpdateEditUrlRedo(pCmdUI);
    }void CMainFrame::OnUpdateEditUrlSelectAll(CCmdUI* pCmdUI) 
    {
    // TODO: Add your command update UI handler code here
    m_wndURLBar.OnUpdateEditUrlSelectAll(pCmdUI);
    }另外MSDN中好象有篇文章是如何讲述怎么实现编辑框的右键菜单的,可以找来看看.