Closing MDI Child Window Via Button on CFormView Derived Class

Closing MDI Child Window Via Button on CFormView Derived Class

I’m writing an MDI application that has two documents. One of the documents has multiple views, which are implemented as FormViews. Several views for the same document can be open at the same time. I need to be able to close a view with a button. This leaves the document and any other views open. Both close from the system menu and work fine using the ‘X’ to close. I want the same functionality, but in a button on the form.

From within the trigger for the button, I’ve tried to execute the system menu close with the following:

This has no effect. I’ve also tried to call DestroyWindow() and OnClose() from the same trigger. OnClose() does nothing, and DestroyWindow() affects the window so it is not redrawn, but is still visible.

Any ideas?

You’re on the right track. However, you must post the message to the child frame window that contains the view. The following code will close a child window as a result of the user pressing a button.

With the assistance of ClassWizard, add a message map macro and message map function for a specified button’s click event to the CFormView derived class similar to the code extract below.

// CMyFormView.cpp…BEGIN_MESSAGE_MAP(CMyFormView, CFormView)        //{{AFX_MSG_MAP(CMyFormView)        ON_BN_CLICKED(IDC_BUTTON1, OnButton1)        //}}AFX_MSG_MAPEND_MESSAGE_MAP()…void CMyFormView::OnButton1() {        // Get parent, which should be the the child frame window in this case.        CWnd* pParentWnd = GetParent();                // Post the SC_CLOSE message to the child frame window.  This will eventually        // lead to the Window and its child controls being destroyed.        if (pParentWnd)                pParentWnd->PostMessage(WM_SYSCOMMAND, SC_CLOSE);        } 
Notice I call CWnd::GetParent() to acquire the parent window of CMyFormView which is a CWnd pointer. Then I call CWnd::PostMessage(?) on this window, causing the MDI child window to close properly.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several