MessageBox弹出后无响应

这几天学习了自定义模板对话框类,就是说不用系统定义的模板对话框类来创建对话框

消息处理就不用返回0了,可直接返回DefWindowProc

HINSTANCE hInstance = ::GetModuleHandle(NULL);

    if (bUserDlgClsName)
    {
        TCHAR szDlgClassName[] = TEXT("MyDialogClassName");

        WNDCLASSEX wndClsEx = { 0 };
        wndClsEx.cbSize = sizeof(WNDCLASSEX);
        if (FALSE == GetClassInfoEx(hInstance, szDlgClassName, &wndClsEx))
        {
            wndClsEx.lpfnWndProc = QWnd::AfxQWindowProc;
            wndClsEx.cbClsExtra = 0;
            wndClsEx.cbWndExtra = DLGWINDOWEXTRA;         //这个必填这
            wndClsEx.hInstance = hInstance;
            wndClsEx.hIcon = ::LoadIcon(hInstance, IDI_APPLICATION);
            wndClsEx.hIconSm = ::LoadIcon(hInstance, IDI_APPLICATION);
            wndClsEx.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
            wndClsEx.lpszClassName = szDlgClassName;
            wndClsEx.lpszMenuName = NULL;
            wndClsEx.style = CS_HREDRAW | CS_VREDRAW;
            wndClsEx.hCursor = ::LoadCursor(hInstance, IDC_ARROW);

            if (FALSE == RegisterClassEx(&wndClsEx))
                return FALSE;

        }
    }

    m_hWnd = ::CreateDialogParam(hInstance,
        MAKEINTRESOURCE(id),
        pParent ? pParent->m_hWnd : NULL,
        (DLGPROC)QWnd::AfxQWindowProc,
        (LPARAM)this);

 

可问题就来了,在这对话框上弹出MessageBox的时候就没反应了,

找了好久,跟踪消息后发现,

MessageBox的消息也进入了消息循环,它就不能直接返回DefWindowProc,要返回0

这就尴尬了,那这自定义对话类就没有意义了,

还是只能在消息循环中判断是否是模板对话框消息来处理了。

在窗口成员变量添加:BOOL m_bDialog;来判断是否是模板对话框的消息

QWnd* pWnd = nullptr;
    if (WM_CREATE == uMsg || WM_NCCREATE == uMsg)
    {
        pWnd = (QWnd*)((LPCREATESTRUCT)lParam)->lpCreateParams;
        if (pWnd != nullptr)
        {
            pWnd->m_hWnd = hWnd;
            ::SetWindowLongPtr(hWnd, GWLP_USERDATA, LONG_PTR(pWnd));

        }
    }
    else if (WM_INITDIALOG == uMsg)
    {
        QDialog* pDlg = (QDialog*)lParam;
        if (pDlg != nullptr)
        {
            pDlg->m_hWnd = hWnd;
            ::SetWindowLongPtr(hWnd, GWLP_USERDATA, LONG_PTR(pDlg));
            return pDlg->OnInitDialog(pDlg->m_lParam);

        }
    }
    else
    {
        pWnd = (QWnd*)::GetWindowLongPtr(hWnd, GWLP_USERDATA);
    }


    if (pWnd != nullptr)
    {
        LRESULT lResult = 0;

        const MESSAGE_MAP* pMsg = pWnd->FindMessageMap(uMsg, wParam, lParam);
        if (pMsg)
        {


            FuntionStyle funStyle;
            funStyle.pfn = pMsg->pfn;

            switch (pMsg->singStyle)
            {
                case sing_vv:
                    (pWnd->*funStyle.pfn_vv)();
                    break;

                case sing_v_l:
                    (pWnd->*funStyle.pfn_v_l)(lParam);
                    break;

                case sing_v_wl:
                    (pWnd->*funStyle.pfn_v_wl)(wParam, lParam);
                    break;

                case sing_v_uii:
                    (pWnd->*funStyle.pfn_v_uii)(wParam, LOWORD(lParam), HIWORD(lParam));
                    break;

                case sing_v_up:
                    (pWnd->*funStyle.pfn_v_up)(wParam, POINT{ LOWORD(lParam), HIWORD(lParam) });
                    break;

                case sing_v_uh:
                    (pWnd->*funStyle.pfn_v_uh)(LOWORD(wParam), (HWND)lParam);
                    break;

                case sing_l_v:
                    lResult = (pWnd->*funStyle.pfn_l_v)();
                    break;

                case sing_l_l:
                    lResult = (pWnd->*funStyle.pfn_l_l)(lParam);
                    break;

                case sing_l_wl:
                    lResult = (pWnd->*funStyle.pfn_l_wl)(wParam, lParam);
                    break;

                default:
                    break;
            }

            if (lResult)
                return lResult;

            
        }

        if (pWnd->m_bDialog)
            return 0;
    }
    

    return DefWindowProc(hWnd, uMsg, wParam, lParam);

 

在消息循环中判断:

if (pWnd->m_bDialog)
return 0;

否则返回DefWindowProc

 

posted @ 2023-01-02 12:12  初吻给了烟灬  阅读(106)  评论(0编辑  收藏  举报