Object communication in the MFC application

zhaozj2021-02-11  208

Visual C Based on MFC-based visual programming technology brings great convenience to programmers, programmers can use few energy to care about the user interface design, and put the main energy to prepare specific event operations code. Using the application framework constructed by the MFC AppWizard, including the following objects: application objects, document templates, main frames window objects, document objects, viewport objects, etc., their respective tasks are shown in Table 1. See the relationship between them Figure one.

Table an object and task of an application

Object task application objects are interfaces between applications and Windows, responsible for registering window classes, establishing an example, accepted, and sending messages. Document template link application object, document object, subframe window object, viewport object. The main frame window object constructs the appearance, management menu, toolbar, status bar, etc. Document object stores the application's data to implement disk I / O. Subframe window object management sub-window, document, window object, etc. Window objects displays data from the application, accept external events. Menu object management program menu. Toolbar of the toolbar object management program. Status section of the status bar object management program.

Figure An application, document template, document, frame window, and interrelationship between viewport objects

The main task of the program is distributed in these objects by using object division. These objects work together by communicating with each other with other object member functions and sending messages. When we did not figure out this object division mechanism of the MFC application and the communication methods between these objects, we always feel the difficulties of MFC programming, a fog, can't appreciate the charm of MFC programming.

From this point of view, a frequent problem that MFC programming is how to access other objects of the application to utilize functions that belong to these objects, that is, object communication. When we generate an application with AppWizard, the member function getDocument () is automatically added in the view class, with this function, and the view class can complete the communication from the view class to document object, you can operate the document class. Functions and member variables. But this is not enough, the following table lists the communication methods between the objects of the MFC application.

Table II application object communication method

Object communication object method Any object application object AFXGetApp (). Application Objects Main Frame Window Object m_pmainwnd. Document Object Window Object GetFirstViewPosition () Function Gets the first window location, with the GetNextView () function with the existing location parameter to get the pointer to the subsequent window. Document Object Document Template Object GetDoCtemplate (). Window Object Document Object GetDocument (), use the UpdateAllViews () function to notify the Window object update. Window object subframe window object getParentFrame (). Window Object Window Object GetFirstViewPosition () function Gets the first window location, with the GetNextView () function with the existing location parameters to get this window and the next pointer. Use the iskindof () function to determine. Subframe window object window object getActiveView (). Main frame window object subframe window object mdiGetAnAntective (). Application Object Menu Object GetMenu (). Any object status bar object CStatusBar * pStatus = (Cstatus-Bar *) AfxGetApp () -> m_pMainWnd-> GetDescendantWindow (AFX_IDW_STATUS_BAR) Toolbar object is any object CToolBar * pTool = (CToolBar *) AfxGetApp () -> m_pMainWnd-> GetDescendantWindow ( AFX_IDW_TOOLBAR)

Let's give an example when double-click the window, change the following function: Change the value of the content and variables displayed by the window; change the title of the main frame window and subframe window; change the contents of the status bar; add a menu option Change tool bar icon. Run AppWizard to generate a multi-document name TEST project. To join the project ID number ID_MY_MENU resources; increase the member variable CString TestText is CTestDoc class; class CTestDoc: public Cdocument {public: CString TestText;}; BOOL CTestDoc :: OnNewDocument () {if (CDocument :: OnNewDocument ()!) Return False; TestText = "The first time entry, no double-click event!"; // Initialize TestText; return true;} void ctestview :: OctRaw (CDC * PDC) {ctestdoc * pdoc = getDocument (); assert_valid (pdoc); PDC; -> Textout (5, 5, pdoc-> testtext);} Use classwizard to add a function of the left mouse button for the CTestView class; void ctestview :: ONLBUTTONDBLCLK (uint nflags, cpoint point) {cview :: ONLBUTTONDBLCLK (nflags, Point); // CTestDoc member variable TestText is re-assured! CTestDoc * pdoc = getDocument (); pdoc-> testtext = "Double-click the event to occur, the variable TestText is re-assigned!"; InvalIDateRect (null, true); // Title of the chassis window window

CWND * pParent = getParentFrame ();

PParent-> setWindowText ("Double-click event, the variable TestText is re-assaped! This is in the sub-window !!"); // Change the title AFXGETAPPPP () -> m_pmainwnd-> setWindowText ("Double-click event, variable TestText is reinfined! This is in the main window! "); // Change state strip content cstatusbar * pstatus = (cstatusbar *) AFXGetApp () -> m_pmainwnd-> getDescendantWindow (AFX_IDW_STATUS_BAR); if (pstatus) PStatus-> SetPaNetext (0, "Double-click the event, the variable TestText is re-assured! This is in the status bar !!"); // Insert a new menu item ID_MY_MENU, the title is a new menu cmenu * Pmenu; pmenu = AFXGetApp () -> m_pmainwnd -> getMenu (); pmenu-> insertmenu (-1, mf_bycommand, id_my_menu, "new menu"); afxgetApp () -> m_pmainwnd-> DrawMenubar (); // Change Toolbar ctoolbar * ptool = (ctoolbar *) AFXGetApp () -> m_pMainWnd-> GetDescendantWindow (AFX_IDW_TOOLBAR); pTool-> SetButtons (NULL, 8); pTool-> SetButtonInfo (0, ID_FILE_NEW, TBBS_BUTTON, 0); pTool-> SetButtonInfo (1, ID_FILE_OPEN, TBBS_BUTTON, 1); pTool-> SetButtonInfo (2, ID_FILE_SAVE, TBBS_BUTTON, 2); pTool-> SetButtonInfo (3, ID_SEPARATOR, TBBS_SEPARATOR, 4); pTool-> SetButtonInfo (4, ID_EDIT_CUT, TBBS_BUTTON, 4); pTool-> SetButtonInfo (5, ID_EDIT_COPY , Tbbs_button, 5); ptool-> setButtonInfo (6, id_edit_paste, tbbs_button, 6); P Tool-> SetButtonInfo (7, ID_separator, TBBS_SEPARATOR, 4);}

Visual C 2.x MFC Windows programming, Wu Zi Xiu, Lin Zhengmin, Southwest Jiaotong University Press, 1996. Visual C 5.0 Practical Programming Technology, Shi Huikang, China Water Conservancy Power Press, 1998. Microsoft Visual C self-study tutorial, David A.HOLZGANG Tsinghua University Press, 1996.

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

New Post(0)