Multiple implementation methods for tab view in Visual C + + _c language

Source: Internet
Author: User

This article describes the Visual C + + TAB view of a variety of implementation methods, shared for everyone to reference. Specifically as follows:

First, the introduction

Label controls (tab control) are one of the most frequently used controls in VC + + programming, allowing you to set up multiple pages in a single dialog box or window, each representing a set of controls. When a page's label is selected, the controls on that page are displayed. The label controls allow for more information to be displayed in a limited window space and are sorted clearly. At the same time, VC + + provides a document/view (DOCUMENT/VIEW) structure to develop the application of the simple way to save data in the document, display data in the view. Whether in an SDI or MDI program, each document can correspond to one or more views, but often when you display data, you need to display the same set of data in a different view, such as a list view, a tree view, or one view that displays part of the data and another part of the data in another view. and want to be able to display in the same display area, switch when needed. A tab view is formed when the label control is applied to a multiple-view switch.
Through the above analysis, we are not difficult to find that the implementation of a tab view mainly to complete the following two areas of work:

① implements a label window. It should have the ability to draw windows, respond to user selection, judge and handle user selection. In this paper, the tab View implementation of the classification is based on the different label window division.

② a switch between multiple views. According to the label window to determine the results of the user selection, from multiple views to select one, displayed in the display area.

This paper analyzes and summarizes the implementation methods of three kinds of tab view:

1 use CTabCtrl Control to realize tab View.
2) Using Csheetctrl tab to select a window to implement tab View.
3) using the static partition window to realize tab View.

Second, CTabCtrl control to implement tab View

CTabCtrl is the standard control class defined in the MFC class library, through the processing of message tcn-selchange and the use of function GetCurSel (), SetCurSel (), and so on, can complete the response, judge and set tag control work well, Therefore, it is easier to use the CTabCtrl control to implement tab view.

1. Main classes of implementations

Copy Code code as follows:
To toggle the displayed view window class
Class Cmyview1:public CListView
Class Cmyview2:public CView
A derived Label control class
Class Cviewtabctrl:public CTabCtrl
The main window that defines the tab view
Class Ctabctrlview:public CWnd
{
Protected
Cviewtabctrl M_tabctl;
...
}
Class Cmainframe:public CFrameWnd
{
Ctabctrlview M_tabview;
...
}

2. Parent-child relationships and location relationships between windows
The following code determines the position relationship between the windows.
Copy Code code as follows:
void Ctabctrlview::recalclayout ()
{ ...
CRect rect;
Client area size of the Ctabctrlview window
GetClientRect (&rect);
M_tabctl.recalclayout (rect, pwnd);
...
}
void Cviewtabctrl::recalclayout (CRect & Rect, CWnd * wnd)
{
The label control occupies the client area of the Ctabctrlview window
SetWindowPos (NULL, Rect.left, Rect.top, Rect.right-rect.left,
Rect.bottom-rect.top, Swp_nozorder);
Adjusts the position of the viewport window to be displayed so that it occupies the display area of the label control
Adjustrect (FALSE, &rect);
Wnd->setwindowpos (NULL, Rect.left, Rect.top, Rect.right-rect.left,
Rect.bottom-rect.top, Swp_nozorder);
}

3. Implementation of switching between multiple vision
Depending on the switch mainly completes the following work:
Copy Code code as follows:
Step 1: Set the ID of the currently active child window
cview* Poldactiveview = GetActiveView ();
:: SetWindowLong (Poldactiveview->m_hwnd, gwl_id, m_ncurrentexample);
Step 2: Generate a new Run-time class that corresponds to the selected viewport window
Cruntimeclass* Pnewviewclass;
Switch (NVIEWID)//Nviewid Identification of individual views
{
Case ID_MYVIEW1:
Pnewviewclass = Runtime_class (CMyView1);
Break
Case ID_MYVIEW2:
Pnewviewclass = Runtime_class (CMYVIEW2);
Break
Default
ASSERT (0);
Return
}
Step 3: Prepare the relevant context for the new view class, creating a new viewport
CCreateContext context;
Context.m_pnewviewclass = Pnewviewclass;
Context.m_pcurrentdoc = GetActiveDocument ();
cview* Pnewview = M_tabview.createview (Pnewviewclass, CSize (100,100), &context);
if (Pnewview!= NULL)
{
Step 4: Show the new view
Pnewview->showwindow (Sw_show);
SetActiveView (Pnewview);
...
Step 5: Close the old view
Poldactiveview->destroywindow ();
}

