SCROLLBAR Help Again

I am using Microsoft Visual C++ 5.0 and running a Win32 Application. I am trying to get mulitple scroll bars to work independently.

Could somebody please modify and Build the following program to make the scrollbars work properly?

Thanks
Randy


#include <afxwin.h>
#include <strstrea.h>

const int IDC_SB1=100;
const int IDC_SB2=101;
const int IDC_SB3=102;
const int IDC_SB4=103;

const int MIN_RANGE=0;
const int MAX_RANGE=100;

// Define an application object
class CApp : public CWinApp
{
public:
      virtual BOOL InitInstance();
};

// Create an instance of the application object
CApp App;  

// Define the window object
class CWindow : public CFrameWnd
{
      CScrollBar* sb1;
      CScrollBar* sb2;
      CScrollBar* sb3;
      CScrollBar* sb4;
public:
      CWindow();
      afx_msg void OnHScroll(UINT nSBCode,UINT nPos, CScrollBar* pScrollBar);
      afx_msg void OnHScroll1(UINT nSBCode1,UINT nPos1, CScrollBar* pScrollBar1);
      afx_msg void OnHScroll2(UINT nSBCode2,UINT nPos2, CScrollBar* pScrollBar2);
      afx_msg void OnHScroll3(UINT nSBCode3,UINT nPos3, CScrollBar* pScrollBar3);
      DECLARE_MESSAGE_MAP()
};

// The message map
BEGIN_MESSAGE_MAP( CWindow, CFrameWnd )
      ON_WM_HSCROLL()
END_MESSAGE_MAP()

// Window constructor
CWindow::CWindow()
{
      // Create the window
      Create(NULL, "c2f", WS_OVERLAPPEDWINDOW,
            CRect(0,0,208,208));

      // Create the fahrenheit label
      
      // Create the scroll bar
      sb1 = new CScrollBar();
      sb1->Create(WS_CHILD|WS_VISIBLE|SBS_HORZ,
            CRect(0,10,200,40),
            this, IDC_SB1);
      sb1->SetScrollRange(MIN_RANGE,MAX_RANGE,TRUE);
      sb1->SetScrollPos(32);

      // Create the scroll bar
      sb2 = new CScrollBar();
      sb2->Create(WS_CHILD|WS_VISIBLE|SBS_HORZ,
            CRect(0,50,200,80),
            this, IDC_SB2);
      sb2->SetScrollRange(MIN_RANGE,MAX_RANGE,TRUE);
      sb2->SetScrollPos(32);

      // Create the scroll bar
      sb3 = new CScrollBar();
      sb3->Create(WS_CHILD|WS_VISIBLE|SBS_HORZ,
            CRect(0,90,200,120),
            this, IDC_SB3);
      sb3->SetScrollRange(MIN_RANGE,MAX_RANGE,TRUE);
      sb3->SetScrollPos(32);

      // Create the scroll bar
      sb4 = new CScrollBar();
      sb4->Create(WS_CHILD|WS_VISIBLE|SBS_HORZ,
            CRect(0,130,200,160),
            this, IDC_SB4);
      sb4->SetScrollRange(MIN_RANGE,MAX_RANGE,TRUE);
      sb4->SetScrollPos(32);

}

// Handle the horizontal scroll bar
void CWindow::OnHScroll( UINT nSBCode,UINT nPos, CScrollBar* pScrollBar )
{
      int pos;

      pos = pScrollBar->GetScrollPos();
      switch ( nSBCode )
      {
            case SB_LINEUP:
                  pos -= 1;
                  break;
            case SB_LINEDOWN:
                  pos += 1;
                  break;
            case SB_PAGEUP:
                     pos -= 10;
                  break;
            case SB_PAGEDOWN:
                  pos += 10;
                  break;
            case SB_TOP:
                  pos = MIN_RANGE;
                  break;
            case SB_BOTTOM:
                  pos = MAX_RANGE;
                  break;
            case SB_THUMBPOSITION:
                  pos = nPos;
                  break;
            default:
                  return;
      }
      if ( pos < MIN_RANGE )
            pos = MIN_RANGE;
      else if ( pos > MAX_RANGE )
            pos = MAX_RANGE;
      sb1->SetScrollPos( pos, TRUE );

}

// Handle the horizontal scroll bar
void CWindow::OnHScroll1( UINT nSBCode1,UINT nPos1, CScrollBar* pScrollBar1 )
{
      int pos1;

      pos1 = pScrollBar1->GetScrollPos();
      switch ( nSBCode1 )
      {
            case SB_LINEUP:
                  pos1 -= 1;
                  break;
            case SB_LINEDOWN:
                  pos1 += 1;
                  break;
            case SB_PAGEUP:
                     pos1 -= 10;
                  break;
            case SB_PAGEDOWN:
                  pos1 += 10;
                  break;
            case SB_TOP:
                  pos1 = MIN_RANGE;
                  break;
            case SB_BOTTOM:
                  pos1 = MAX_RANGE;
                  break;
            case SB_THUMBPOSITION:
                  pos1 = nPos1;
                  break;
            default:
                  return;
      }
      if ( pos1 < MIN_RANGE )
            pos1 = MIN_RANGE;
      else if ( pos1 > MAX_RANGE )
            pos1 = MAX_RANGE;
      sb2->SetScrollPos( pos1, TRUE );

}

