OnkeyDown中只能实现数字和字母的编辑,编辑汉字时为什么是乱码?

解决方案 »

  1.   

    编辑汉字时?输入汉字吧?
    WM_CHAR
      

  2.   

    OnkeyDown捕捉的是每次按键,你一个字不论是五笔还是拼音都要打好多下呢,所以捕捉不了OnChar函数可以帮你捕捉
      

  3.   

    用VC实现记事本的功能,但是切换到中文输入时每按一次键盘就响应了,且界面上出现乱码,用OnChar捕捉可以说的详细点吗?能否实现按空格或回车再响应?
      

  4.   

    在输入法下“能否实现按空格或回车再响应”是给输入法的与你无关。
    你用 WM_CHAR 或 WM_IMM_CHAR 可以得到输入法给你的字符。
      

  5.   

    用VC实现记事本的功能,但是切换到中文输入时每按一次键盘就响应了,且界面上出现乱码,WM_CHAR得到输入法的字符可以给我详细解释一下吗,新手拜托啦~~
      

  6.   

     [color=#FF00FF]我写了一个如下的函数可还是乱码,能否帮我详细说明一下,新手拜托啦~~[/color] void CXXXView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)  
    {  
    // TODO: 在此添加消息处理程序代码和/或调用默认值  
    CClientDC dc(this);  
    TEXTMETRIC tm;  
    dc.GetTextMetrics(&tm);  if(0x0d==nChar) //回车  
    {  
    ...  
    }  
    else if(0x08==nChar) //退格  
    {  
    ...  
    }  
    else  
    {  
    m_strLine += (TCHAR)nChar; //累加输入的字符  
    }  CSize sz=dc.GetTextExtent(m_strLine);  
    CPoint pt;  
    pt.x=m_ptOrigin.x+sz.cx;  
    pt.y=m_ptOrigin.y;  
    SetCaretPos(pt); //光标重新定位  dc.TextOutW(m_ptOrigin.x, m_ptOrigin.y, m_strLine); //输出  CView::OnChar(nChar, nRepCnt, nFlags);  
    }  
      

  7.   

    void CXXXView::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
    {
       afxDump << nChar << "\n";
    }
      

  8.   

    要实现记事本功能,为啥不使用EDIT控件呢?
      

  9.   

    我是要实现记事本一样的界面,功能也要与记事本一致,可以在我的软件中将它保存成txt文件,不是对话框,嘻嘻~但是还有好多问题呢,除了汉字的编辑,有些按键也有问题,比如按了F6再按其他按键就无反应了,还有每输入一个字符界面就会闪一下,用了双缓冲也没用,哎~~
      

  10.   

    大侠有空的话能否再帮小妹解释一下啊,写了OnChar还是会有乱码哦,除了汉字的编辑,有些按键也有问题,比如按了F6再按其他按键就无反应了,还有每输入一个字符界面就会闪一下,用了双缓冲也没用~~有知道的能否指点一下,比较笨,拜托啦~~
      

  11.   

    自己搞edit
    汉字输入时(2个字节)
    在WM_CHAR 要:
    查是不是LeadingByte 即第1字节是不是 》0x80
    还要用 PeekMessage (WM_CHAR) 再取一个字符。
    2个字节都>0x80
    就是汉字
    再显示帮助中有个sdk 的 editor 叫 halfime 你找找看。
      

  12.   

    你所做的一切就是在重走CEdit的路
    直接用吧!
    而且你想改变记事本的尺寸直接响应OnSize就行了
    然后CEdit的容器你用CFormView
    不要用CDialog了
      

  13.   

    即便是基于单文档的MFC,也可以使用EDIT控件呢,不一定非得基于对话框的
      

  14.   

    怎么我的老掉(12楼,13楼)?
    帮助中搜索“halfime 和 Fullime”
      

  15.   

    ??你是在回答我的编辑汉字乱码的问题吗?你说的“用 WM_CHAR 或 WM_IMM_CHAR 可以得到输入法给你的字符”挺有道理的,可是能麻烦具体点吗?小妹急用~~
      

  16.   

    在帮助中搜索“halfime 和 Fullime”
    halfime 是一个sdk的editor。
    可以输入汉字
      

  17.   

    我在View中用了Onchar确实能得到我输的汉字字符了,可还是会有其他的乱码一起出现,它好像既响应了OnKeyDown又响应了Onar,不知道如何处理,谢谢你的耐心讲解~~小妹比较笨,O(∩_∩)O嘻嘻~
      

  18.   

    我VC6在帮助中搜索halfime就可以找到的。
      

  19.   

    给点halfime中的代码:
    case WM_CHAR: 
    CharHandler( hWnd, wParam ); 
    break; /************************************************************************ 

    *   CharHandler - WM_CHAR handler 

    ************************************************************************/ 
     
    void CharHandler( HWND hWnd, UINT wParam ) 

        unsigned char ch = (unsigned char)wParam; 
     
        // 
        // Because DBCS characters are usually generated by IMEs (as two 
        // PostMessages), if a lead byte comes in, the trail byte should 
        // arrive very soon after.  We wait here for the trail byte and 
        // store them into the text buffer together. 
     
        if ( IsDBCSLeadByte( ch ) ) { 
     
    // 
    // Wait an arbitrary amount of time for the trail byte to 
    // arrive.  If it doesn't, then discard the lead byte. 
    // 
    // This could happen if the IME screwed up.  Or, more likely, 
    // the user generated the lead byte through ALT-numpad. 
    // 
     
    MSG msg; 
    int i = 10; 
     
    while (!PeekMessage((LPMSG)&msg, hWnd, WM_CHAR, WM_CHAR, PM_REMOVE)) { 
        if ( --i == 0 ) 
    return; 
        Yield(); 

     
    StoreDBCSChar( hWnd, (WORD)(((unsigned)(msg.wParam)<<8) | (unsigned)ch )); 
     
        } else { 
     
    switch( ch ) 

    case '\r': 
    case '\t': 
    case '\b': 
     
        // 
        // Throw away.  Already handled at WM_KEYDOWN time. 
        // 
     
        break; 
     
    default: 
        StoreChar( hWnd, ch ); 
        break; 

        } 
    } /************************************************************************ 

    *   IsDBCSTrailByte - returns TRUE if the given byte is a DBCS trail byte 

    *                     The algorithm searchs backward in the string, to some 
    *                     known character boundary, counting consecutive bytes 
    *                     in the lead byte range. An odd number indicates the 
    *                     current byte is part of a two byte character code. 

    *   INPUT: PCHAR  - pointer to a preceding known character boundary. 
    *          PCHAR  - pointer to the character to test. 

    *   OUTPUT:BOOL   - indicating truth of p==trailbyte. 

    ************************************************************************/ BOOL IsDBCSTrailByte( char *base, char *p ) 

        int lbc = 0;    // lead byte count 

        assert(base <= p); 

        while ( p > base ) { 
    if ( !IsDBCSLeadByte(*(--p)) ) 
    break; 
    lbc++; 
        } 

        return (lbc & 1); 
    } /************************************************************************ 

    *   StoreChar - Stores one SBCS character into text buffer and advances 
    *               cursor 

    ************************************************************************/ void StoreChar( HWND hWnd, BYTE ch ) 

        int i; 
        HDC hdc; 

        // 
        // If insert mode, move rest of line to the right by one 
        // 

        if ( fInsertMode ) { 
    for ( i = LASTCOL; i > xPos; i-- ) 
    textbuf[yPos][i] = textbuf[yPos][i-1]; 

    // 
    // If the row ends on a lead byte, blank it out 
    // To do this we must first traverse the string 
    // starting from a known character boundry until 
    // we reach the last column. If the last column 
    // is a character boundry then the last character 
    // is either a single byte or a lead byte 
    // 

    for ( i = xPos+1; i < LASTCOL; ) { 
    if ( IsDBCSLeadByte( textbuf[yPos][i] ) ) 
    i++; 
    i++; 

    if (i==LASTCOL) 
    if ( IsDBCSLeadByte( textbuf[yPos][LASTCOL] ) ) 
    textbuf[yPos][LASTCOL] = ' '; 

        } else {  // overtype mode 

    if ( IsDBCSLeadByte( textbuf[yPos][xPos] ) ) 

    // 
    // Blank out trail byte 
    // 

    textbuf[yPos][xPos+1] = ' '; 

    // 
    // or shift line left on character and blank last column 
    // 
    // for ( i = xPos+1; i < LASTCOL; i++ ) 
    //     textbuf[yPos][i] = textbuf[yPos][i+1]; 
    // textbuf[yPos][LASTCOL] = ' '; 

        } 

        // 
        // Store input character at current caret position 
        // 

        textbuf[yPos][xPos] = ch; 


        // 
        // Display input character. 
        // 

        hdc = GetDC( hWnd ); 
        HideCaret( hWnd ); 
        TextOut( hdc, xPos*cxMetrics, yPos*cyMetrics, 
    &(textbuf[yPos][xPos]), MAXCOL-xPos ); 
        ShowCaret( hWnd ); 
        ReleaseDC( hWnd, hdc ); 

        SendMessage( hWnd, WM_KEYDOWN, VK_RIGHT, 1L );