Use of the scroll subview

Source: Internet
Author: User

I haven't written a blog for several days. Today, I found a requirement. There are many drop-down box options in the dialog box, and the scroll bar is needed for a screen to be displayed. At first, I did not know the CScrollView class, so I tried to use the scroll bar and then changed the coordinates of the control on the screen during scrolling. But later I found that not only do I often scroll around, but how do I display some hidden drop-down boxes? After some tangle, I began to look for more concise solutions.

I have heard of the nested subview statement before. As a result, Ling Guang naturally finds CScrollView when he thinks of creating a subview with a scroll bar.

The function of this view class is very simple, that is, to create a view area, you can also add controls to it, and the excess part will be "invisible". You can view more areas through the scroll bar.

No picture, no truth, so a small program will know what is going on.

Create a dialog box-based MFC program ScrollViewTest. Right-click the project, add a new MFC class CMyScrollView, and select CScrollView when selecting the base class.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1A2102058-0.png "border =" 0 "alt =" "/>

Then add a member variable in ScrollViewTest. h.

 
 
  1. CMyScrollView * m_pMyView; 

Next, add the following code in CScrollViewTestDlg: OnInitDialog.

 
 
  1. m_pMyView = (CMyScrollView*)(RUNTIME_CLASS(CMyScrollView)->CreateObject()); 
  2. CCreateContext context; 
  3. context.m_pCurrentDoc = NULL; 
  4.  
  5. m_pMyView->Create(NULL, 
  6.     NULL,  
  7.     WS_BORDER, 
  8.     CRect(0, 0, 100, 100), 
  9.     this,  
  10.     WM_USER + 1,  
  11.     &context); 
  12.  
  13. ((CView*)m_pMyView)->OnInitialUpdate(); 
  14. m_pMyView->ShowWindow(SW_SHOW); 

All right, you can run it. The result is as follows.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1A2106148-1.png "border =" 0 "alt =" "/>

You can see that this is embedded in the dialog box like a control.

Then you can do whatever you want. Modify the code.

 
 
  1. m_pMyView->Create(NULL,  
  2.     NULL,   
  3.     WS_BORDER,  
  4.     CRect(0, 0, 200, 200),  
  5.     this,   
  6.     WM_USER + 1,   
  7.     &context);  

Run it again, and you will find, what about the scroll bars?

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1A2106416-2.png "border =" 0 "alt =" "/>

Note that after Create, call the virtual function OnInitialUpdate () of CScrollView to set some basic parameters, one of which is "rolling area ". Obviously, the "rolling area" after running this method is smaller than CRect (0, 0,200,200), so the subview considers "I Don't Need To scroll anymore, you can see everything ". To verify this, Debug and check it.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1A210NB-3.png "border =" 0 "alt =" "/>

M_totalDev is the "rolling area", m_pageDev is the size to be rolled for "rolling one page", and m_lineDev is the size to be rolled for "rolling one row.

It is easy to solve this problem. Increase the "rolling area ". Use the SetScrollSize () method.

 
 
  1. void SetScrollSizes(int nMapMode, 
  2.             SIZE sizeTotal, 
  3.             const SIZE& sizePage = sizeDefault, 
  4.             const SIZE& sizeLine = sizeDefault); 

The first parameter is "rolling area". We will set it to 500 in length and width.

The second parameter is the area to be rolled when a page is rolled.

The third parameter is the area to be rolled when a row is rolled.

Okay. Set "scroll area ".

 
 
  1. SIZE size; 
  2. size.cx = 500; 
  3. size.cy = 500; 
  4. m_pMyView->SetScrollSizes(MM_TEXT, size); 

Note: These lines of code must be added after OnInitialUpdate (). Otherwise, you will not be able to get rid of them? 650) this. width = 650; "src ="/neweditor/editor/images/smiley/2.gif" alt = ""/>

The result is as follows.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1A210Bb-4.png "border =" 0 "alt =" "/>

Okay. Next, add a control to the subview. Add a member variable:

 
 
  1. CMFCButton * m_button; 

Initialize it in the constructor:

 
 
  1. m_button = new CMFCButton; 

Add the following code to CScrollViewTestDlg: OnInitDialog:

 
 
  1. button->Create(_T("Hello"), 
  2. WS_VISIBLE, 
  3. CRect(5, 5, 80, 250), 
  4. m_pMyView, 
  5. WM_USER + 1); 

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1A210C37-5.png "border =" 0 "alt =" "/>

Well, you can only drag the scroll bar to see the full picture of this button.

The basic functions are introduced, and some other methods, such

 
 
  1. void CheckScrollBars(BOOL& bHasHorzBar, BOOL& bHasVertBar) const; 

Check whether the scroll bar exists.

For example

 
 
  1. CPoint GetDeviceScrollPosition( ) const; 

Returns the position of the current scroll bar, a coordinate value.

For example

 
 
  1. void SetScaleToFitSize(SIZE sizeTotal); 

You can adjust the size of a view based on its content.

Such methods can be flexibly applied to programs.

 

 

 

This article from the "front of the tradeford reverse tairuibao blog, please be sure to keep this source http://serious.blog.51cto.com/242085/876251

Related Article

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.