【MFC】控件的宽度、高度自适应

    技术2025-07-14  11

    【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);

     

     【CEdit 方法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); 

     

    (src:http://www.uipower.com/bbs/thread-128063-1-1.html)

     

    最新回复(0)