Some Views in MFC

This chapter mainly introduces the main view classes in MFC, which inherit from the Cview class.

 

The inheritance relationship is shown in the figure above.

 

scroll view

CscrollView adds basic scrolling functionality to Cview, it includes handlers for WM_VSCROLL and WM_HSCROLL messages, and leaves the work of designing scrolling windows to MFC.

First of all, let's introduce the basic concepts. Physical view refers to the view window and space that occupies the screen; logical view refers to the entire virtual space that can be seen using scroll bars.

 

The member function SetScrollSize accepts 4 parameters, 2 of which are optional.

void SetScrollSizes(

    int nMapMode, // specifies the mapping mode

    SIZE sizeTotal, // SIZE structure or Csize object specifying the logical size of the view

    const SIZE& sizePage = sizeDefault, //When the scroll axis is clicked, MFC scrolls the view

const SIZE& sizeLine = sizeDefault); // MFC scrolls the view when the scroll arrow is clicked

 

The following code sets the view to a size of 8.5 * 11 inches:

void CMyScrollView::OnInitialUpdate()

{

   CScrollView::OnInitialUpdate();

   SetScrollSizes(MM_LOENGLISH, Csize(850, 1100));

}

 

When OnDraw is called, the mapping mode is already set to the mode specified in the call to SetScrollSize, so there is no need to call SetMapMode in OnDraw. There are two principles you should keep in mind when using CscrollView:

If you draw the output outside the OnDraw function in the view, call the OnPrepareDC function to let MFC consider the effects of the mapping mode and scroll position in the output.

If hit testing is performed in response to mouse messages, use DptoLP to convert the coordinates of the click from device coordinates to logical coordinates, thus taking the effects of mapping mode and scroll position into account in the hit test.

 

         When a scroll event occurs, CscrollView uses the OnVScroll or OnHScroll message handler to capture and call ScrollWindow to scroll the view horizontally or vertically. The OnPaint function will then be called to paint part of the effective window created by the ScrollWindow. The OnPaint function will get the CpaintDC object, and then call the OnPrePareDC and OnDraw functions.

 

        CscrollView contains some member functions that can be used to manipulate the scroll view.

GetScrollPosition: Retrieves the current horizontal or vertical scroll position from the CscrollView.

ScrollToPosition: scroll to a given position

GetTotalSize: Measures the logical width and height of the view.

SetScaleToFitSize: The entire logical view can be scaled in the physical view.

 

CscrollView exerts more pressure on the view's OnDraw function than Cview, but the OnDraw call caused by scrolling events usually only requires redrawing a few lines of pixels, so there is no need to redraw the entire view, otherwise it may cause the scrolling operation to be very bad. . The key function to optimize the OnDraw function is the CDC function GetClipBox. It is called in the device description table object passed to OnDraw to obtain the logical size of the invalid rectangle that is the part of the view that needs to be redrawn.

HTML view

Use the Navigate function or its enhanced version Navigate2, the former can only be used for file system objects, the latter can be accessed anywhere in the command interpreter's namespace. Use the Navigate function to parse the Html file and display it in the application in the form of a web page.

tree view

CtreeView is quite simple, it derives most of the functions from the tree view control. In MFC, CtreeCtrl provides a program interface to the tree view control. A tree view is implemented programmatically by calling the CtreeCtrl function on the basis of the tree view control, and the CtreeView function GetTreeCtrl returns the reference of the control. For example, if you want to determine the number of items contained in the tree view, you should use it like this:

UINT count = GetTreeCtrl().getCount();

Obtaining the reference of the corresponding control by calling the view member function is a method commonly used by all MFC CctrlView derived classes.

 

Each item in a treeview control is composed of a text string (also called a label) and an optional graphic in a graphic list. In MFC, graphic lists are represented by instances of the class CImageList. A bitmap is stored in the instance, identified by an index number starting with 0.

The SetImageList function of CtreeCtrl assigns an instance of CimageList to the tree view. The InsertItem function of CtreeCtrl adds an item to the tree view control. Notifications of tree views usually come in the form of WM_NOTIFY messages, and in most cases, NM_TREEVIEW points to an NM_TREEVIEW structure. Update item text and graphics, for example, by passing the LPSTR_TEXTCALLBACK and I_IMAGECALLBACK parameters to InsertItem and handling the TVN_GETDISPINFO notification. Handle TVN_KEYDOWN notifications to customize the control's response to keyboard input.

list view

List view supports 4 presentation styles, large icon, small icon, list, report. ClistView derives most of its functionality from the list view control. To make a list view, you can call ClistView::GetListCtrl() to get the ClistCtrl reference of the control that appears inside the list view, and then call the ClistCtrl function to use the returned ClistCtrl reference.

When deriving a class from ClistView, always override PreCreateWindow in the derived class and give the view more than one default style.

Use it like ClistCtrl, you can refer to the online example.

 

When there is a lot of data, if it is added to the view at one time, the memory used by the view will be very large, and querying a piece of data will be very time-consuming. At this time, the technology of virtual list control in MFC control needs to be used. ( Of course, paging technology can also be used.) Specific Baidu.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324864302&siteId=291194637