// Handle the horizontal scroll bar
void CWindow::OnHScroll2( UINT nSBCode2,UINT nPos2, CScrollBar* pScrollBar2 )
{
      int pos2;

      pos2 = pScrollBar2->GetScrollPos();
      switch ( nSBCode2 )
      {
            case SB_LINEUP:
                  pos2 -= 1;
                  break;
            case SB_LINEDOWN:
                  pos2 += 1;
                  break;
            case SB_PAGEUP:
                     pos2 -= 10;
                  break;
            case SB_PAGEDOWN:
                  pos2 += 10;
                  break;
            case SB_TOP:
                  pos2 = MIN_RANGE;
                  break;
            case SB_BOTTOM:
                  pos2 = MAX_RANGE;
                  break;
            case SB_THUMBPOSITION:
                  pos2 = nPos2;
                  break;
            default:
                  return;
      }
      if ( pos2 < MIN_RANGE )
            pos2 = MIN_RANGE;
      else if ( pos2 > MAX_RANGE )
            pos2 = MAX_RANGE;
      sb3->SetScrollPos( pos2, TRUE );

}

// Handle the horizontal scroll bar
void CWindow::OnHScroll3( UINT nSBCode3,UINT nPos3, CScrollBar* pScrollBar3 )
{
      int pos3;

      pos3 = pScrollBar3->GetScrollPos();
      switch ( nSBCode3 )
      {
            case SB_LINEUP:
                  pos3 -= 1;
                  break;
            case SB_LINEDOWN:
                  pos3 += 1;
                  break;
            case SB_PAGEUP:
                     pos3 -= 10;
                  break;
            case SB_PAGEDOWN:
                  pos3 += 10;
                  break;
            case SB_TOP:
                  pos3 = MIN_RANGE;
                  break;
            case SB_BOTTOM:
                  pos3 = MAX_RANGE;
                  break;
            case SB_THUMBPOSITION:
                  pos3 = nPos3;
                  break;
            default:
                  return;
      }
      if ( pos3 < MIN_RANGE )
            pos3 = MIN_RANGE;
      else if ( pos3 > MAX_RANGE )
            pos3 = MAX_RANGE;
      sb4->SetScrollPos( pos3, TRUE );

}


// Init the application and the main window
BOOL CApp::InitInstance()
{
      m_pMainWnd = new CWindow();
      m_pMainWnd->ShowWindow(m_nCmdShow);
      m_pMainWnd->UpdateWindow();
      return TRUE;
}
randygAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

randygAuthor Commented:
Can someone please help me? My program is almost complete except for this one obstacle.
0
chensuCommented:
Remove the OnHScroll1, OnHScroll2 and OnHScroll3 functions. They won't be called at all. Modify the OnHScroll function as follows.

Change

sb1->SetScrollPos( pos, TRUE );

to

if (pScrollBar != NULL)
    pScrollBar->SetScrollPos( pos, TRUE );
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
nietodCommented:
Randy,
    Could you possibly give Chensu a bad grade.  I'm only a few points behind him and if you give him a good grade I'll never catch up!  

Just kidding,

todd
0
randygAuthor Commented:
Thank you very much for your response. I tried what you said and it works great!!

I have now added the following code to it so that it can update a Result Window. The problem is that when I move one of the Scrollbars it updates them all in a strange sequence.

How can I make them all update independently?

/ Handle the horizontal scroll bar
    void CWindow::OnHScroll( UINT nSBCode,UINT nPos, CScrollBar* pScrollBar )
    {
    int pos;

    pos = pScrollBar->GetScrollPos();
    switch ( nSBCode )
    {
    case SB_LINEUP:
    pos -= 1;
    break;
    case SB_LINEDOWN:
    pos += 1;
    break;
    case SB_PAGEUP:
    pos -= 10;
    break;
    case SB_PAGEDOWN:
    pos += 10;
    break;
    case SB_TOP:
    pos = MIN_RANGE;
    break;
    case SB_BOTTOM:
    pos = MAX_RANGE;
    break;
    case SB_THUMBPOSITION:
    pos = nPos;
    break;
    default:
    return;
    }
    if ( pos < MIN_RANGE )
    pos = MIN_RANGE;
    else if ( pos > MAX_RANGE )
    pos = MAX_RANGE;
   
    if (pScrollBar != NULL)
    pScrollBar->SetScrollPos( pos, TRUE );

    SetDlgItemInt(IDC_RESULT1,pos);

    }
0
chensuCommented:
You need to create four Result Windows. Each is for corresponding scroll bar. You can compare pScrollBar with sb? to determine which scroll bar is causing the message and update the corresponding Result Window.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.