MFC编辑框根据内容调整大小
我在MFC界面上创建一个EDIT控件,我要在EDIT控件上显示一些文字,但是这些文字是不固定的,例如,第一次显示“您好欢迎使用”,但是第二次可能显示“谢谢使用”,我的问题是我要怎样才能让EDIT控件根据我显示内容的大小自动调整大小。
跪求高人指点!
[解决办法]
给你看段代码,希望能有用。
void CHyperLink::PositionWindow(){ if (!::IsWindow(GetSafeHwnd()) || !m_bAdjustToFit) return; // Get the current window position CRect rect; GetWindowRect(rect); CWnd* pParent = GetParent(); if (pParent) pParent->ScreenToClient(rect); // Get the size of the window text CString strWndText; GetWindowText(strWndText); CDC* pDC = GetDC(); CFont* pOldFont = pDC->SelectObject(&m_Font); CSize Extent = pDC->GetTextExtent(strWndText); pDC->SelectObject(pOldFont); ReleaseDC(pDC); // Get the text justification via the window style DWORD dwStyle = GetStyle(); // Recalc the window size and position based on the text justification if (dwStyle & SS_CENTERIMAGE) rect.DeflateRect(0, (rect.Height() - Extent.cy)/2); else rect.bottom = rect.top + Extent.cy; if (dwStyle & SS_CENTER) rect.DeflateRect((rect.Width() - Extent.cx)/2, 0); else if (dwStyle & SS_RIGHT) rect.left = rect.right - Extent.cx; else // SS_LEFT = 0, so we can't test for it explicitly rect.right = rect.left + Extent.cx; // Move the window SetWindowPos(NULL, rect.left, rect.top, rect.Width(), rect.Height(), SWP_NOZORDER);}