首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VC/MFC >

MFC编辑框依据内容调整大小

2012-08-10 
MFC编辑框根据内容调整大小我在MFC界面上创建一个EDIT控件,我要在EDIT控件上显示一些文字,但是这些文字是

MFC编辑框根据内容调整大小
我在MFC界面上创建一个EDIT控件,我要在EDIT控件上显示一些文字,但是这些文字是不固定的,例如,第一次显示“您好欢迎使用”,但是第二次可能显示“谢谢使用”,我的问题是我要怎样才能让EDIT控件根据我显示内容的大小自动调整大小。

跪求高人指点!

[解决办法]
给你看段代码,希望能有用。

C/C++ code
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);} 

热点排行