如何实现Rich Editl里的disabled属性的功能,即不能选中,没有光标,但是字体不变灰色在CRichEditCtrl类里没找到合适的函数,想问问经验丰富的人们,是否可以用CWnd的函数来实现这个功能。或者用属性也可以。

解决方案 »

  1.   

    EnableWindow来手动把这个控件来disable掉
      

  2.   

    但是EnableWindow之后,和disabled效果是一样的,字体变灰色了
      

  3.   

    手动disabled是不是还要进行其他的辅助设置,可是我看enblewindows函数没有什么特别需要设置的地方啊
      

  4.   

    那就继承CRichEditCtrl , 映射 WM_CHAR删除  CRichEditCtrl::OnChar(nChar, nRepCnt, nFlags);
      

  5.   

    哈哈,striking(硬撑者)方法独特~,应该是可以的。 
    自己重载CRichEditCtrl类,增加一个自定义的成员函数,该函数实现让OnChar是否起作用就OK了。
      

  6.   

    void CRichEditCtrlEx::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
    {
    // CRichEditCtrl::OnChar(nChar, nRepCnt, nFlags);
    }
      

  7.   

    首先感谢各位指点:)
    我已经按照重载类的思路做了,而且也屏蔽了OnChar函数,但是还是达不到效果
    目前效果是:可以选中显示字体,而且选中后不能消除这个选中状态。
    继续请教。。
      

  8.   

    ReadOnly后,可以选中,但不能更改。消除选中状态没问题。
    关键是阻止光标进入。在ONLBUTTON上做做文章看看。
    前阵子我也碰到这个问题。希望READONLY后,光标不能进入。
    不过,这个效果不重要。我做了个右键菜单的RICHEDIT。
    class CInPlaceRichEdit : public CRichEditCtrl
    {
    // Construction
    public:
       // CInPlaceRichEdit(CDiagramEntity *pOwner, CWnd* pParent, CRect& rect, DWORD dwStyle, UINT nID,
       //              int nRow, int nColumn, CString sInitText, UINT nFirstChar);
          CInPlaceRichEdit();
    // Attributes
    public:
     
    // Operations
    public:
         //void EndEdit();
     //void SetLogFont(LOGFONT &lgft);
     //void SerCaretForCell();
           void SetLineSpace(int LineSpace);  
         BOOL MyCanUndo();
       void ShowLineSpace();
       void SetPf( PARAFORMAT2 pf );
    // Overrides
         // ClassWizard generated virtual function overrides
         //{{AFX_VIRTUAL(CInPlaceRichEdit)
    public:
    virtual BOOL PreTranslateMessage(MSG* pMsg);
    protected:
    virtual void PostNcDestroy();
    //}}AFX_VIRTUAL
     
    // Implementation
    public:
         virtual ~CInPlaceRichEdit();
     
    // Generated message map functions
    protected:
        //{{AFX_MSG(CInPlaceRichEdit)
        afx_msg void OnKillFocus(CWnd* pNewWnd);
    afx_msg void OnSetFocus(CWnd* pNewWnd);
        afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
        afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
    afx_msg void OnVscroll();
    afx_msg void OnSetfocus();
    afx_msg void OnEditCopy();
    afx_msg void OnEditPaste();
    afx_msg void OnEditCut();
    afx_msg void OnEditUndo();
    afx_msg void OnEditSelectAll();
    afx_msg void OnEditDelete();
    afx_msg void OnUpdateEditCopy(CCmdUI* pCmdUI);
    afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
    afx_msg void OnChange();
    afx_msg void OnKeyUp(UINT nChar, UINT nRepCnt, UINT nFlags);
    afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
      

  9.   

    void CInPlaceRichEdit::OnRButtonDown(UINT nFlags, CPoint point) 
    {
        TRACE("OnRButtonDown");
    //只读不显示菜单
    if(!(GetStyle() & ES_READONLY))
    {
    CMenu mnu;
    //ClientToScreen(&point);
    //GetParent()->ScreenToClient (&point);
    ::GetCursorPos(&point);
    mnu.LoadMenu(IDR_MENURICHEDITCTRL); //设置各种使能
    //int tempretu; //刚进入 不能撤消
    if( MyCanUndo() )
    {
              mnu.EnableMenuItem(IDD_RICH_UNDO,MF_ENABLED );
    }
    else
    {
              mnu.EnableMenuItem(IDD_RICH_UNDO,MF_GRAYED );
    } //是否能全选
    CString tempcaption;
    GetWindowText(tempcaption);
    if(tempcaption.IsEmpty())
    {
              mnu.EnableMenuItem(IDD_RICH_SELECTALL,MF_GRAYED );
    }
    else
    {
              mnu.EnableMenuItem(IDD_RICH_SELECTALL,MF_ENABLED );
    }
    //是否能粘贴
    if( CanPaste() )
    {
              mnu.EnableMenuItem(IDD_RICH_PASTE,MF_ENABLED );
    }
    else
    {
              mnu.EnableMenuItem(IDD_RICH_PASTE,MF_GRAYED );
    }
    //是否有选中 来判断 是否能 剪切复制删除 
            long tempstart = 0,tempend =0 ;
    GetSel(tempstart,tempend);
            if (tempstart ==tempend ) {
              mnu.EnableMenuItem(IDD_RICH_COPY,MF_GRAYED );
              mnu.EnableMenuItem(IDD_RICH_CUT,MF_GRAYED );
              mnu.EnableMenuItem(IDD_RICH_DELETE,MF_GRAYED );
    }
    else
    {
              mnu.EnableMenuItem(IDD_RICH_COPY,MF_ENABLED );
              mnu.EnableMenuItem(IDD_RICH_CUT,MF_ENABLED );
              mnu.EnableMenuItem(IDD_RICH_DELETE,MF_ENABLED );
    }
    //mnu.GetSubMenu(1)->CheckMenuItem(IDD_RICH_COPY,MF_CHECKED); mnu.GetSubMenu(1)->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON,point.x,point.y,this); CRichEditCtrl::OnRButtonDown(nFlags, point);
    }
    }
      

  10.   

    这个我拿下来分析一下,现在我想知道的是,当我屏蔽了onchar函数时,我的rich edit的多行显示功能没有了为什么呢? 是否在屏蔽onchar函数的同时需要设置其他的功能呢?
      

  11.   

    依然没有头绪中...
    阻止光标进入,似乎可以hidecaret();
    就是无法消除选中状态,最主要的是,多行显示没有了。setwindowtext之后,只显示回车前第一行文字。
      

  12.   

    那是因为 自动换行需要你手工去做。
    光标问题可以考虑 LOCKWINDOW
      

  13.   

    //解决某行单个字符出现乱码    //解决不按回车符时 输多行内容后提交的内容有部分隐藏的问题
       CString  strfail,title;
       strLine = "";
       GetWindowText(title);
       //char buf[100];
       // Dump every line of text of the edit control.
       if( !(title.IsEmpty()) )
       {
       //CRect temprect = rect;
               CSize tempsize;
       LONG  linewidth,lineheight;   //总共只有一行
      if(1==nLineCount)
      {
                 strText = title;
     strLine = strText;
      }
      else
      {    for (i=0;i < nLineCount;i++)
       {
      int len = LineLength(LineIndex(i));//(m_pRichEdit->LineIndex(i));   {   strText = title.Left(len);
      //strLine += strText;
      //是否只剩一行
      if( title.GetLength() > len )
      {
      if(title.GetAt(len)=='\r')
      {
      //手工加入了回车换行
      title = title.Right(title.GetLength()-len-2);
      //若为空行 打印空行
      //if(len==0)
      //   strText ="\r\n";
      strLine += strText+"\r\n";
      }
      else
      {
      //自动换行
      title = title.Right(title.GetLength()-len);
      strLine += strText+"\r\n";
      }
      }
      else
      {
       strLine += title;
      }
      }
       }
      }
       }
      

  14.   

    //定义一个字符类型变量
    CString strName = _T("");
    m_MyEdit.SetWindowText(_T(""));
    Invalidate();
    for(UINT i = 0; i < svec.size(); ++i)
    {
    strName = strName + svec[i] + _T("\n");
    }
    m_MyEdit.SetWindowText(strName);这是我控制换行显示的方式
    在用原有的CRichEditCtrl时候,是有效的
    但是,继承了这个类,这样就无效了