VS2010-MFC (Common controls: Scroll bar control scroll bar)

Source: Internet
Author: User
Tags assert function prototype

Transferred from: http://www.jizhuomi.com/software/191.html

Introduction to scroll bar controls

The scroll bar is familiar to everyone, and there are many scroll bars in the Windows window. After the list box and combo box are set with the corresponding properties, the scroll bar appears if the list item does not appear. The scrollbar is divided into two horizontal scrollbars (horizontal Scroll bar) and vertical scroll bars (Vertical Scroll bar). The scroll bar has a scroll block that identifies where the scroll bar is currently scrolled. We can drag the scroll bar, or you can use the mouse to click on the scroll bar to move the thumb in a position.

There are two kinds of standard scroll bar and scroll bar control, which can be divided into the form of scroll bar creation. The scroll bars that appear after the Ws_hscroll or Ws_vscroll style are set up like list boxes and combo boxes, not as separate windows, but as part of these windows, which is the standard scroll bar . The ScrollBar control is a separate window that gets the focus and responds to certain actions.

creation of scroll bar controls

MFC also provides a class for the operation of the ScrollBar control, which is the CScrollBar class .

There are still two ways to create a scrollbar control, one to drag a ScrollBar control into a dialog template directly in Toolbox, then to add a control variable to use, and to create it dynamically with the Create member function of the CScrollBar class. Both of these methods are suitable for different occasions.

The function prototype for the CScrollBar class's member function Create is as follows:

Virtual BOOL Create (
DWORD Dwstyle,
Const rect& RECT,
cwnd* pParentWnd,
UINT NID
);

This function is basically the same as the CREATE function prototype for other control classes. The dwstyle parameter specifies the style of the ScrollBar control, rect specifies the position and dimensions of the scrollbar control, pParentWnd the pointer to the parent window of the scrollbar control, and NID specifies the ID of the scrollbar control. The following is a brief introduction to some of the main scroll bar control styles, more specific to MSDN.

       Sbs_horz: Specifies that the scrollbar is a horizontal scroll bar. If you do not specify a sbs_bottomalign or sbs_topalign style, the height, width, and position of the scrollbar are given by the RECT parameter of the CREATE function.
       sbs_vert: Specifies that the scrollbar is a vertical scroll bar. If you do not specify a sbs_rightalign or sbs_leftalign style, the height, width, and position of the scrollbar are given by the RECT parameter of the CREATE function.
       sbs_topalign: Used in conjunction with Sbs_horz. The top edge of the scroll bar is aligned with the RECT parameter of the CREATE function to specify the top edge of the rectangle. The scrollbar height is the default height of the system scroll bar.
       sbs_bottomalign: With Sbs_ Horz in conjunction with. The bottom edge of the scrollbar is aligned with the RECT parameter of the CREATE function, which specifies the bottom edge of the rectangle. The scrollbar height is the default height of the system scroll bar.
       sbs_leftalign: Used in conjunction with Sbs_vert. The left edge of the scroll bar is aligned with the RECT parameter of the CREATE function that specifies the left edge of the rectangle. The scroll bar width is the default width of the system scroll bar.
       sbs_rightalign: Used in conjunction with Sbs_vert. The right edge of the scroll bar aligns with the rect parameter of the CREATE function to specify the right edge of the rectangle. The scroll bar width is the default width of the system scroll bar.

Dwstyle parameters can be a combination of some of the above styles, in addition to the general will also use Ws_child, ws_visible style. For example, to create a horizontal scroll bar control, the dwstyle parameter should be ws_child| ws_visible| Sbs_horz, the dwstyle parameter should be ws_child| when you create a vertical scroll bar control ws_visible| Sbs_vert.

main member functions of the CScrollBar class:

BOOL getscrollinfo (lpscrollinfo lpscrollinfo, UINT nmask = sif_all);

Gets the parameter information for the scroll bar, which is the form of the SCROLLINFO structure. The parameter lpscrollinfo is a pointer to the SCROLLINFO struct variable. The SCROLLINFO structure is defined as follows:

