CruelIO
CruelIO

Reputation: 18634

Override OnClose()

I got this class

class CWebBrowser2 : public CWnd

And i want to override OnClose What i have done so far is in the header file I added void OnClose(); and in the .cpp file i added

void CWebBrowser2::OnClose ()
{
        int i=0;
        i++;
}

But the OnClose is never called.

Then I tried to to modify the header file to

afx_msg void OnClose();
DECLARE_MESSAGE_MAP()

And added this to the .cpp file

BEGIN_MESSAGE_MAP(CWebBrowser2, CWnd)
    //{{AFX_MSG_MAP(CBrowserDlg)
    ON_WM_CLOSE()
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

But still OnClose is never called. I have tried to Change to OnClose to OnDestroy but that isnt called either.

any ideas on what Im doing wrong?

Upvotes: 6

Views: 15217

Answers (2)

Steve A
Steve A

Reputation: 2013

Although my answer is three years late, I expect others may end up here (as I did) trying to resolve this problem. The OP did write the message handling correctly, but when you dynamically create an ActiveX control (as is usually done when using CWebBrowser2), you need to subclass the HWND that is associated with the control. You can read about this at http://support.microsoft.com/kb/156051.

// This is how the control is normally created (i.e., dynamically):

/* CWebBrowser2 * */ pBrowser = new CWebBrowser2;
CWebBrowser2 * pBrowser = new CWebBrowser2;
ASSERT(pBrowser);

if (!pBrowser->Create(_T("windowname"), _T("classname"), WS_VISIBLE, CRect(0,0,0,0), this, ID_OF_BROWSER))
{
    TRACE( _T("An error occurred creating the Map tab"), true, false );
    delete pBrowser;
    pBrowser = NULL;
    return 0;
}

// Add these two lines so your control receives Windows messages:
HWND hWnd = pBrowser->Detach();
pBrowser->SubclassWindow(hWnd);

Upvotes: 1

Brian R. Bondy
Brian R. Bondy

Reputation: 347446

After adding the ON_WM_CLOSE() it should work. In which way are you closing the window?

In your header file of your class do you have this line? DECLARE_MESSAGE_MAP()

Upvotes: 5

Related Questions