MFC Multiline Edit Box 自动显示滚动条

唉,真的没办法,mfc中的Multiline Edit Box不能自动显示滚动条。如果将verticle scrollbar设置为true,则内容少时也显示了滚动条。

 

没办法,参考了这篇文章

http://www.codeguru.com/cpp/controls/editctrl/article.php/c3917

 

 

If you need a multiline Edit Control, you can implement one at design time by using the Resource Editor and setting the styles Multiline, Horizontal scroll, Auto HScroll, Vertical scroll, and Auto VScroll from the Styles Tab in Edit Properties. Or, if you create the Edit Control from code at run time, you need to set the styles WS_HSCROLL, WS_VSCROLL, ES_MULTILINE, ES_AUTOHSCROLL, and ES_AUTOVSCROLL. For displaying multiple lines, you need to enter Ctrl+RETURN interactively from the keyboard during program execution, or from the code you need to separate the lines with the sequence "\r\n", for example:

m_oEdit.SetWindowText(_T("First Line\r\nSecond Line\r\n...\r\n"));

The problem with these approaches is that the scroll bars are visible even when they are not needed; for example, when the Edit Box is empty or when the text is very short. This problem is solved by the CScrollEdit class presented in this article.

The Solution

The CScrollEdit class is derived from the CEdit MFC class. As explained before, the styles WS_HSCROLL, WS_VSCROLL, ES_MULTILINE, ES_AUTOHSCROLL, and ES_AUTOVSCROLL have to be set for proper functioning of the Edit Control. When the scroll bars are not needed for display, they can be hidden by calling the function ShowScrollBar():

ShowScrollBar(SB_HORZ, FALSE);
ShowScrollBar(SB_VERT, FALSE);

The problem is how to detect when the scroll bars are needed and when they are not needed. This is done by the function CheckScrolling(), which is the core of the CScrollEdit class:

void CScrollEdit::CheckScrolling(LPCTSTR lpszString)
{
  CRect oRect;
  GetClientRect(&oRect);
  CClientDC oDC(this);
  int iHeight=0;
  BOOL bHoriz = FALSE;
  CFont* pEdtFont = GetFont();
  if(pEdtFont != NULL)
  {
    //Determine Text Width and Height
    SIZE oSize;
    CFont* pOldFont = oDC.SelectObject(pEdtFont);
    //Determine the line Height
    oSize = oDC.GetTextExtent(CString(_T(" ")));
    iHeight = oSize.cy;
    //Text Width
    int iWidth=0, i =0;
    CString oStr;
    //Parse the string; the lines in a multiline Edit are
    //separated by "\r\n"
    while(TRUE == ::AfxExtractSubString(oStr, lpszString,
                                        i, _T('\n')))
    {
      if(FALSE == bHoriz)
      {
        int iLen = oStr.GetLength()-1;
        if(iLen >=0)
        {
          //Eliminate last '\r'
          if(_T('\r') == oStr.GetAt(iLen))
            oStr = oStr.Left(iLen);
          oSize = oDC.GetTextExtent(oStr);
          if(iWidth < oSize.cx)
            iWidth = oSize.cx;
          if(iWidth >= oRect.Width())
            bHoriz = TRUE;
        }
      }
      i++;
    }
    oDC.SelectObject(pOldFont);
    //Text Height
    iHeight *= i;
  }
  ShowHorizScrollBar(bHoriz);
  ShowVertScrollBar(iHeight >= oRect.Height());
}

The idea in this function is to parse the Edit Control's text and extract the lines (which are separated in the text by the sequence "\r\n"). Then for getting the size of each line the GetTextExtent() function is used. The height of the text is determined by multiplying the height of a line with the number of lines. If the height of the text is greater or equal than the height of the Edit Control's client rectangle then the vertical scroll bar has to be displayed, otherwise is not displayed. For displaying the horizontal scroll bar it is enough to detect just one line with the width greater than or equal to the width of the Edit Control's client rectangle.

The function CheckScrolling() is called from two places:

  1. From the function SetWindowText(), which is overloading the CWnd::SetWindowText() function:
    void CScrollEdit::SetWindowText(LPCTSTR lpszString)
    {
      CheckScrolling(lpszString);
      CEdit::SetWindowText(lpszString);
    }
    
  2. From the handler OnCheckText() which is treating the special user message UWM_CHECKTEXT:
    LRESULT CScrollEdit::OnCheckText(WPARAM wParam, LPARAM
                                                    lParam)
    {
      CString oStr;
      GetWindowText(oStr);
      CheckScrolling(oStr);
      return 0;
    }
    

    The message UWM_CHECKTEXT is posted from the OnChar() handler (the handler for the WM_CHAR message) each time the user is editing inside the Edit Control:

    void CScrollEdit::OnChar(UINT nChar, UINT nRepCnt,
                             UINT nFlags)
    {
      //Possible Text Change
      PostMessage(UWM_CHECKTEXT);
      CEdit::OnChar(nChar, nRepCnt, nFlags);
    }
    

How to Use the Code

Using the CScrollEdit class is very easy, and it is demonstrated by the accompanying demo project. You need to include the source files ScrollEdit.h and ScrollEdit.cpp in your project. In ScrollEdit.cpp, change the line

#include "ScrollEditTst.h"

to the appropriate #include for your application's header file. Then, declare the control member variable in the appropriate header file:

#include "ScrollEdit.h"
//...
CScrollEdit m_oScrollEdit;

and do subclassing in the appropriate place; for example, in the OnInitDialog() handler:

m_oScrollEdit.SubclassDlgItem(IDC_EDIT1, this);
m_oScrollEdit.SetWindowText(_T(""));

The initialization with text _T("") is necessary for the proper initialization of the scroll bar's display (in this case, they are not displayed initially).

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值