我在CMyWnd的自定义的Create代码:
int CMyWnd::Create(...)
{
...
int hr=CWnd::OnCreate(lpCreateStruct);
//此处添加了若干的EDIT、STATIC等子控件,已经超出CMyWnd的Height(CRect中定义的)
this->EnableScrollBar(SB_VERT,ESB_ENABLE_BOTH);
this->SetScrollRange(SB_VERT,nHeight,nTop,TRUE);
//这里nHeight为创建CMyWnd的高度,即CRect中指定的高度,nTop为CMyWnd中放置的内容的总高度。
}
消息映射:
BEGIN_MESSAGE_MAP(CDataTranPanel, CWnd)
//{{AFX_MSG_MAP(CMainFrame)
// NOTE - the ClassWizard will add and remove mapping macros here.
// DO NOT EDIT what you see in these blocks of generated code !
ON_WM_CREATE()
ON_WM_VSCROLL()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
我的处理函数:
void CMyWnd::OnVScroll(UINT nSBCode,UINT nPos,CScrollBar* pScrollBar)
{
int nMinPos;
int nMaxPos;
int nLimit;
SCROLLINFO ScrollInfo;
//以下是报错语句
//还有后面的类似的凡是使用了pScrollBar的地方都会导致异常
pScrollBar->GetScrollInfo(&ScrollInfo);
nMinPos=ScrollInfo.nMin;
nMaxPos=ScrollInfo.nMax;
//pScrollBar->GetScrollRange((LPINT)&nMinPos, (LPINT)&nMaxPos);
//nLimit = pScrollBar->GetScrollLimit();
CWnd::OnVScroll(nSBCode, nPos, pScrollBar);
return;
if(nLimit<nMaxPos)
nMaxPos=nLimit;
int curpos = pScrollBar->GetScrollPos();
AfxMessageBox("ABC");
switch (nSBCode)
{
case SB_TOP: // Scroll to far left.
curpos = nMinPos;
break;
case SB_BOTTOM: // Scroll to far right.
curpos = nMaxPos;
break;
case SB_ENDSCROLL: // End scroll.
break;
case SB_LINEUP: // Scroll left.
if (curpos > nMinPos)
curpos-=10;
break;
case SB_LINEDOWN: // Scroll right.
if (curpos < nMaxPos)
curpos+=10;
break;
case SB_PAGEUP: // Scroll one page left.
/*{
// Get the page size.
SCROLLINFO info;
pScrollBar->GetScrollInfo(&info, SIF_ALL);
case SB_THUMBPOSITION: // Scroll to absolute position. nPos is the position
curpos = nPos; // of the scroll box at the end of the drag operation.
break;
case SB_THUMBTRACK: // Drag scroll box to specified position. nPos is the
curpos = nPos; // position that the scroll box has been dragged to.
break;
}
// Set the new position of the thumb (scroll box).
//pScrollBar->SetScrollPos(curpos);