Exhausted! How do I convince MFC's CScrollView to scroll by whole lines, not just by pixels? I'm so desperate that I even took Jeff Procise's "Programming Windows with MFC" to copy & paste the trivial example found there. Without success...!
I'm extending a larger project with a rather simplified hexa editor and I've run into this nightmare. As it is usually the case, I'm missing something terribly small to make it work.
See the code below and the two versions of the OnSize handler which I susspect to be the source of problems. In each one of them, I'm: (a) determining the number of lines that represent a file (each line with 16 bytes, so for instance a 500 Bytes file is represented by 32 lines), and (b) setting parameters of the vertical scrollbar. If 32 lines are to be shown, I set the range to 0..32. Unfortunately, MFC takes this range as if it was in pixels instead of "lines", and I can't convince it to do otherwise in my OnDraw method.
Mentioning the OnDraw method, the problem visually seems like there was a clipping region that can't be removed by calling myDC.SelectClipRgn(NULL);. Aka, I can't draw in the area that Windows knew how to draw by itself by moving the original content. The problem, however, will quite certainly lay somewhere else...
// MFC message map set correctly
afx_msg int CHexaEditor::OnCreate(LPCREATESTRUCT lpcs){
// window created; instantiating the window manually
if (CScrollView::OnCreate(lpcs)==-1) return -1;
m_nMapMode=MM_TEXT; // reason - see OnSize version 2
m_lineDev=CSize(0,1); // reason - see OnSize version 2
}
void CHexaEditor::PostNcDestroy(){
// window ready to be destroyed
//nop (I'll do the job)
}
// OnSize handler version 1 (making use of MFC methods)
afx_msg void CHexaEditor::OnSize(UINT nType,int cx,int cy){
// window size changed
nLinesTotal=...;
SetScrollSizes( MM_TEXT,
CSize(0,nLinesTotal),
...page size...,
CSize(0,1) // scroll up & down by one line
);
ShowScrollBar(SB_VERT,TRUE); // force visible
}
// OnSize handler version 2 (making use of API functions)
afx_msg void CHexaEditor::OnSize(UINT nType,int cx,int cy){
// window size changed
nLinesTotal=...;
SCROLLINFO si={ sizeof(si), SIF_RANGE|SIF_PAGE, ... };
SetScrollInfo( SB_VERT, &si, FALSE );
ShowScrollBar(SB_VERT,TRUE); // force visible
}
Any help apreciated, thanks in advance!
Tomas