gpt4 book ai didi

c++ - CScrollbar SetScrollInfo 无效

转载 作者:行者123 更新时间:2023-11-30 04:02:09 24 4
gpt4 key购买 nike

我有类似的问题:How do you use MFC CScrollbar controls?但我发现我的 ON_WM_VSCROLL 消息正在发送参数 nPos 始终等于 0。我认为我应该使用 SetScrollInfo 方法或在至少使用 SetScrollRange,我尝试在 View 类函数(派生自 CFormView)的 PreCreateWindow() 中执行此操作。

然而,滚动条似乎没有从 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 在创建窗口(及其滚动条)之前被调用。在 View 类中,您应该在 OnInitialUpdate 中进行初始化。这是在窗口创建之后但在窗口变得可见之前调用的。

关于c++ - CScrollbar SetScrollInfo 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25363059/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com