How to Use the MFC scrollbar Control

Source: Internet
Author: User

1. Drag the cscrollbar control in the dialog box and modify the ID,
2. Add the following code to the initdialog function of the dialog box to complete cscrollbar initialization.
Cscrollbar * pscrollbar = (cscrollbar *) getdlgitem (idc_scrollbar_horizontal );
Pscrollbar-> setscrollrange (0,100); // the position where the slider is moved is 0--100;
Pscrollbar = (cscrollbar *) getdlgitem (idc_scrollbar_vertical );
Pscrollbar-> setscrollrange (0,100 );
Pscrollbar = (cscrollbar *) getdlgitem (idc_scrollbar3 );
Pscrollbar-> setscrollrange (0,100 );
3. Add two message processing functions for the dialog box wm_hscroll and wm_vscroll, as shown below:
Void ctestscrollbardlg: onhscroll (uint nsbcode, uint NPOs, cscrollbar * pscrollbar)
{
// Todo: add the message processing program code and/or call the default value here
Int temppos = pscrollbar-> getscrollpos ();
Switch (nsbcode)
{
Case sb_thumbposition: // drag the slider
Pscrollbar-> setscrollpos (NPOs );
Break;
Case sb_lineleft: // click the arrow on the left
If (temppos> 1)
{
Temppos --;
}
Pscrollbar-> setscrollpos (temppos );
Break;
Case sb_lineright: // click the arrow on the right
If (temppos <1, 100)
{
Temppos ++;
}
Pscrollbar-> setscrollpos (temppos );
Break;
}
Cdialog: onhscroll (nsbcode, NPOs, pscrollbar );
}

 

 

A scroll bar is divided into a horizontal scroll bar and a vertical scroll bar. A scroll bar is usually used in combination with other controls or views by setting a value range. The method for loading a scroll bar in the dialog box is as follows:
1. Add a variable in the dialog box (use the Class Wizard to connect the control and the variable): cscrollbar m_scroll;
2. Initialize the scroll bar: Use the cscrollbar: setscrollrange (INT Nmin, int Nmax, bool bredraw = true) function to initialize the scroll bar, that is, to set the maximum and minimum values of the scroll bar.
3. Process the scroll bar message in the parent window: the horizontal scroll bar responds to the onhscroll function, and the vertical scroll bar responds to the onvscroll function. Generally, the following steps must be taken in the function:
1. Obtain the scroll bar range: cscrollbar: getscrollrange (Nmin, Nmax );
2. Obtain the position of the current scroll bar. The value is int ncurpos = pscrollbar-> getscrollpos ();
3. Process rolling information. Press left and right, and the mouse key.
4. Determine whether the current value is greater than or less than the maximum or minimum value after rolling.
5. Updates the scroll bar information.
6. Set the value of other controls based on the current value of the scroll bar (this is the purpose of using the scroll bar ).
A detailed function is provided below:
Void cmydlg: onhscroll (uint nsbcode, uint NPOs, cscrollbar * pscrollbar)
{
// Todo: add your message handler code here and/or call default
Int Nmin, Nmax, ncurpos;
Pscrollbar-> getscrollrange (& Nmin, & Nmax); // get the scroll bar range
Ncurpos = pscrollbar-> getscrollpos (); // get the current value
Switch (nsbcode) // process rolling Information
{
Case sb_lineleft: // The left arrow is pressed
Ncurpos = nCurpos-5; break;
Case sb_lineright:
Ncurpos ++; break; // The right arrow is pressed
Case sb_thumbtrack: // drag the mouse
Ncurpos = NPOs; break;
}
If (ncurpos if (ncurpos> Nmax) ncurpos = Nmax;
Pscrollbar-> setscrollpos (ncurpos); // update information
// Process what you want to handle
Cdialog: onhscroll (nsbcode, NPOs, pscrollbar );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.