During the view switching process, you will need to regenerate the new view and close the old view. In some cases, however, you want multiple view windows to exist at the same time, displaying only one at a time, and hiding other windows. You can save the window information for each new view you join by defining the following data structure and variables.
Copy Code code as follows:
typedef struct
{
CWnd *pwnd; Window pointer
Char szlabel[32]; Label window on the string that should view
}tcb_item;
Save the View information that you have entered into the list
CList <tcb_item *,tcb_item *> m_views;

When switching, set the parameter Swp_showwindow or Swp_hidewindow in the function SetWindowPos () to display the selected new view, or hide the old view. This approach makes the view operation more flexible, making it easy to add and remove different views in the tab view.

Three, Csheetctrl tab Selection window implementation tab View

Csheetctrl is not a class in the MFC class library, and the method used to implement the tab view is essentially the same as the previous method. The "A tab-like Sheet Ctrl" program contains the source code for the VC programming source code set in the programmer's base camp site, but changes the window switch to view switch when used.

1. The creation of a label window

Csheetctrl implements the Attach () function, which is its interface with the external. Csheetswnd m_sheet is defined in Ctabsheetctrl, and during initialization, Csheetctrl is created as a child window of the Ctabsheetctrl window by calling M_sheet.attach (this).

Copy Code code as follows:
BOOL Csheetswnd::attach (CWnd * pwndparent, COLORREF rgbbackground)
{
Determines the size and position of the label window
CRect rect = GetRect (pwndparent);
...
BOOL bresult = Create (..., rect, ...);
...
return bresult;
}
CRect Csheetswnd::getrect (cwnd* pwndparent)
{
CRect rect;
Pwndparent->getclientrect (rect);
Rect.top = Rect.bottom-getsystemmetrics (SM_CYVTHUMB);
return rect;
}

2. Find the selected view window

Csheetctrl implements a function similar to CTabCtrl, such as drawing a label window, judging the user's choice, and so on. It does not itself save any information about the view, and when the view is switched, it finds the user's selected view window through its parent window.

Copy Code code as follows:
Get the first View child window pointer function of the parent window
cwnd* Csheetswnd::getfirstview ()
{
M_pviewfind = GetParent ()->getwindow (gw_child);
The M_hwnd store is the Csheetctrl label window.
Exclude Csheetctrl Tab window
while (m_pviewfind && m_pviewfind->m_hwnd = = m_hwnd)
M_pviewfind = M_pviewfind->getwindow (Gw_hwndnext);
return m_pviewfind;
}
Get the next view window pointer function
cwnd* Csheetswnd::getnextview ()
{
if (M_pviewfind)
{
M_pviewfind = M_pviewfind->getwindow (Gw_hwndnext);
while (m_pviewfind && m_pviewfind->m_hwnd = = m_hwnd)
M_pviewfind = M_pviewfind->getwindow (Gw_hwndnext);
return m_pviewfind;
}
return NULL;
}

After the child window (CMyView1, CMYVIEW2) pointers for all parent windows (Ctabsheetctrl) are obtained in a certain order, the selected view window is determined according to the space position of each child window's caption.
Copy Code code as follows:
Get first view window pointer
cwnd* pchild = Getfirstview ();
while (pchild)
{
CRect rect (cx,0,0,0);
Rect returns the size of the rectangle needed to draw the caption of the window
Pdc->drawtext (Getviewtitle (pchild), rect, Defaultformatdrawtext | Dt_calcrect);
rect.top = 0;
Rect.bottom = M_drawrect.height ();
if (Apointx > CX-LRB) && (apointx <= cx + rect. Width () + LRB))//Apointx The horizontal axis of the mouse position
break;//found the selected view
Get a pointer to the next view window
pchild = GetNextView ();
int next = rect. Width () + 6 + LRB;
CX + + next;
}

Four, the static partition window realizes the tab View

Using the static partition window CSplitterWnd to Realize tab view, the main design idea is to use the splitter window Pane (0,0) as the display area of switching view, Pane (0,1) Display the Advanced label window similar to Figure 3 Cwndtab. The CSplitterWnd window completes the work of both Ctabctrlview and Ctabsheetview in the above two methods as a container for the Viewport and label window. The source code for this method is also available at the site of the programmer's base camp.

V. Concluding remarks

The author through the analysis of the source code of several related programs, and in the actual programming process, according to the need to achieve their own tab View. This paper briefly summarizes three ways to realize the tab View, hoping to help the reader's programming.

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.