How to add Scrolling to an ActiveX object ?

Hello !

Could you provide me with a sample program that uses scrolling feature in an ActiveX object client area ?

Alternatively, you could direct me as to sites that explains the use of scrolling in ActiveX objects.

Appreciate very much for your prompt answer and co-operation. Thank you.
pyeoAsked:
Who is Participating?
 
javanConnect With a Mentor Commented:
I'm assuming that you are starting with a stock ActiveX control wizard application.  If that is the case, the following should work for you.

The scroll bar control is a gimme since this is a CWnd derived class.  In some place in your code, you will need to turn the scroll bar on.  This place will depend on your app.  You may only need the scroll bar sometimes.  It can be turned on or off. For this example, override OnCreate and place the following code there

int CScrollCtrl::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
      if (COleControl::OnCreate(lpCreateStruct) == -1)
            return -1;
      
        //Turns the Vertical scrollbar on.
      ShowScrollBar(SB_VERT, true);
      EnableScrollBarCtrl(SB_VERT, true);
      SetScrollRange(SB_VERT, 0, m_nMaxScrollPosition);

      //The following part is not necessary but is nice.  It
        //makes it so the scroll thumb is proportionally sized
        //for the number of steps.  Few steps == bigger thumb.
      SCROLLINFO info;
      GetScrollInfo(SB_VERT, &info, SIF_PAGE);
                  
      info.fMask = SIF_PAGE;
      info.nPage = 1;
      SetScrollInfo(SB_VERT, &info);
      SetScrollPos(SB_HORZ, m_nScrollPosition);

      return 0;
}

Note the use of two variables.  I am looking at the client area as though it were several pages.  m_nScrollPosition specifies which page I am currently on.  m_nMaxScrollPosition specifies the maximum number of positions that I can have.  That is needed for initializing the scrollbar control with how many positions it can have.  Otherwise, you don't get much feedback when you click an arrow.

Next: Override WM_VSCROLL  (or HSCROLL if you doing horizontal)

void CScrollCtrl::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
  switch(nSBCode) {
    case SB_BOTTOM:                              
      break;
    case SB_TOP:
      break;
    case SB_PAGEDOWN:      //pressed in space
    case SB_LINEDOWN:      //down arrow button
                  //don't go past the bottom
      if(m_nScrollPosition <= m_nMaxScrollPosition) {
         m_nScrollPosition++;

           //update the m_nScrollPosition
         SetScrollPos(SB_VERT, m_nScrollPosition);
      }
      break;
    case SB_PAGEUP:
    case SB_LINEUP:
      if(m_nScrollPosition > 0) {
           m_nScrollPosition--;

         //update the m_nScrollPosition
         SetScrollPos(SB_VERT, m_nScrollPosition);
      }
      break;
    case SB_ENDSCROLL:      //we have completed our scrolling task
      break;
    case SB_THUMBPOSITION:      //moving the thumb
      break;
    case SB_THUMBTRACK:            //clicked on the thumb
      m_nScrollPosition = nPos;
      SetScrollPos(SB_VERT, m_nScrollPosition);
      break;
    default:                        
      break;
  }
      
  //Update the control for the new position
  InvalidateControl();
      
  COleControl::OnVScroll(nSBCode, nPos, pScrollBar);
}

Each case corresponds to user interaction with the scrollbar.  The documentation goes into more specifics on that.  I am handling the arrow and page down(clicking in space) the same way here for simplicity.  Note that each case updates the current m_nScrollPosition and then invalidates the control so it will redraw itself.

Finally, in the OnDraw code, what is displayed needs to change.
void CScrollCtrl::OnDraw(CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
{
      CBrush br;

      switch(m_nScrollPosition) {
            case 0:
                  br.CreateSolidBrush(RGB(255,0,0));
                  break;
            case 1:
                  br.CreateSolidBrush(RGB(0,255,0));
                  break;
            case 2:
                  br.CreateSolidBrush(RGB(0,0,255));
                  break;
            case 3:
                  br.CreateSolidBrush(RGB(255,0,255));
                  break;
            default:
                  br.CreateSolidBrush(RGB(0,0,0));
      }
      
      pdc->FillRect(rcBounds, &br);
      pdc->Ellipse(rcBounds);
}

In this example, I am just changing the color behing the elipse based on the page I am on.  You will need to do your own app specific work to make sure the page always shows the correct stuff.

That's about it.  If you want the whole project mailed to you, please give me an address to mail it to.  
0
 
pyeoAuthor Commented:
Hi Javan,

Your answer is sufficient for me to provide scroll feature for the ActiveX object. Once again thankyou very much for your assistance.

Peter
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.