Create an Active Cursor on the Scroll Bar

Environment: VC6 SP5, VC.NET, Windows 2000

This code enables you to change the cursor dynamically on the scroll bar of the window, with scrolling capabilities. The variable m_nCursorID is used to prevent multiple loading of the cursor.

CMyWnd is derived from CWnd.

UINT m_nCursorID;    //Current ID of the cursor resource

CMyWnd::CMyWnd()
{
  m_nCursorID = 0;
}

BOOL CMyWnd::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
  if (nHitTest == HTVSCROLL)
  {
    //Change cursor on the vertical scroll bar
    if(m_nCursorID != IDC_VSCROLLBAR)
    {
      SetCursor(AfxGetApp()->LoadCursor(IDC_VSCROLLBAR));
      m_nCursorID = IDC_VSCROLLBAR;
    }
    return true;
  }
  else if (nHitTest == HTHSCROLL)
  {
    //Change cursor on the horizontal scroll bar
    if(m_nCursorID != IDC_HSCROLLBAR)
    {
      SetCursor(AfxGetApp()->LoadCursor(IDC_HSCROLLBAR));
      m_nCursorID = IDC_HSCROLLBAR;
    }
    return true;
  }
  else
  {
    if(m_nCursorID != 0) m_nCursorID = 0;
    return BaseClass::OnSetCursor(pWnd, nHitTest, message);
  }

}

void CMyWnd::OnVScroll(UINT nSBCode, UINT nPos,
                       CScrollBar* pScrollBar)
{
  switch (nSBCode) {
  case SB_THUMBTRACK:    //Drag scroll box to specified position
    if(m_nCursorID != IDC_VTHUMBTRACK)
    {
      SetCursor(AfxGetApp()->LoadCursor(IDC_VTHUMBTRACK));
      m_nCursorID = IDC_VTHUMBTRACK;
    }
    break;
  case SB_LINEDOWN:      //Scroll one line down
    if(GetScrollPos(SB_VERT) == GetScrollLimit(SB_VERT))
    {
      if(m_nCursorID != IDC_VLINEDOWN_DISABLE)
      {
        SetCursor(AfxGetApp()->LoadCursor(IDC_VLINEDOWN_DISABLE));
        m_nCursorID = IDC_VLINEDOWN_DISABLE;
      }
    }
    else
    {
      if(m_nCursorID != IDC_VLINEDOWN)
      {
        SetCursor(AfxGetApp()->LoadCursor(IDC_VLINEDOWN));
        m_nCursorID = IDC_VLINEDOWN;
      }
      }
    break;
  case SB_LINEUP:        //Scroll one line up
    SCROLLINFO  info;
    info.cbSize = sizeof(SCROLLINFO);
    GetScrollInfo(SB_VERT, &info);
    if(info.nPos == info.nMin)
    {
      if(m_nCursorID != IDC_VLINEUP_DISABLE)
      {
        SetCursor(AfxGetApp()->LoadCursor(IDC_VLINEUP_DISABLE));
        m_nCursorID = IDC_VLINEUP_DISABLE;
      }
    }
    else
    {
      if(m_nCursorID != IDC_VLINEUP)
      {
        SetCursor(AfxGetApp()->LoadCursor(IDC_VLINEUP));
        m_nCursorID = IDC_VLINEUP;
      }
    }
    break;
  case SB_PAGEDOWN:      //Scroll one page down
    if(m_nCursorID != IDC_VPAGEDOWN)
    {
      SetCursor(AfxGetApp()->LoadCursor(IDC_VPAGEDOWN));
      m_nCursorID = IDC_VPAGEDOWN;
    }
    break;
  case SB_PAGEUP:        //Scroll one page up
    if(m_nCursorID != IDC_VPAGEUP)
    {
      SetCursor(AfxGetApp()->LoadCursor(IDC_VPAGEUP));
      m_nCursorID = IDC_VPAGEUP;
    }
    break;
  }
  BaseClass::OnVScroll(nSBCode, nPos, pScrollBar);
}

void CMyWnd::OnHScroll(UINT nSBCode, UINT nPos,
                       CScrollBar* pScrollBar)
{
  switch (nSBCode) {
  case SB_THUMBTRACK:    //Drag scroll box to specified position
    if(m_nCursorID != IDC_HTHUMBTRACK)
    {
      SetCursor(AfxGetApp()->LoadCursor(IDC_HTHUMBTRACK));
      m_nCursorID = IDC_HTHUMBTRACK;
    }
    break;
  case SB_LINEDOWN:      //Scroll one line down
    if(GetScrollPos(SB_HORZ) == GetScrollLimit(SB_HORZ))
    {
      if(m_nCursorID != IDC_HLINEDOWN_DISABLE)
      {
        SetCursor(AfxGetApp()->LoadCursor(IDC_HLINEDOWN_DISABLE));
        m_nCursorID = IDC_HLINEDOWN_DISABLE;
      }
    }
    else
    {
      if(m_nCursorID != IDC_HLINEDOWN)
      {
        SetCursor(AfxGetApp()->LoadCursor(IDC_HLINEDOWN));
        m_nCursorID = IDC_HLINEDOWN;
      }
    }
    break;
  case SB_LINEUP:        //Scroll one line up
    SCROLLINFO  info;
    info.cbSize = sizeof(SCROLLINFO);
    GetScrollInfo(SB_HORZ, &info);
    if(info.nPos == info.nMin)
    {
      if(m_nCursorID != IDC_HLINEUP_DISABLE)
      {
        SetCursor(AfxGetApp()->LoadCursor(IDC_HLINEUP_DISABLE));
        m_nCursorID = IDC_HLINEUP_DISABLE;
      }
    }
    else
    {
      if(m_nCursorID != IDC_HLINEUP)
      {
        SetCursor(AfxGetApp()->LoadCursor(IDC_HLINEUP));
        m_nCursorID = IDC_HLINEUP;
      }
    }
    break;
  case SB_PAGEDOWN:      //Scroll one page down
    if(m_nCursorID != IDC_HPAGEDOWN)
    {
      SetCursor(AfxGetApp()->LoadCursor(IDC_HPAGEDOWN));
      m_nCursorID = IDC_HPAGEDOWN;
    }
    break;
  case SB_PAGEUP:        //Scroll one page up
    if(m_nCursorID != IDC_HPAGEUP)
    {
      SetCursor(AfxGetApp()->LoadCursor(IDC_HPAGEUP));
      m_nCursorID = IDC_HPAGEUP;
    }
    break;
  }
  BaseClass::OnHScroll(nSBCode, nPos, pScrollBar);
}

Downloads

Download demo project - 37 Kb


Comments

  • Very good code and article format !!! Level A article in CodeGuru.com

    Posted by Legacy on 08/11/2003 12:00am

    Originally posted by: Zhefu Zhang

    hope more article from you. Great job!

    Reply
  • very nice

    Posted by Legacy on 08/03/2002 12:00am

    Originally posted by: piers

    good work,I appreciate you!

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • This paper introduces IBM Java on the IBM PowerLinux 7R2 server and describes IBM's implementation of the Java platform, which includes IBM's Java Virtual Machine and development toolkit.

  • Agile methodologies give development and test teams the ability to build software at a faster rate than ever before. Combining DevOps with hybrid cloud architectures give teams not just the principles, but also the technology necessary to achieve their goals. By combining hybrid cloud and DevOps: IT departments maintain control, visibility, and security Dev/test teams remain agile and collaborative Organizational barriers are broken down Innovation and automation can thrive Download this white paper to …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds