场景:
在文本框中打印日志,或向ListCtrl中插入数据时,怎么样可以把滚动条时时滚动到最后一行,这样便可看到添加的新数据内容。
1. 文本框
方法I:
CWnd *pWnd = GetDlgItem(IDC_EDIT1); HWND g_hLog = pWnd->GetSafeHwnd(); ::SendMessage(g_hLog, WM_VSCROLL, MAKEWPARAM(SB_BOTTOM,0), 0);
方法II:
HWND g_hLog = ::GetDlgItem(m_hWnd, IDC_EDIT1); ::SendMessage(g_hLog, WM_VSCROLL, MAKEWPARAM(SB_BOTTOM,0), 0);
方法III:
CWnd* g_CWnd = GetDlgItem(IDC_EDIT1); int nLineCount = ((CEdit*)g_cWnd)->GetLineCount(); ((CEdit*)g_cWnd)->LineScroll(nLineCount);
2. ListCtrl
方法I:
加完数据后执行: m_ListCtrl.SendMessage(WM_VSCROLL, SB_BOTTOM, NULL); 或全局 ::SendMessage(g_hList, WM_VSCROLL, MAKEWPARAM(SB_BOTTOM,0), 0);
方法II:
m_ListCtrl.SetFocus(); //设置最后一行被选中 m_ListCtrl.SetItemState(m_ListCtrlLog.GetItemCount() - 1, LVIS_ACTIVATING | LVIS_FOCUSED | LVIS_SELECTED, LVIS_SELECTED | LVIS_FOCUSED); //滚动到最后一行 m_ListCtrl.Scroll(CSize(0, 100000));
方法III:
int nCount = m_ListCtrl->GetItemCount(); if (nCount > 0) { m_ListCtrl->EnsureVisible(nCount - 1, FALSE); }
时间: 2024-10-19 01:02:04