大家过年好!!我向大家请教一个问题了。我的一个类是继承CWnd.由于内容比较多了,需要实现滚动。代码如下:
OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
static int yPos;  
static int yChar;  

si.cbSize = sizeof (si);
si.fMask  = SIF_ALL;
GetScrollInfo ( SB_VERT, &si);
// Save the position for comparison later on
yPos = si.nPos;
switch (nSBCode)
{
// user clicked the HOME keyboard key
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:
si.nPos -= 1;
break; // user clicked the bottom arrow
case SB_LINEDOWN:
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;
break; default:
break; 
}
// Set the position and then retrieve it.  Due to adjustments
//   by Windows it may not be the same as the value set.
si.fMask = SIF_POS;
SetScrollInfo ( SB_VERT, &si, TRUE);
GetScrollInfo ( SB_VERT, &si);
// If the position has changed, scroll window and update it
if (si.nPos != yPos)
{                    
ScrollWindow( 0, yChar * (yPos - si.nPos),NULL, NULL);
UpdateWindow ();
}
//__super::OnVScroll(nSBCode, nPos, pScrollBar);
}
这个我写的代码,现在scrollbar可以移动,但是屏幕不跟滚动,一点都不动。请指教一下了。
如何解决这个问题呢?

解决方案 »

  1.   

    还要在OnPaint中根据滚动位置改变显示坐标。
      

  2.   

    yChar 没有初始化,ScrollWindow(0, 0 * (yPos - si.nPos),NULL, NULL); 
      

  3.   

    是不是在窗口内容绘制函数里坐标计算中没有计算滚动条的位置,应该通过GetScrollPos(SB_VERT);得到纵向滚动条的位置,通过GetScrollPos(SB_HORZ)得到横向滚动条的位置,并在x和y坐标中减去滚动条的位置。
      

  4.   

    ychar一直不变吗?
    那么默认初始化成0,结果就是ScollWindow相当于不动了。
      

  5.   

    请楼上几位  知道怎么处理  能不能再说的详细一些了。
    我已经初始化了
    yChar = tm.tmHeight + tm.tmExternalLeading; 
    xChar = tm.tmAveCharWidth; OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
    {
    // TODO: 在此添加消息处理程序代码和/或调用默认值
    si.cbSize = sizeof (si);
    si.fMask  = SIF_ALL;
    // Save the position for comparison later on
    GetScrollInfo ( SB_HORZ, &si);
    xPos = si.nPos;
    switch (nSBCode)
    {
    // user clicked left arrow
    case SB_LINELEFT: 
    si.nPos -= 1;
    break; // user clicked right arrow
    case SB_LINERIGHT: 
    si.nPos += 1;
    break; // user clicked the scroll bar shaft left of the scroll box
    case SB_PAGELEFT:
    si.nPos -= si.nPage;
    break; // user clicked the scroll bar shaft right of the scroll box
    case SB_PAGERIGHT:
    si.nPos += si.nPage;
    break; // user dragged the scroll box
    case SB_THUMBTRACK: 
    si.nPos = si.nTrackPos;
    break; default :
    break;
    }
    // Set the position and then retrieve it.  Due to adjustments
    //   by Windows it may not be the same as the value set.
    si.fMask = SIF_POS;
    SetScrollInfo ( SB_HORZ, &si, TRUE);
    GetScrollInfo ( SB_HORZ, &si); // If the position has changed, scroll the window 
    if (si.nPos != xPos)
    {
    ScrollWindow( xChar * (xPos - si.nPos), 0, NULL, NULL);
    }
    __super::OnHScroll(nSBCode, nPos, pScrollBar);
    }OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
    {
    // TODO: 在此添加消息处理程序代码和/或调用默认值



    si.cbSize = sizeof (si);
    si.fMask  = SIF_ALL;
    GetScrollInfo ( SB_VERT, &si);
    // Save the position for comparison later on
    yPos = si.nPos;
    switch (nSBCode)
    {
    // user clicked the HOME keyboard key
    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:
    si.nPos -= 1;
    break; // user clicked the bottom arrow
    case SB_LINEDOWN:
    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;
    break; default:
    break; 
    }
    // Set the position and then retrieve it.  Due to adjustments
    //   by Windows it may not be the same as the value set.
    si.fMask = SIF_POS;
    SetScrollInfo ( SB_VERT, &si, TRUE);
    GetScrollInfo ( SB_VERT, &si);
    // If the position has changed, scroll window and update it
    if (si.nPos != yPos)
    {                    
    ScrollWindow( 0, yChar * (yPos - si.nPos),NULL, NULL);
    UpdateWindow ();
    }
    //__super::OnVScroll(nSBCode, nPos, pScrollBar);
    }
    OnPaint()
    {
    CPaintDC dc(this); // device context for painting
    // TODO: 在此处添加消息处理程序代码
    // 不为绘图消息调用 __super::OnPaint()
    CDC *pDC;
    pDC = GetDC();
    CRect rectClient;
    GetClientRect (rectClient);
    int pImgN = m_pImgs.size();
    for(int i = 0;i<pImgN;i++)
    {
    WMLimg* pImg=m_pImgs[i];
    m_pImg[i].Draw(pDC->m_hDC,pImg->Left,pImg->Top,pImg->Width,pImg->Height);
    }
    ShowNodeText(pDC);
    /*int i = m_ers.size();
    ShowNodeText(pDC);
    CString str;
    str.Format("Node is  %d er is%d",m_number,i);*/
    //pDC->DrawText();
    //pDC->DrawText(str,&rectClient,DT_CENTER);
    int x = rectClient.Width();
    TEXTMETRIC tm;
    pDC->GetTextMetrics(&tm);
    int y = tm.tmHeight;
    //////信号
    pDC->MoveTo(2,2);
    pDC->LineTo(14,2);
    pDC->MoveTo(7,2);
    pDC->LineTo(7,y-1);
    pDC->MoveTo(10,10);
    pDC->LineTo(10,y-1);
    pDC->MoveTo(13,7);
    pDC->LineTo(13,y-1);
    pDC->MoveTo(16,4);
    pDC->LineTo(16,y-1);
    pDC->MoveTo(19,1);
    pDC->LineTo(19,y-1);
    pDC->MoveTo(2,2);
    pDC->LineTo(7,5);
    pDC->MoveTo(13,2);
    pDC->LineTo(7,5);
    ////电池
    pDC->MoveTo(x-20,2);
    pDC->LineTo(x-45,2);
    pDC->MoveTo(x-20,y-1);
    pDC->LineTo(x-45,y-1);
    pDC->MoveTo(x-45,2);
    pDC->LineTo(x-45,y);
    pDC->MoveTo(x-20,2);
    pDC->LineTo(x-20,7);
    pDC->MoveTo(x-20,y-5);
    pDC->LineTo(x-20,y-1);
    pDC->MoveTo(x-20,7);
    pDC->LineTo(x-16,7);
    pDC->MoveTo(x-20,y-5);
    pDC->LineTo(x-16,y-5);
    pDC->MoveTo(x-16,7);
    pDC->LineTo(x-16,y-4);
    pDC->MoveTo(x-38,2);
    pDC->LineTo(x-45,y-1);
    pDC->MoveTo(x-37,2);
    pDC->LineTo(x-44,y-1);
    pDC->MoveTo(x-36,2);
    pDC->LineTo(x-43,y-1);
    pDC->MoveTo(x-30,2);
    pDC->LineTo(x-37,y-1);
    pDC->MoveTo(x-29,2);
    pDC->LineTo(x-36,y-1);
    pDC->MoveTo(x-28,2);
    pDC->LineTo(x-35,y-1);
    pDC->MoveTo(x-20,2);
    pDC->LineTo(x-27,y-1);
    pDC->MoveTo(x-21,2);
    pDC->LineTo(x-28,y-1);
    pDC->MoveTo(x-22,2);
    pDC->LineTo(x-29,y-1);
    m_pCy   = 0;
    m_pCx   = 0;       这些是关于scroll  坐标计算的。以上是别的内容了
    si.cbSize = sizeof (si);
    si.fMask  = SIF_POS;
    GetScrollInfo ( SB_VERT, &si);
    yPos = si.nPos;
    // Get horizontal scroll bar position
    GetScrollInfo ( SB_HORZ, &si);
    xPos = si.nPos;
    // Find painting limits
    FirstLine = max (0, yPos + dc.m_ps.rcPaint.top / yChar);
    LastLine = min (LINES - 1, yPos + dc.m_ps.rcPaint.bottom / yChar); for (int i = FirstLine; i <= LastLine; i++)
    {
    x = xChar * (1 - xPos);
    y = yChar * (i - yPos);
    }}
      在拖动滚动条的时候,出现重影。也就是没有滚动了。在onpaint中是如何处理这段代码的吗?