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

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Java developers know that testing code changes can be a huge pain, and waiting for an application to redeploy after a code fix can take an eternity. Wouldn't it be great if you could see your code changes immediately, fine-tune, debug, explore and deploy code without waiting for ages? In this white paper, find out how that's possible with a Java plugin that drastically changes the way you develop, test and run Java applications. Discover the advantages of this plugin, and the changes you can expect to see …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds