文章标题:积累的VC编程小技巧之滚动条 |
1.设置滚动条的滚动大小创建一个基于CScrollview的SDI Project(在第6步中选CScrollview)
2.滚动条的控制BOOL CDiagramShowView::PreTranslateMessage(MSG* pMsg) { CFileTreeDoc* pDoc = (CFileTreeDoc*)GetDocument(); CPoint point = GetScrollPosition();
if(pMsg->message == WM_KEYDOWN) { switch(pMsg->wParam) { case VK_LEFT: if( point.x > 10) { EndPoint.x = EndPoint.x - 10; EndPoint.y = EndPoint.y; } else { EndPoint.x = 0; EndPoint.y = EndPoint.y; } ScrollToPosition(EndPoint); InvalidateRect(NULL,TRUE); break; case VK_RIGHT: if( point.x < pDoc->intDiagramColumnCount * pDoc->intColumnWidth - 10 ) { EndPoint.x = EndPoint.x + 10; EndPoint.y = EndPoint.y; } else { EndPoint.y = pDoc->intDiagramColumnCount * pDoc->intColumnWidth; EndPoint.x = EndPoint.x; } ScrollToPosition(EndPoint); InvalidateRect(NULL,TRUE); break; case VK_UP: if( point.y > 10) { EndPoint.y = EndPoint.y - 10; EndPoint.x = EndPoint.x; } else { EndPoint.y = 0; EndPoint.x = EndPoint.x; } ScrollToPosition(EndPoint); InvalidateRect(NULL,TRUE); break; case VK_DOWN: if( point.y < pDoc->intDiagramRowCount * pDoc->intRowHeight - 10 ) { EndPoint.y = EndPoint.y + 10; EndPoint.x = EndPoint.x; } else { EndPoint.y = pDoc->intDiagramRowCount * pDoc->intRowHeight; EndPoint.x = EndPoint.x; } ScrollToPosition(EndPoint); InvalidateRect(NULL,TRUE); break; default: break; } } return FALSE; }
// 通过正负号判断是向上还是向下滚动 if(zDelta==120) 向上滚动
BOOL CDiagramShowView::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt) { CFileTreeDoc* pDoc = (CFileTreeDoc*)GetDocument(); CPoint point = GetScrollPosition();
if(zDelta==120) { if( point.y >= 20 ) { EndPoint.x = point.x; EndPoint.y = point.y;
EndPoint.x = EndPoint.x; EndPoint.y = EndPoint.y - 20; } else { EndPoint.x = EndPoint.x; EndPoint.y = 0; } }
if(zDelta==-120) { if( point.y <= pDoc->intDiagramRowCount * pDoc->intRowHeight - 20 ) { EndPoint.x = point.x; EndPoint.y = point.y;
EndPoint.x = EndPoint.x; EndPoint.y = EndPoint.y + 20; } else { EndPoint.x = EndPoint.x; EndPoint.y = EndPoint.y; } }
ScrollToPosition(EndPoint); InvalidateRect(NULL,TRUE); return CScrollView::OnMouseWheel(nFlags, zDelta, pt); }
3.给从CWnd派生的窗口添加滚动条ModifyStyle(0,WS_VSCROLL);
4.如何用键盘滚动分割的视口我的问题是当我用鼠标滚动分割窗口时,视口滚动都很正常,但用键盘时,却什么也没有发生. 在你的视图继承类中加入如下两个函数,假定该类为CScrollerView: void CScrollerView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) BOOL CScrollerView::KeyScroll(UINT nChar) |