 大家都知道回调函数字只能写成全局或静态的,要想回掉类成员函数,已经有很多人讲过几种方法,但是如果想在系统函数里面回调类 成员函数,还有一定困难,比如在DialogBox(...)中传入类成员函数处理消息过程。 本人给出一个简单的思路:将类成员函数写成虚函数,直接访问虚函数表,得到函数地址,再传递给调用函数,比如说DialogBox(...); 例子如下: // TestCallbackDlg.h : header file class CTestCallbackDlg : public CDialog { // Construction public:
virtual LRESULT DlgProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);//放在最前面.
CTestCallbackDlg(CWnd* pParent = NULL); // standard constructor ...
// TestCallbackDlg.cpp : implementation file //
LRESULT CTestCallbackDlg::DlgProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message) { case WM_COMMAND: { TRACE("DlgProc %d\r\n",hWnd);
if( wParam ==IDOK) { //AfxMessageBox("gfhdfhfgh");/*前后都不响应,MessageBox不消失?*/ ::MessageBox(hWnd,"HAHAHA","dfgdfgh",MB_OK); ::EndDialog(hWnd,IDOK); //AfxMessageBox("gfhdfhfgh"); } break; }
case WM_CLOSE: { AfxMessageBox("gfhdfhfgh"); ::EndDialog(hWnd,IDOK); break; } } return ::DefWindowProc(hWnd, message, wParam, lParam); }
//从虚函数表中获取函数地址. void CTestCallbackDlg::OnOK() { // TODO: Add extra validation here unsigned long pThis = (unsigned long) this; DLGPROC TempFuncAddr = NULL; _asm { mov eax,pThis; mov edx,dword ptr [eax]; mov eax, dword ptr [edx+0D8h]; mov TempFuncAddr,eax; }
TRACE("CTestCallbackDlg %d\r\n",this->m_hWnd); //TempFuncAddr中就是函数LRESULT CTestCallbackDlg::DlgProc(...)的地址
DialogBox(AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDD_ABOUTBOX), this->m_hWnd, (DLGPROC)TempFuncAddr); }
但是,程序中弹出MessageBox()后,就不响应了,希望高手指点。本文旨在抛砖引玉。我的MAIL [email protected],欢迎探讨.

|