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

  • Hurricane Sandy was one of the most destructive natural disasters that the United States has ever experienced. Read this success story to learn how Datto protected its partners and their customers with proactive business continuity planning, heroic employee efforts, and the right mix of technology and support. With storm surges over 12 feet, winds that exceeded 90 mph, and a diameter spanning more than 900 miles, Sandy resulted in power outages to approximately 7.5 million people, and caused an estimated $50 …

  • When it comes to desktops – physical or virtual – it's all about the applications. Cloud-hosted virtual desktops are growing fast because you get local data center-class security and 24x7 access with the complete personalization and flexibility of your own desktop. Organizations make five common mistakes when it comes to planning and implementing their application management strategy. This eBook tells you what they are and how to avoid them, and offers real-life case studies on customers who didn't …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds