Use MFC to create a new window in operation

zhaozj2021-02-11  215

Use MFC to create a new dynamic in the window of operation: Zheng Yong Published: 2001/03/23

Thesis:

This article describes a method of dynamically creates a new window in the program run in the Visual C 6.0 programming environment. This method uses the MFC's framework class, so the new window has its own menu bar, toolbar, and status bar. Keywords MFC, Visual C , Window, Frame

text:

Using the MFC Dynamically Create a new window in the run in the program run, you often use the dialog to give some prompts or receive feedback from the user. However, in a case, it is not enough to use the dialog box. We may need to pop up a new window, which contains your own menu bar, dialog, and status bar; Do not create a new window? This article gives a method under MFC. We know, under Windows programming, create a new window consisting of two steps: (1) Register the corresponding Windows window class; (2) Generate a window according to the registered window class. However, if you want to take advantage of C and MFC, we can use the ready-made MFC classes, and transformed to add our necessary elements. The specific example is given, the main window displays a rectangular graphics, and the new window containing the edit control will pop up in the main window. Note You can create multiple new windows. When the main window is turned off, all new windows are closed. Step 1. Create an SDI project Test. Edit the CTestView: OnDraw function, draw a rectangle in the customer area: void ctestview :: OnDraw (CDC * PDC) {ctestdoc * pdoc = getDocument (); assert_valid (pdoc); // Todo: add draw code for native data here // DRAW A Rectangle in Client PDC-> Rectangle (50, 50, 200, 200); PDC-> TextOut (10, 10, "Click the left mouse button in the window, create a new window");} Step 2: Use classwizard from cframewnd Inheriting a new window CNEWFRAME: Resources in resources: Simple, the toolbar still uses the toolbar and status of the main window.

Insertion tool bar and status bar of the object in the class declaration statement CNewFrame.H: protected: // control bar embedded members CStatusBar m_wndStatusBar; CToolBar m_wndToolBar; WM_CREATE message in response CNewFrame loaded toolbar and status bar in CNewFrame :: OnCreate : int CNewFrame :: OnCreate (lPCREATESTRUCT lpCreateStruct) {if (CFrameWnd :: OnCreate (lpCreateStruct) == -1) return -1; // TODO:! Add your specialized creation code here if (m_wndToolBar.CreateEx (this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) || m_wndToolBar.LoadToolBar (IDR_MAINFRAME)) // simplicity, still loaded // main window's toolbar, users can load their own toolbar {TRACE0 ( "! Failed to create toolbarn "); return -1; // fail to create} if (! M_wndstatusbar.create (this) ||! M_wndstatusbar.setindicators (Indicators, Sizeof (Indicators) / sizeof (uint)) {trace0 (" Failed to create status barn "); return -1; // fail to create} // TODO: Delete these three lines if you do not want the toolbar to // be dockable m_wndToolBar.EnableDocking (CBRS_ALIGN_ANY); EnableDocking (CBRS_ALIGN_ANY) ; DockControlBar (& m_wndToolBar); static int s_nNewFrameID = 0; s_nNewFrameID ; CString str; str.Format ( "New Window #% d", s_nNewFrameID); SetWindowText (str); // set the title of the new window return 0;} Step 3: Treatment of the destruction of the window. If we can make sure that only a new window is created, then this step can skip, we don't have to have additional code; however, it is often practical to create multiple windows, each window is independent of each other. Then, the problem destroyed by the window is very complicated. We know that the destruction of the window class in the MFC includes two aspects of content, first is the destruction of the window, followed by the desemerical structure of the window.

Because CNewFrame inherited from CFrameWnd, while the latter have delete this statement in its virtual function PostNcDestroy years, as follows: void CFrameWnd :: PostNcDestroy () {// default for frame windows is to allocate them on the heap // the default post -Cleanup is to 'delete this'. // NEVER EXPLICITLY CALL 'DELETE' ON A CFRAMEWND, USE DESTROYWINDOW INSTELETE THIS;} Description CFrameWnd includes its subclasses must be built on the heap. If we strictly create a new window, close it, then create, in turn, then everything is normal; if we have created multiple windows, then close the main window, all new windows will be closed as the end of the program However, memory leaks occur at this time, because we do not destroy new windows one by one when we exit, so the delete this in PostncDestroy is not executed. The solution is to save all new windows in the main window program. We use a list to save all pointers.

Add: #include "afxtempl.h" // clist's header file Description #include "newframe.h" // New window header Add member Add member: public: Clist m_listframe; // Save all new window Pointers When closing the new window, you should delete the pointer from the pointer list: void cnewframe :: postncdestroy () {// Todo: add your specialized code here and oR call the base class // because CFrameWnd :: PostNcDestroy () will delete this but // will not set the pointer to this frame (pFrame) to NULL, thus // this will cause the CMsgsManagerApp :: ExitInstance () to // delete the pointer again, and this will cause one exception // so we must travel the list frame to set the point to null // or delete it CTestApp * pApp = (CTestApp *) AfxGetApp (); POSITION pos = pApp-> m_listFrame .Find (this); if (pOS) PAPP-> M_ListFrame.Removeat (POS); cframeWnd :: postncdestroy ();} If the new window is not closed, you should traverse the list when you exit the main program, and destroy the window one by one: int CtestApp: : ExitInstance () {// Todo: add your specialized code here and / or call the base class cnewframe * pframe; // If there is a window when exiting the program, the window is not closed, then the window chain table // and close every frame of the listframe Trace ("count =% DN", m_listframe.getcount ()); while (! M_listframe.Isempty ()) {pframe = m_listframe.removehead (); if (pframe-> getsafehwnd ()) {trace ("count =% dn ", m_listFrame.GetCount ()); pFrame-> DestroyWindow (); // we need not delete pFrame // because pFrame inherit the CFrameWnd // and the virtual method PostNCDestroy of CFrameWnd // will delete this}} return CWinApp: : EXITINSTANCE ();} Step 4: Create a new window.

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

New Post(0)