文本编辑器<二>滚动条

关键函数OnSize获取Client的x,y范围

void CMyEditorView::OnSize(UINT nType, int cx, int cy)
{
	CView::OnSize(nType, cx, cy);

	cxClient = cx;
	cyClient = cy;
}


关键函数OnPaint进行绘制

void CMyEditorView::OnPaint()
{
	CPaintDC pdc(this);

	const int FIRST_X  = 0;
	const int SECOND_X = 5*cxCaps;
	const int THIRD_X  = 20*cxCaps;
	const int FORTH_X  = 20*cxCaps + 40*cxChar;

	for (int i = iVscrollPos ; i < NUMLINES ; i++)
	{
		TCHAR                szBuffer [10] ;
		int y = cyChar * (i-iVscrollPos);

		TextOut(pdc, FIRST_X,  y,   szBuffer,wsprintf(szBuffer,TEXT("%5d"),i-iVscrollPos)) ;
		TextOut(pdc, SECOND_X, y,   sysmetrics[i].szLabel,lstrlen (sysmetrics[i].szLabel)) ;
		TextOut(pdc, THIRD_X,  y,   sysmetrics[i].szDesc,lstrlen (sysmetrics[i].szDesc)) ;

		SetTextAlign (pdc, TA_RIGHT | TA_TOP);
		TextOut(pdc, FORTH_X,  y,   szBuffer,wsprintf (szBuffer, TEXT ("%5d"),
		                            GetSystemMetrics (sysmetrics[i].index))) ;

		SetTextAlign (pdc, TA_LEFT | TA_TOP) ;
	}
}


关键函数OnVscroll获取滚动条事件

void CMyEditorView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
	switch(nSBCode)
	{
		case SB_THUMBTRACK:
			iVscrollPos = nPos;
			break;
		case SB_PAGEDOWN:
			iVscrollPos += cyClient/cyChar;
			break;
		case SB_PAGEUP:
			iVscrollPos -= cyClient/cyChar;
			break;
	}

	iVscrollPos = max(0,min(iVscrollPos,NUMLINES-1));
	if(iVscrollPos != GetScrollPos(SB_VERT))
	{
		SetScrollPos(SB_VERT,iVscrollPos,TRUE);

		RECT rect;
		GetClientRect(&rect);
		InvalidateRect(&rect);
	}

	CView::OnVScroll(nSBCode, nPos, pScrollBar);
}


 

你可能感兴趣的:(文本编辑器<二>滚动条)