Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Override OnClose()

Tags:

c++

events

mfc

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?

like image 419
CruelIO Avatar asked Jun 17 '10 11:06

CruelIO


2 Answers

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()

like image 170
Brian R. Bondy Avatar answered Sep 28 '22 11:09

Brian R. Bondy


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);
like image 44
Steve A Avatar answered Sep 28 '22 12:09

Steve A