小弟想实现当CEdit的内容宽度超过其自身的宽度时,让CEdit自动变宽适应其内容

解决方案 »

  1.   


    CDC* pDC = m_edit.GetDC();
    CString strText(_T(""));
    m_edit.GetWindowText(strText);
    CSize size = pDC->GetTextExtent(strText);
    m_edit.ReleaseDC(pDC);
    CRect rc;
    m_edit.GetWindowRect(&rc);
    ScreenToClient(&rc);
    rc.right = rc.left + size.cx;
    m_edit.MoveWindow(rc);可能不是很精确
      

  2.   

    的确不是很精确,我之前是用的这个方法。
    CRect rc;
    CSize size;
    HDC hDC;
    HWND hWnd;
    CString strTextValue;
    int lenStrLen;
            CFontDialog AAB;
    CEdit* edit1=(CEdit*)GetDlgItem(IDC_EDITINPUT);
    edit1->GetClientRect(&rc);
    edit1->GetWindowText(strTextValue);
    lenStrLen=edit1->GetWindowTextLength();
    hDC=::GetDC(edit1->m_hWnd);
            hWnd=edit1->m_hWnd;
    GetTextExtentPoint(hDC,strTextValue,lenStrLen,&size); 
    ::SetWindowPos(hWnd,HWND_TOP,rc.top,rc.left,rc.left+size.cx,rc.bottom+4,SWP_NOMOVE|SWP_NOZORDER); 
    ::ReleaseDC(hWnd,hDC);
    得到的效果和你的一样,不过你的代码看起来比我的简洁多了。
    我想找要实现的效果就是像搜狗输入法打字时的那样。
      

  3.   

    这个需要自绘控件实现  用系统的EDIT 确实可能存在一些问题 当然 你或许可以将(文字宽度)+(一小块宽度)来作为edit的宽度