我的mfc应用程序的视图类继承了CSrollView类,因为它可以很容易地支持滚动条。我可以让滚动条出现在某一点,就像我放大视图中显示的东西,但是当我尝试向左滚动时,没有任何反应。我已经在网上研究了几个小时,我无法确定我做错了什么。这是视图类中的OnDraw()和OnInitialUpdate()方法:
void CWaveEditView::OnDraw(CDC* pDC)
{
CWaveEditDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
// TODO: add draw code for native data here
WaveFile * wave = &pDoc->wave;
if (wave->hdr==NULL) {
return;
}
// Get dimensions of the window.
CRect rect;
GetClientRect(rect);
// Set color in pen and brush for wave
COLORREF color = RGB( 0, 255, 0 );
CPen pen2( PS_SOLID, 0, color );
pDC->SelectObject( &pen2 );
CBrush brush2(color);
pDC->SelectObject( &brush2 );
// Draw selection if any
if (selectionStart != selectionEnd) {
pDC->Rectangle(selectionStart, 0, selectionEnd, rect.Height());
}
// Draw the wave
pDC->MoveTo(0,0);
int x;
for (x=0; x < zoomAmount*wave->lastSample/scaling; x++) {
// Assuming the whole file will be fit in the window, for every x value in the window
// we need to find the equivalent sample in the wave file.
float val = wave->get_sample((int) (x*scaling/zoomAmount) );
// We need to fit the sound also in the y axis. The y axis goes from 0 in the
// top of the window to rect.Height at the bottom. The sound goes from -32768 to 32767
// we scale it in that way.
int y = (int) ((val+32768) * (rect.Height()-1) / (32767+32768));
pDC->LineTo(x,rect.Height() - y);
}
CSize totalSize;
totalSize.cx = x;
SetScrollSizes(MM_TEXT,totalSize);
}
void CWaveEditView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
// Initial scroll sizes
CSize sizeTotal;
CRect rect;
CWaveEditDoc *doc = GetDocument();
WaveFile * wave = &doc->wave;
GetClientRect(rect);
scaling = wave->lastSample/rect.Width();
sizeTotal.cx = 700;
sizeTotal.cy = 700;
SetScrollSizes(MM_TEXT, sizeTotal);
}