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

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

Most Popular Programming Stories

More for Developers

RSS Feeds