We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now


Dialog Scrollbar

randyg asked
Medium Priority
Last Modified: 2011-08-18
I am writing a Dialog Based program in Visual C++ 5 using the MFCAppWizard(exe.)

I am trying to use a ScrollBar. In the Dialog Editor I select a ScrollBar from the Dialog Menu and place it in the Dialog. Using the ClassWizard I select EditCode and it takes me here:

CScrollbDlg::CScrollbDlg(CWnd* pParent /*=NULL*/)
      : CDialog(CScrollbDlg::IDD, pParent)
      m_xscroll = 0;
      m_yscroll = 0;
      m_zscroll = 0;
      m_rscroll = 0;
      // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
      m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);

The problem is I can't find any information or examples in any books on how to write the code to make the ScrollBars work. Can someone please write an example on how to use two independent ScrollBars.

Watch Question

Unlock this solution with a free trial preview.
(No credit card required)
Get Preview


I am a very beginner and I'm still not clear about your explanation. Would it be possible to write a sample of the code so that I can see it?

I think Answers2000 is off of the net for a couple weeks, should return soon, though.  I can give you a little direction though....

To set the member variable of the CScrollBar:
In the dialog editor right click on your scroll bar.
from the pop-up menu select CLASS WIZARD.

Click on the Member Variables tab in the wizard.

You will be provided a list of the controls by ID number.
Select the ID of the status bar you want to control.

Click   Add Variable
the variable name will automatically have the "m_" for you, all you have to do is fill in the name you want to use to control the scroll bar.

Make sure the Category is set to "Control".
The variable type will change for you to CScrollBar.

select OK and then OK again to get out of the class wizard.  If you don't select OK your changes will not be made.

You have now added a Member Variable to your dialog that will allow you to control the scroll bars.

If you search the on-line help for the following phrase:    "CScrollBar class members"
you will find the functions that are available to you with that control you have put on your dialog.

To use these functions within your dialog, just use the member variable with the function name as an example, Answers2000 suggested setting the scroll range inside the OnInitDialog function.  This would be like so....

m_ScrollControl will be the name of the control set in the wizard that I explained previously, however, you should use whatever name you gave it.

Inside YourProjectDlg.cpp
// CLimitTextDlg message handlers

BOOL CYourProjectDlg::OnInitDialog()

   // Add "About..." menu item to system menu.

   // IDM_ABOUTBOX must be in the system command range.

   CMenu* pSysMenu = GetSystemMenu(FALSE);
   if (pSysMenu != NULL)
      CString strAboutMenu;
      if (!strAboutMenu.IsEmpty())
         pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);

   // Set the icon for this dialog.  The framework does this automatically
   //  when the application's main window is not a dialog
   SetIcon(m_hIcon, TRUE);                  // Set big icon
   SetIcon(m_hIcon, FALSE);            // Set small icon
   // TODO: Add extra initialization here
  Here you would put your code to set the range
   int MinRange, MaxRange;
   MinRange = 0;
   m_ScrollControl.SetScrollRange(MinRange, MaxRange, TRUE);

   return TRUE;  // return TRUE  unless you set the focus to a control

Hope this helps a little.


P.S.  I'm not trying to get the points or anything, just trying to help.  Points should still go to Answers2000.

Good Luck.


JR, thanks for the help. I'm almost there but I have one more question.

I added this in the OnInitDialog. I took this out of "Inside Visual C++" by David Kruglinski.

CScrollBar* pSB = (CScrollBar*) GetDlgItem(IDC_XAXIS_SCROLLBAR);
      pSB->SetScrollRange(nMin, nMax);

      pSB = (CScrollBar*) GetDlgItem(IDC_YAXIS_SCROLLBAR);      
      pSB->SetScrollRange(nMin, nMax);      
      pSB = (CScrollBar*) GetDlgItem(IDC_ZAXIS_SCROLLBAR);      
      pSB->SetScrollRange(nMin, nMax);      

      pSB = (CScrollBar*) GetDlgItem(IDC_RAXIS_SCROLLBAR);      
      pSB->SetScrollRange(nMin, nMax);            

Then I wrote this:

void CCnc_lathe_50Dlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
      int pos;
      pos = pScrollBar->GetScrollPos();
      switch ( nSBCode )
            case SB_LINEUP:
                  pos -= 1;
            case SB_LINEDOWN:
                  pos += 1;
            case SB_PAGEUP:
                     pos -= 10;
            case SB_PAGEDOWN:
                  pos += 10;
            case SB_TOP:
                  pos = nMin;
            case SB_BOTTOM:
                  pos = nMax;
            case SB_THUMBPOSITION:
                  pos = nPos;
      if ( pos < nMin )
            pos = nMin;
      else if ( pos > nMax )
            pos = nMax;

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

Since I have 4 Scrollbars, when I click any one of them I want to execute code for that one only.

I tried this but I keep getting an error

     if (pScrollBar==m_XAxisScrollbar)   //m_XAxisScrollbar is the variable I selected.

What would be the correct statement to make this work?

#1 What error are you getting? ( I have a pretty good idea )
#2 Did you create the 'control' variables with the wizard as suggested?
     Reason I ask - you set the min/max ranges with pointers to the      GetDlgItem(IDC_CONTROL).  These min max levels can be set with the control      variables....


inside your OnInitDialog.

There is no need to get a pointer to the Dialog Item there.

To answer your question.  The variable that is created with the wizard is a CScrollBar.

It is declared in the Dialog Class header as:
CScrollBar m_XAxisScrollbar;
The value passed in the OnVScroll function is a pointer.

You are checking to see if a pointer is equal to a variable of type CScrollBar.

For a better example:

int iNumber = 5;
int* pNumber;
pNumber = &iNumber;

pNumber is equal to an address value (whatever that may be)
iNumber is equal to 5.

So the proper test would be....

if (pScrollBar==&m_XAxisScrollbar)
   // code

Let me know if you need any further help.....


I have one more question. I'd appreciate it if you could answer this one too.

I'm trying to move the ScrollBar in the code using this:

&m_XAxisScrollbar->SetScrollPos(x_counter);  //&m_XAxisScrollbar is my variable

and I get an error. What is the correct way to do this?


Try this instead...


This will be correct as long as you are accessing it from the Dialog class that it is a member of.

Let me know if this is what you needed...


If you feel I have helped you more than Answers2000 you can reject his answer and I will lock it.  I originally wasn't worried about the points, but he has had enough time to get back in on this, and I have answered a couple more questions.....

It is up to you, I just want to make sure you realize that you have that option...




That did it!!

Thanks so much. How can I give you 200 points too for your answers? I think your answers are worth it.


If you want to give me 200 you can post a 200 point 'dummy' question and put 'For jrmcg' in the title of the question, and I can lock it.  You also had the option of rejecting the previous expert's answer and then letting me lock it.  You do not have to give me any points though.  I really didn't mind answering the questions.  They were all along the same lines as your original question, and you have paid for the answers already.  Good luck with your project....




That did it!!

Thanks so much. How can I give you 200 points too for your answers? I think your answers are worth it.




That did it!!

Thanks so much. How can I give you 200 points too for your answers? I think your answers are worth it.

Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.