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: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

RSS Feeds