用 read-only 的 edit control 來顯示訊息
第一個遇到的問題就是哪時候要顯示 scrollbar
下面的程式會抓取 edit control 的高度和字元的高度
然後字元的高度乘上行數大於 control 高度就顯示 scrollbar
SIZE textSize;
CFont* pEdtFont = m_editControl.GetFont();
CClientDC msgDC(this);
CRect editRect;
m_editControl.GetClientRect(&editRect);
// Determine Text Width and Height
msgDC.SelectObject(pEdtFont);
textSize = msgDC.GetTextExtent(str);
// Determine the line Height
msgHeight = textSize.cy * m_editControl.GetLineCount();
// show scroll
m_editControl.ShowScrollBar(SB_VERT, msgHeight >= editRect.Height());
第二個問題是 edit control 加入字串怎麼讓 scrollbar 跟著往下跑
就是先把 edit control 定位在內容最後
然後再取代
// put the selection at the end of text
m_editControl.SetSel(m_editControl.GetWindowTextLength(),
m_editControl.GetWindowTextLength());
// replace the selection, scroll will move to the bottom
m_editControl.ReplaceSel(strText);
Append Text 參考資料
Show Scroll 參考資料
- Jan 17 Thu 2013 11:18
[MFC] Edit Control Multiline Show Scroll & Append Text
全站熱搜
留言列表
禁止留言