Link to home
Start Free TrialLog in
Avatar of randyg
randyg

asked on

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;
}
Avatar of randyg
randyg

ASKER

Can someone please help me? My program is almost complete except for this one obstacle.
ASKER CERTIFIED SOLUTION
Avatar of chensu
chensu
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of randyg

ASKER

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);

    }
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.