Solved

SCROLLBAR Help Again

Posted on 1998-05-21
5
451 Views
Last Modified: 2008-02-01
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;
}
0
Comment
Question by:randyg
  • 2
  • 2
5 Comments
 

Author Comment

by:randyg
ID: 1164474
Can someone please help me? My program is almost complete except for this one obstacle.
0
 
LVL 23

Accepted Solution

by:
chensu earned 300 total points
ID: 1164475
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
 
LVL 22

Expert Comment

by:nietod
ID: 1164476
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
 

Author Comment

by:randyg
ID: 1164477
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
 
LVL 23

Expert Comment

by:chensu
ID: 1164478
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question