[MFC] dialog box supports scroll bar function

Posted by jamey on Sun, 19 Dec 2021 05:45:35 +0100

01. Demand analysis

MFC supports scroll bar and docking. It has been added on the basis of some controls before, such as RichEdit2, Edit Control, etc.
Recently, I want to make a general template to facilitate future expansion and iteration on the existing basis, and learn from some functional modules of some software. Then there is a problem to be solved. If the dialog box is large enough and the page display is incomplete, the scroll bar of the dialog box must be supported, and the scroll bar of the dialog box is different from that of some controls.

This paper introduces a method, which is implemented through the message function in the dialog box base class.

02. Specific implementation

The message function here can support vertical scroll bars. It can be written according to your needs.

Take the vertical scroll bar as an example:

  1. Add vertical slider: right click the dialog box - > properties to change the Vertical Scrollbar status to True.

  2. Add response function: right click dialog box - > Add Class Wizard

  3. Add virtual function: OnInitDialog initializes scroll bar information

    The addition of OnInitDialog is similar to the figure above, but you can find it in the virtual function and add it to the project.

    BOOL XXX::OnInitDialog()
    {
    	CDialogEx::OnInitDialog();
    	//Add additional initialization code here
    	SCROLLINFO scrollinfo;
    	GetScrollInfo(SB_VERT,&scrollinfo,SIF_ALL);
    	scrollinfo.nPage=10; //Set slider size
    	scrollinfo.nMax=100; //Sets the maximum position of the scroll bar 0 – 100
    	SetScrollInfo(SB_VERT,&scrollinfo,SIF_ALL);
    	return TRUE;
    }
    
  4. Add in the onvscroll (uint nsbcode, uint NPOs, cscrollbar * pscollbar) message processing function

    void CChangeonDlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)   //Scrolling effect of dialog box
    {
        // TODO: add message handler code here and / or call default values
    
        SCROLLINFO scrollinfo;
        GetScrollInfo(SB_VERT,&scrollinfo,SIF_ALL);
        int unit=3;        
         switch (nSBCode)  
         {      
         case SB_LINEUP:          //Scroll one line up
             scrollinfo.nPos -= 1;  
             if (scrollinfo.nPos<scrollinfo.nMin)
             {  
                 scrollinfo.nPos = scrollinfo.nMin;  
                 break;  
             }  
             SetScrollInfo(SB_VERT,&scrollinfo,SIF_ALL);  
             ScrollWindow(0,unit); 
             break;  
         case SB_LINEDOWN:           //Scroll one line down
             scrollinfo.nPos += 1;  
             if (scrollinfo.nPos+scrollinfo.nPage>scrollinfo.nMax)  //Be sure to add the length of the slider here before making judgment
             {  
                 scrollinfo.nPos = scrollinfo.nMax;  
                 break;  
             }  
             SetScrollInfo(SB_VERT,&scrollinfo,SIF_ALL);  
             ScrollWindow(0,-unit);  
             break;  
         case SB_PAGEUP:            //Scroll one page up.
             scrollinfo.nPos -= 5;  
             if (scrollinfo.nPos<=scrollinfo.nMin)
             {  
                 scrollinfo.nPos = scrollinfo.nMin;  
                 break;  
             }  
             SetScrollInfo(SB_VERT,&scrollinfo,SIF_ALL);  
             ScrollWindow(0,unit*5);  
             break;  
         case SB_PAGEDOWN:        //Scroll one page down        
             scrollinfo.nPos += 5;  
             if (scrollinfo.nPos+scrollinfo.nPage>=scrollinfo.nMax)  //Be sure to add the length of the slider here before making judgment
             {  
                 scrollinfo.nPos = scrollinfo.nMax;  
                 break;  
             }  
             SetScrollInfo(SB_VERT,&scrollinfo,SIF_ALL);  
             ScrollWindow(0,-unit*5);  
             break;  
         case SB_ENDSCROLL:      //End scroll     
             break;  
         case SB_THUMBPOSITION:  //Scroll to the absolute position. The current position is provided in nPos
             break;  
         case SB_THUMBTRACK:                  //Drag scroll box to specified position. The current position is provided in nPos
             ScrollWindow(0,(scrollinfo.nPos-nPos)*unit);  
             scrollinfo.nPos = nPos;  
             SetScrollInfo(SB_VERT,&scrollinfo,SIF_ALL);
             break;  
         }
    
        CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
    }
    

03. Summary

After completing the above steps, the dialog box also supports the function of vertical scroll bar. The effect picture is not pasted here. You can imitate the above steps.

The technology in the article is written by consulting materials. The above code is the blogger's personal test code, which can realize the scroll bar. If you want the horizontal scroll bar, you only need to add step 2 above to on_ WM_ Change vscroll to add on_ WM_ Hscrol is enough. The contents are quite different.

Here is a general introduction to the function of adding scroll bars to the dialog box. Briefly record the implementation of the function of adding scroll bars to the MFC dialog box.
If there are mistakes, you can comment and have a look. Thank you!

Topics: C++ MFC