覆盖 OnClose() 方法

6
我得到了这个类
class CWebBrowser2 : public CWnd

我希望覆盖OnClose函数。到目前为止,我在头文件中添加了void OnClose();,并在.cpp文件中添加了以下内容:

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

但是OnClose从未被调用。

然后我尝试修改头文件为

afx_msg void OnClose();
DECLARE_MESSAGE_MAP()

然后将这段代码添加到 .cpp 文件中

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

但仍然没有调用OnClose。我尝试将OnClose更改为OnDestroy,但也不起作用。有任何想法吗?
2个回答

5
在添加了 ON_WM_CLOSE() 后,它应该可以工作。你是用什么方式关闭窗口的?
在你的类的头文件中,是否有这一行?DECLARE_MESSAGE_MAP()

1

虽然我的回答晚了三年,但我希望其他人(就像我一样)也可能会在这里尝试解决这个问题。 OP确实正确编写了消息处理,但是当您动态创建ActiveX控件时(通常在使用CWebBrowser2时),您需要对与控件关联的HWND进行子类化。 您可以在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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接