[英]CScrollbar SetScrollInfo has no effect
我對此有類似的問題: 如何使用MFC CScrollbar控件? 但是我發現我的ON_WM_VSCROLL
消息正在發送參數nPos
始終等於0。我認為我應該使用SetScrollInfo
方法或至少使用SetScrollRange
設置滾動條,然后嘗試在View類函數的PreCreateWindow()
進行設置(這是從CFormView
派生的。
但是,滾動條似乎並沒有從SCROLLINFO
結構獲取數據。
這是我的代碼示例:
BOOL CInterfaceView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
drawphoto=false; //other unrelated variables;
zoomfactor=1.0;
info1.cbSize=sizeof(SCROLLINFO); //SCROLLINFO global variable
info1.fMask=SIF_ALL;
info1.nMin=0;
info1.nMax=100;
info1.nPage=2;
info1.nPos=5;
info1.nTrackPos=2;
ScrollBar1.SetScrollInfo(&info1); //the vertical ScrollBar
// ScrollBar1.SetScrollRange(0,100); //this has no effect either
return CFormView::PreCreateWindow(cs);
}
VSCROLL消息處理程序:
void CInterfaceView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
int CurPos = pScrollBar->GetScrollPos();
//debug code:
CString test;
int rn,rx;
pScrollBar->GetScrollRange(&rn,&rx);
test.Format(_T("%d %d %d\n"),nPos,CurPos,rx-rn);
if(pScrollBar!=NULL)
TRACE(test+_T(" dzialamy\n"));
//end debug code
//this part found on the Internet
// Determine the new position of scroll box.
switch (nSBCode)
{
case SB_LEFT: // Scroll to far left.
CurPos = 0;
break;
case SB_RIGHT: // Scroll to far right.
CurPos = 100;
break;
case SB_ENDSCROLL: // End scroll.
break;
case SB_LINELEFT: // Scroll left.
if (CurPos > 0)
CurPos--;
break;
case SB_LINERIGHT: // Scroll right.
if (CurPos < 100)
CurPos++;
break;
case SB_PAGELEFT: // Scroll one page left.
{
// Get the page size.
SCROLLINFO info;
ScrollBar1.GetScrollInfo(&info, SIF_ALL);
if (CurPos > 0)
CurPos = max(0, CurPos - (int) info.nPage);
}
break;
case SB_PAGERIGHT: // Scroll one page right
{
// Get the page size.
SCROLLINFO info;
ScrollBar1.GetScrollInfo(&info, SIF_ALL);
if (CurPos < 100)
CurPos = min(100, CurPos + (int) info.nPage);
}
break;
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).
ScrollBar1.SetScrollPos(50); //orignally it was CurPos
CFormView::OnVScroll(nSBCode, 50, pScrollBar);
// ScrollBar1.SetScrollPos(nPos);
}
因此,我懷疑是嘗試將滾動條設置在錯誤的位置還是做錯了什么? 感謝您的幫助。
在創建窗口(及其滾動條)之前,將調用PreCreateWindow。 在視圖類中,您應該在OnInitialUpdate中進行初始化。 在創建窗口之后但在窗口變為可見之前調用此方法。
我認為PreCreateWindow()
還為時過早,無法設置滾動條,“正確的”位置應該是CDocument派生的類加載/修改了會影響滾動條范圍的數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.