C + + code
typedefstructTagscrollinfo {UINT cbsize; //Dimensions of the structure (in bytes)UINT fmask;//describes which parameters in the structure are valid and can be a combination of masked values, such as sif_pos| Sif_page, if it's sif_all, the whole structure works.    intNMin;//Scroll Range minimum value, valid when Sif_range is included in Fmask    intNMax;//Scroll range Maximum, valid when Sif_range is included in FmaskUINT Npage;//page size to determine the size of the proportional scroll box, which is valid when the fmask contains Sif_page    intNPos;//the position of the scroll box when the fmask contains sif_pos valid    intNtrackpos;//Scroll The position of the scroll box when fmask contains Sif_trackpos, the parameter can only query, cannot be set, it is best not to use this parameter to query the position of scroll box when dragging} SCROLLINFO, *Lpscrollinfo; typedef SCROLLINFO CONST*lpcscrollinfo; 

The parameter nmask has the same meaning as the fmask in the scrollinfo structure. The function returns True if the information is obtained successfully, otherwise false.

bool Setscrollinfo (lpscrollinfo lpscrollinfo, bool bredraw = TRUE);

Used to set various parameter information for the scroll bar. The parameter lpscrollinfo is a pointer to the SCROLLINFO struct variable, and the parameter bredraw indicates whether the scrollbar needs to be redrawn and, if true, redraws. Returns true if the function operation succeeds, otherwise false.

int getscrollpos() const;

Gets the current position of the scroll block. Returns 0 if it fails.

int setscrollpos(int nPos, BOOL bredraw = TRUE);

Moves the scroll block to the specified position. The parameter NPOs specifies the new position of the thumb, and the parameter Bredraw indicates whether the scroll bar needs to be redrawn and, if true, redraws. The function returns the original position of the scroll box and returns 0 if the operation fails.

void Getscrollrange (Lpint lpminpos, lpint lpmaxpos) const;

Gets the scroll range of the scroll bar. The parameter Lpminpos points to the minimum value of the scroll bar scroll range, and the parameter lpmaxpos the maximum value of the scroll bar scroll range.

void setscrollrange(int nminpos, int nmaxpos, BOOL bredraw = TRUE);

Used to specify the scroll range of the scroll bar. The parameters Nminpos and Nmaxpos Specify the minimum and maximum values for the scroll range, respectively, and the difference must not exceed 32767. When both are 0 o'clock, the scroll bar is hidden. The parameter Bredraw indicates whether the scroll bar needs to be redrawn and, if true, redraws.

OnHScroll () and OnVScroll () functions

Whether it is a standard scroll bar or a scrollbar control, the notification messages for the scroll bar are sent out with Wm_hscroll and wm_vscroll messages. The default handler for these two messages is CWnd::OnHScroll and CWnd::OnVScroll, which is generally required to overload the two functions in a derived class to implement scrolling functionality. That is, assuming that a horizontal scrollbar is placed in a dialog box, we can overload the OnHScroll function in the dialog class and implement the scrolling function in the OnHScroll function.

The declarations of these two functions are as follows:

afx_msg void OnHScroll (UINT nsbcode,uint npos,cscrollbar* pscrollbar);
afx_msg void OnVScroll (UINT nsbcode,uint npos,cscrollbar* pscrollbar);

The parameter nsbcode is the notification message code, the main notification code and the meaning of the introduction below is listed. NPos is the position of the scroll box, which makes sense only if Nsbcode is Sb_thumbposition or sb_thumbtrack. If the notification message is from a scrollbar control, then Pscrollbar is a pointer to the control and Pscrollbar is NULL if the standard scroll bar is sent.

       sb_bottom/sb_right: Scroll to End (right)
Span style= "font-size:15px" >       sb_top/sb_left: Scroll to Top (left side)
       Sb_linedown/sb_lineright: Scrolls Down (right) one row (column)
       Sb_lineup/sb_lineleft: Scrolls Up (left) by one row (column)
       Sb_pagedown/sb_pageright: Scrolls Down (right) one page
       sb_pageup/sb_pageleft: Scroll up (left) one page
       sb_thumbposition: Scroll to the specified position
       sb_thumbtrack: scroll box is dragged. You can use this message to track the drag of the scroll box
       sb_endscroll: Scroll End

CScrollBar Class Application Example

Give us a simple example after we have finished the basic knowledge. The simple example is to include a horizontal scrollbar control and an edit box control in a dialog box, regardless of whether the scrollbar control is scrolling or still, and the current position of the thumb is displayed in the edit box. The following are the specific development steps:

1. Create a dialog-based MFC project with the name set to "Example26".

2. In the automatically generated dialog template Idd_example26_dialog, delete "Todo:place dialog controls here." The static text control, the OK button, and the Cancel button. Add a horizontal Scroll bar control with the ID set to Idc_hori_scrollbar. Add a static text control and an edit box, and the Caption property of the static text control is set to "thumb current position:" And the edit box has the ID set to Idc_hscroll_edit,read only property set to True. The dialog template at this point is as follows:


3. Add the CScrollBar-type control variable M_horiscrollbar for the scroll bar Idc_hori_scrollbar.

4. When the dialog is initialized, we need to set the scroll range and initial position of the scroll bar, and display the initial position in the edit box, then we need to modify the Cexample26dlg::oninitdialog () function to:

C + + code
BOOL Cexample26dlg::oninitdialog () {cdialogex::oninitdialog (); //Add "About ..." menu item to System menu. //Idm_aboutbox must is in the System command range. ASSERT ((Idm_aboutbox &0xfff0) ==Idm_aboutbox); ASSERT (Idm_aboutbox<0xf000); CMenu* Psysmenu =GetSystemMenu (FALSE); if(Psysmenu! =NULL)           {BOOL bnamevalid;           CString Straboutmenu; Bnamevalid=straboutmenu.loadstring (Ids_aboutbox);           ASSERT (Bnamevalid); 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 was not a dialogSetIcon (M_hicon, TRUE);//Set Big iconSetIcon (M_hicon, FALSE);//Set Small Icon//Todo:add Extra Initialization here//sets the scroll range of the horizontal scroll bar to 1 toM_horiscrollbar.setscrollrange (1, -); //set the initial position of the horizontal scroll bar toM_horiscrollbar.setscrollpos ( -); //in the edit box, display theSetdlgitemint (Idc_hscroll_edit, -); returnTRUE;//return TRUE Unless you set the focus to a control} 

5. Now the scroll bar does not scroll properly, and the number in the edit box does not change with the scroll. According to the above, we can overload the OnHScroll function of the Cexample26dlg class. To do this, click the "Messages" button on the toolbar of the Cexample26dlg class's property page to find the Wm_hscroll message and add the response function.

The OnHScroll function is rewritten as follows:

voidCexample26dlg::onhscroll (UINT Nsbcode, uint nPos, cscrollbar*Pscrollbar) {      //todo:add your message handler code here and/or call default    intpos = M_horiscrollbar.getscrollpos ();//get the current position of the horizontal scroll bar      Switch(nsbcode) {//If you scroll one column to the left, POS minus 1         CaseSb_lineleft:pos-=1;  Break; //If you scroll one column to the right, Pos adds 1         CaseSb_lineright:pos+=1;  Break; //If you scroll one page to the left, Pos minus ten         CaseSb_pageleft:pos-=Ten;  Break; //If you scroll one page to the right, the POS adds ten         CaseSb_pageright:pos+=Ten;  Break; //If you scroll to the leftmost end, POS is 1         CaseSb_left:pos=1;  Break; //If you scroll to the far right end, the POS is         CaseSb_right:pos= -;  Break; //If you drag the thumb to the specified position, the POS is assigned a value of NPOs         CaseSb_thumbposition:pos=NPos;  Break; //the following m_horiscrollbar.setscrollpos (POS), which is executed a second time into this function, eventually determines the scroll block position, and will go directly to the default branch, so the setting in the edit box shows the value        default: Setdlgitemint (Idc_hscroll_edit, POS); return; }         //Set the scroll block positionM_horiscrollbar.setscrollpos (POS);  Cdialogex::onhscroll (Nsbcode, NPos, Pscrollbar); }

6. Compile and run the program, pop Up the results dialog box, you can drag the thumb to see if you can scroll normally, and the correct value is displayed in the edit box. The effect is as follows:

As for the vertical scroll bar , in fact, similar to the horizontal scroll bar, you can write your own vertical scroll bar example. the contents of the scrollbar control are described here, which are the basics, but these are the necessary things to apply to the scroll bar control later.

VS2010-MFC (Common controls: Scroll bar control scroll bar)

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.