为什么我的滚动条只向一个方向滚动?

时间:2014-03-30 03:34:51

标签: c++ winapi scrollbar paint

我试图滚动在动态循环中创建的编辑字段子窗口,如下所示:

 for(int x = 0; x<//Some Predefined Number of Windows decided by the user; x ++)
            {
                int m = //Some Predefined spacing determined by the Programmer
                EditBoxes = CreateWindow("Edit","Witness ",
                                         WS_CHILD|WS_VISIBLE|ES_AUTOHSCROLL|WS_TABSTOP ,0,
                                         m,250,16,hwndx,NULL,GetModuleHandle(NULL),NULL);
            }

Heres我是如何尝试使用ScrollWindowEx:

 case WM_VSCROLL:

{
    xNewPos = si.nPos;
     si.cbSize = sizeof(&si);
    si.fMask = SIF_ALL;
    GetScrollInfo(hwnd,SB_VERT,&si);





    switch (LOWORD(wParam))
    {
   case SB_TOP:
        si.nPos = si.nMin;
        break;

    // User clicked the END keyboard key.
    case SB_BOTTOM:
        si.nPos = si.nMax;
        break;

    // User clicked the top arrow.
    case SB_LINEUP:

        if(si.nPos>si.nMin)
        si.nPos = si.nPos-1;
        break;

    // User clicked the bottom arrow.
    case SB_LINEDOWN:
        if(si.nPos<si.nMax)
        si.nPos -= 1;
        break;

    // User clicked the scroll bar shaft above the scroll box.
    case SB_PAGEUP:
        si.nPos -= si.nPage;
        break;

    // User clicked the scroll bar shaft below the scroll box.
    case SB_PAGEDOWN:
        si.nPos += si.nPage;
        break;

    // User dragged the scroll box.
    case SB_THUMBTRACK:
        si.nPos = si.nTrackPos;
        cout << si.nTrackPos;
        break;

    default:

       break;
    }
    si.fMask = SIF_POS;


     SetScrollInfo(hwnd,SB_VERT,&si,TRUE);
     SetScrollPos(hwnd,SB_VERT,si.nPos,TRUE);

    if(si.nPos != xNewPos)
    {

    ScrollWindowEx(hwnd,0,si.nPos,(RECT*)NULL,NULL,NULL,NULL, SW_SCROLLCHILDREN| SW_INVALIDATE| SW_ERASE );

    }

我能够让滚动条向下或向上滚动,具体取决于我是否在ScrollWindowEx函数中使si.nPos为负或正,但我无法使窗口向上或向下滚动。

1 个答案:

答案 0 :(得分:2)

// User clicked the top arrow.
case SB_LINEUP:

    if(si.nPos>si.nMin)
    si.nPos = si.nPos-1;
    break;

// User clicked the bottom arrow.
case SB_LINEDOWN:
    if(si.nPos<si.nMax)
    si.nPos -= 1;
    break;

请注意,对于换行和换行都使用相同的表达式。在这两种情况下,你都会减少头寸。对于线下,你需要增加而不是减少。

我会这样写:

// User clicked the top arrow.
case SB_LINEUP:

    if(si.nPos>si.nMin)
        si.nPos--;
    break;

// User clicked the bottom arrow.
case SB_LINEDOWN:
    if(si.nPos<si.nMax)
        si.nPos++;
    break;