Closing MDI Child Window Via Button on CFormView Derived Class

Question:
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:

PostMessage(WM_SYSCOMMAND, SC_CLOSE);
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?

Answer:
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:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

Top 5 B2B SaaS Marketing Agencies for 2023

In recent years, the software-as-a-service (SaaS) sector has experienced exponential growth as more and more companies choose cloud-based solutions. Any SaaS company hoping to stay ahead of the curve in this quickly changing industry needs to invest in effective marketing. So selecting the best marketing agency can mean the difference

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.