Solved

SCROLLBAR Help Again

Posted on 1998-05-21
5
452 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

732 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