在使用
CScrollView
的 MFC SDI 项目中,我们能否在鼠标滚动时使 CListBox
移动,以便即使用户向上或向下滚动,列表框也会出现在相同位置(相对于可见客户区域)?
例如,即使用户向下滚动,菜单栏(文件/编辑/查看/帮助/等)也会始终出现。我想让列表框像那样定位。但据我所知,在使用初始位置创建后,无法移动
CListBox
,例如。在OnCreate()
。
如果不可能的话,有没有办法达到类似的效果?
任何建议都会有帮助。谢谢。
这里是一个简单的示例,使 CListBox 出现在相对于可见客户区域的相同位置,而不管主窗口的垂直滚动(使用 CScrollView 的 MFC SDI 项目)。
void CmyprojectView::OnDraw(CDC* pDC) {
RECT r;
// screen coord. (relative to the upper-left corner of the monitor)
m_myListbox.GetWindowRect(&r);
// screen coord -> client coord, relative to the upper-left corner of the 'visible' client area.
// (Note that `this` indicates the (instance of) View class)
this->ScreenToClient(&r);
// Again, relative to the upper-left corner of the visible client area.
m_myListbox.SetWindowPos(&wndTop, r.left, 100, r.right-r.left, r.bottom-r.top, SWP_SHOWWINDOW);
}
感谢@paddy的评论。
编辑:奇怪的是,这段代码适用于一个列表框,但如果在上面的代码中添加另一个列表框,不知何故,当我滚动到顶部时,包括两个列表框在内的所有控件都变得不可见。任何建议将不胜感激。