Talk about "VC ++ 5.0 to achieve multi-view"

zhaozj2021-02-11  250

Practical skills and talk about "VC 5.0 to achieve multi-view" Zhejiang Yixian Zhonggong Temple, Zhejiang Yixian Zhonggong Temple, Feng Lei, Wang Lei - "Computer World" introduced two ways to achieve multi-view. The former, the author uses the MDI method to achieve, the programming is more complicated; the latter author uses the CSPillterWnd class to achieve more attention. - , Provide us with convenience. ---- Here is a brief introduction to cdocument :: addview and cdocument :: RemoveView, please refer to online documentation: void cdocument :: addView (cView * pView);

---- Add a new view for the current CDocument class instance. The parameter PVIEW is a pointer to the new view. Void CDocument :: RemoveView (CView * pView);

---- Remove one view from the current CDocument class instance. The parameter PVIEW is a pointer to the view to be removed. Note that it must be removed in the case where the view is invisible, otherwise an exception is generated. ---- Through these two functions, we can easily increase and remove views. ---- The following is an instance of implementation: ---- This instance implements a two-view function of a document. The view is derived from CVIEW to display the data in the document, and the view is derived from the CEDITVIEW to edit the data in the document. Use the views in the View menu to select the view. For additional information on how to implement the new view class and menu, please refer to other information. ---- The following is the main window class to add data members and message processing functions: cdemoview * m_pView1;

/ / Pointer to the pointer, CDemoview is generated by the application wizard

CMYEDITVIEW * m_PVIEW2;

/ / Pointer to the two pointers, CMYEDITVIEW is derived from CeditView

INT M_NWHICHVIEW;

/ / Indicent which one of the current is currently, and initializes 0

---- The following is a menu Select View III Handle: Void CMAINFRAME: ONVIEW2 ()

{

// Todo: add your command handler code here

IF (m_pView2 == null)

{

/ / Get a pointer to the view

M_PView1 = (CDemoview *) getActiveView ();

/ / Generate view two real estimation objects

M_PView2 = New CMYEDITVIEW;

// Pointer to acquire an activity document

CDEMODOC * PDOC = (cdemodoc *) getActiveDocument ();

/ / Create a window for the view 2 real estimation object,

For specific parameters, please refer to the CWIN :: Create of the online documentation

M_PView2-> Create (null, null, afx_ws_default_view,

RectDefault, this, AFX_IDW_PANE_FIRST 1, NULL);

// Replace the view one and view II of the ID number

// Make the main window correctly to occupy the current view throughout the customer area.

INT NID = m_pview2-> getdlgctrlid ();

m_pview2-> setdlgctrlid (AFX_IDW_PANE_FIRST);

M_PView1-> setdlgctrlid (NID);

// Display two view, hidden view

M_PView2-> showwindow (SW_SHOW);

M_PView1-> showwindow (sw_hide); // Add a new view

PDOC-> AddView (M_PView2);

// Set the view two active view

SetActiveView (M_PView2);

M_PView2-> setWindowText (pdoc-> m_hello);

RECALCLAYOUT ();

}

Else

IF (m_nwhichview == 0)

{// 同 同, 省 视 视 视 创 创 创 工作

INT NID = m_pview2-> getdlgctrlid ();

m_pview2-> setdlgctrlid (AFX_IDW_PANE_FIRST);

M_PView1-> setdlgctrlid (NID);

M_PView1-> showwindow (sw_hide);

M_PView2-> showwindow (SW_SHOW);

SetActiveView (M_PView2);

RECALCLAYOUT ();

}

m_nwhichview = 1;

}

---- The following is the process function of the menu selection view 1: if (m_nwhciew == 1)

{

// Principle

INT NID = m_pview1-> getdlgctrlid ();

m_pview1-> setdlgctrlid (AFX_IDW_PANE_FIRST);

M_PView2-> setdlgctrlid (NID);

M_PView2-> showwindow (sw_hide);

M_PView1-> showWindow;

SetActiveView (m_pView1);

RECALCLAYOUT ();

m_nwhichview = 0;

}

---- Then add member data CString m_hello to the document class CDEMODOC, which is used to display data and the editing data of the display. ---- Finally, the process functions of the view one and views are added separately. ---- Join the onDRAW handler in the view 1 for display data, as follows: void cdemoview :: Ondraw (CDC * PDC)

{

CDEMODOC * PDOC = getDocument ();

Ask_VALID (PDOC);

// Todo: Add Draw Code for Native Data HERE

RECT;

GetClientRect (& RECT);

PDC-> SetTextColor (RGB (0,128,128));

PDC-> DrawText (pdoc-> m_hello, pdoc->

M_Hello.getLength (), & Rect, Dt_wordbreak;

}

---- Join the processing function for the corresponding data change in the view II: Void CMyEditView :: OnChange ()

{

// Todo: IF this is a richedit control,

The Control Will Not NOT

// send this Notification UnlessNless Unless IOUOLERRIDE

The CEDITVIEW :: OnInitDialog ()

// Function to send the em_seteventmask

Message to the control

// with the enm_change flag

Orad Into the lparam mask.

// Todo: Add Your Control

Notification Handler Code Here

// Save data to the document

CDEMODOC * PDOC = (cdemodoc *) getDocument (); getWindowText (pdoc-> m_hello);

}

---- Note: Demoview.h and MyEditView.h and Demodoc.h three header files were added to MAINFRM.CPP before compiling. So I am very successful and can be compiled. ---- Summary: Use MDI and CSPLitter to achieve multi-view, nothing more than implicit use addView to implement one document, and here, the author directly uses AddView to implement more attention. Of course, friends may ask, this demonstration program, why can only be switched in various views, not to display it at the client area of ​​the main window. In fact, it is possible to display in the customer area, and the author has repeatedly experimented, found that two views can be displayed in the client area of ​​the same main window, but the view class window is not a normal window, no title, system control menu and zoom Reduce the button, so it lacks some of the properties of ordinary windows, such as not using a special means, cannot be used to drag and zoom out of the magnification. Of course, this can be implemented by some API functions, but this may be too troublesome. ---- Careful friends may notice that each view of MDI is actually placed at the client area of ​​the main frame window derived at the cmdichildwnd class, thereby gaining various features of the normal window, making each view window in the same master Coexis under the window, which is also a method of implementing a document with MDI. However, we can do it all, create the main frame window, and point your parent window pointer to the main frame window when establishing the view, thus obtaining the same effect as the same as the same frequency, but this may be slightly complicated. The separator window also provides a mechanism for the display view window and the relocation of each view window, and we can also achieve the repositioning of the view window without the divider window. Re-introduction. Interested friends can study the MFC source program implemented by cmdichildwnd and csplitterwnd.

转载请注明原文地址:https://www.9cbs.com/read-3988.html

New Post(0)