Solved

Dialog Scrollbar

Posted on 1998-09-01
12
521 Views
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)
{
      //{{AFX_DATA_INIT(CScrollbDlg)
      m_xscroll = 0;
      m_yscroll = 0;
      m_zscroll = 0;
      m_rscroll = 0;
      //}}AFX_DATA_INIT
      // 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.

Thanks
Randy
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
  • 6
  • 5
12 Comments
 
LVL 8

Accepted Solution

by:
Answers2000 earned 200 total points
ID: 1171844
GENERAL

First off, I'd recommend binding to CScrollBar control variables to the controls, (member variables tab in class wizard)

1. use SetScrollRange to set the limits of each scroll bar.  OnInitDialog is a good place for this.

2. Add overrides for OnVScroll and OnHScroll, these are where you're told the scroll bar has moved.

You then calculate the new position in each case (it's upto you to decide what a "line" or "page" means) and then set the new position using SetScrollPos

3. If you want the controls on the dialog to move, again this is upto you, but you can do it using on each control

GetWindowRect - get the old pos
ScreenToClient - translate into client coords
MoveWindow - move the control to a new pos

BOOK & OTHER STUFF

You can find some example code in Programming Windows 95 with MFC by Jeff Prosise ISBN 1-55615-902-1.  

The class you should read up is CScrollBar (in this book or elsewhere)


0
 

Author Comment

by:randyg
ID: 1171845
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?
0
 
LVL 2

Expert Comment

by:jrmcg
ID: 1171846
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()
{
   CDialog::OnInitDialog();

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

   // IDM_ABOUTBOX must be in the system command range.
   ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
   ASSERT(IDM_ABOUTBOX < 0xF000);

   CMenu* pSysMenu = GetSystemMenu(FALSE);
   if (pSysMenu != NULL)
   {
      CString strAboutMenu;
      strAboutMenu.LoadString(IDS_ABOUTBOX);
      if (!strAboutMenu.IsEmpty())
      {
         pSysMenu->AppendMenu(MF_SEPARATOR);
         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;
   MaxRange=100;
   m_ScrollControl.SetScrollRange(MinRange, MaxRange, TRUE);

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


Hope this helps a little.

J.R.

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

Good Luck.


0
Independent Software Vendors: 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!

 

Author Comment

by:randyg
ID: 1171847
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;
                  break;
            case SB_LINEDOWN:
                  pos += 1;
                  break;
            case SB_PAGEUP:
                     pos -= 10;
                  break;
            case SB_PAGEDOWN:
                  pos += 10;
                  break;
            case SB_TOP:
                  pos = nMin;
                  break;
            case SB_BOTTOM:
                  pos = nMax;
                  break;
            case SB_THUMBPOSITION:
                  pos = nPos;
                  break;
            default:
                  return;
      }
      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?
0
 
LVL 2

Expert Comment

by:jrmcg
ID: 1171848
#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....

m_XAxisScrollbar.SetRange(iMin,iMax);
m_YAxisScrollbar.SetRange(iMin,iMax);
m_ZAxisScrollbar.SetRange(iMin,iMax);

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.....
J.R.

0
 

Author Comment

by:randyg
ID: 1171849
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?

Thanks
0
 
LVL 2

Expert Comment

by:jrmcg
ID: 1171850
Try this instead...

m_XAxisScrollbar.SetScrollPos(x_counter);

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

J.R.
0
 
LVL 2

Expert Comment

by:jrmcg
ID: 1171851
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...

Thanks,
J.R.
0
 

Author Comment

by:randyg
ID: 1171852
JR

That did it!!

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

Randy
0
 
LVL 2

Expert Comment

by:jrmcg
ID: 1171853
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....

J.R.
0
 

Author Comment

by:randyg
ID: 1171854
JR

That did it!!

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

Randy
0
 

Author Comment

by:randyg
ID: 1171855
JR

That did it!!

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

Randy
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
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 be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

729 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