自己定义了一个类:
class CMyRichEditCtrl : public CRichEditCtrl
{
public:
DECLARE_MESSAGE_MAP()
afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
}
然后在CMyView中定义了一个CMyRichEditCtrl m_rich对象,并且重写了CRichEditCtrl的OnChar消息处理函数,第一问题是在控件了输入英文时,
CRichEditCtrl::OnChar有响应,但是输入中文时没响应,这是怎么回事?同样在CMyView中不论引文还是中文都能被响应。
第二个问题,我在创建m_rich时,屏蔽了垂直滚动条WS_VSCROLL,当RichEdit中的文本长度超过程序客户区时,如果有垂直滚动条,可以下翻,没有滚动条时可以用键盘的方向键控制光标下翻,现在时有没有办法通过函数来控制内容的向下向上翻阅,达到有垂直滚动条或方向键光标的效果?
CRect rect(0,0,0,0);
m_rich.Create(ES_AUTOVSCROLL | ES_MULTILINE | ES_WANTRETURN | WS_CHILD | ES_LEFT | WS_VISIBLE ,rect,this,1);

解决方案 »

  1.   

    1 输入中文时键盘输入被输入法截获
    2 EM_SCROLL
      

  2.   

    1、试试响应WM_IME_CHAR消息
    2、CRichEditCtrl::LineScroll
      

  3.   

    第二个问题我自己解决了。现在还是第一个问题。
    响应WM_IME_CHAR消息只能针对中文输入,有时会有英文输入,这是就不需要响应WM_IME_CHAR消息了而是WM_CHAR消息了,有没有统一的解决办法?
      

  4.   


    这就是统一的解决方案,不再需要WM_CHAR了
      

  5.   

    我试了,在CRichEditCtrl中根本不会响应这个消息,不信你试试
      

  6.   

    class CMyRichEditCtrl : public CRichEditCtrl
    {
    public:
    DECLARE_MESSAGE_MAP()
    afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
    afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
    afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
    afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
    afx_msg LRESULT OnIMEChar(WPARAM wParam, LPARAM lParam);//声明
    };
    BEGIN_MESSAGE_MAP(CMyRichEditCtrl, CRichEditCtrl)
    ON_WM_CHAR()
    ON_WM_LBUTTONDBLCLK()
    ON_WM_LBUTTONDOWN()
    ON_WM_KEYDOWN()
    ON_MESSAGE(WM_IME_CHAR,OnIMEChar)//映射
    END_MESSAGE_MAP()
     LRESULT CMyRichEditCtrl::OnIMEChar(WPARAM wParam, LPARAM lParam)//定义
    {
    MessageBox(_T("aa"),0,0);
    return 0; }无论我怎么输入中文,都不会触发这个函数
      

  7.   

    还有,我发现CEdit能响应WM_IME_CHAR消息
      

  8.   

    不行就用unicode编码 响应wm_char楼主主要用onchar做什么??change不行么??如果实在不满足需求的话 只好重载了.
    重载的DefWindowProc如下∶   
              static   long   nStart=0;   
              static   long   nEnd=0;   
              static   CString   fromRichEdit;   
              static   CString   CompositionStr   =   _T("");     
        
              if   (message   ==   WM_IME_STARTCOMPOSITION)   
              {   
                      GetSel(nStart,   nEnd);     
                      GetWindowText(fromRichEdit);   
                      CompositionStr   =   _T("");   
                      //if   we   need   format   of   text,   we   should   get   it   by   StreamOut   
              }   
              if   (message   ==   WM_IME_ENDCOMPOSITION)   
              {   
                      if   (CompositionStr   !=     _T(""))   
                      {   
                              long   Count;   
                              char   *   fmtString="";   
              SetWindowText(m_hWnd,   (LPCTSTR)fromRichEdit);   
                              //we   should   set   format   of   text   by   StreamIn,   if   we   need.   
                              SetSel(nStart,nEnd);   
        
                              UINT   IMEChar;   
                              for   (Count=0;   Count<CompositionStr.GetLength();   Count++)   
                              {   
                                      IMEChar   =   (UINT)CompositionStr.GetAt(Count);   
                                      SendMessage(WM_CHAR,   IMEChar,   0);   
                              }   
                      }   
              }   
              if   (message   ==   WM_IME_COMPOSITION)   
              {   
                      if   (lParam   &   GCS_RESULTSTR)   
                      {   
                              //Get   result   Composition   String   from   IME   
                              HIMC   hIMC;   
                              HWND   hWnd;   
                              DWORD   dwSize;   
                              HGLOBAL   hstr;   
                              char   *   lpstr;   
                              LOGFONT   lplf;   
        
                              hWnd   =   m_hWnd   ;   
                              hIMC   =   ImmGetContext(hWnd);   
        
                              if   (!hIMC)     
                                    return   CRichEditCtrl::DefWindowProc(   message,   wParam,   lParam   );   
        
                              //   Get   the   size   of   the   result   string.   
                              dwSize   =   ImmGetCompositionString(hIMC,   GCS_RESULTSTR,   NULL,   0);   
        
                              hstr   =   GlobalAlloc(GHND,dwSize);   
                              if   (hstr   ==   NULL)   
                                        return   CRichEditCtrl::DefWindowProc(   message,   wParam,   lParam   );   
        
                              lpstr   =   (char   *)GlobalLock(hstr);   
                              if   (lpstr   ==   NULL)   
                                        return   CRichEditCtrl::DefWindowProc(   message,   wParam,   lParam   );   
        
                              //   Get   the   result   strings   that   is   generated   by   IME   into   lpstr.   
                              ImmGetCompositionString(hIMC,   GCS_RESULTSTR,   lpstr,   dwSize);   
                              ImmReleaseContext(hWnd,   hIMC);   
        
                                //   add   this   string   into   text   buffer   of   application   
        
                              UINT   IMEChar;   
                              for   (int   i   =   0;   i   <   dwSize/sizeof(WCHAR);   i++)   
                              {   
                                      IMEChar   =   lpstr[   i*2   ]   +   lpstr[   i*2+1   ]   *   256;   
                                      CompositionStr   =   CompositionStr   +   (dtChar)IMEChar   ;   
                              }   
        
                              GlobalUnlock(hstr);   
                              GlobalFree(hstr);   
                      }   
              }   
              return   CRichEditCtrl::DefWindowProc(   message,   wParam,   lParam   );   
      

  9.   

    首先,很感谢!其次,这个太复杂了。
    我现在只需要知道用输入法向RichEdit输入的内容如何截获?
    还有什么叫用unicode编码 响应wm_char ?我现在不是不会处理中文的wm_char消息,而是根本收不到。
      

  10.   

    为什么在普通的CView视图下,OnChar()既可以响应直接输入的英文和由输入法输入的中文呢?
    我现在有个想法,能不能在CRichEdit上再覆盖一个透明的CView呢?这样通过这个最上层的CView来接收中英文输入呢?