getmessage中的无限循环(DispatchMessage(& msg);无效)

时间:2013-07-17 14:55:41

标签: winapi button dialog dispatch getmessage

我正在使用资源编辑器创建一个按钮应用程序。创建按钮后,我尝试这样做 -

 m_hwndPreview = CreateDialogParam( g_hInst,MAKEINTRESOURCE(IDD_MAINDIALOG), m_hwndParent,(DLGPROC)DialogProc, (LPARAM)this);


        if (m_hwndPreview == NULL)
        {
          hr = HRESULT_FROM_WIN32(GetLastError());
        }
    MSG  msg;
        BOOL bRet;
        while ( (bRet=GetMessage (& msg,0, 0,0)) != 0)
        {// this msg always contains the data like -msg = {msg=0x0000c03e wp=0x0000000000000012//always 12 I don't know why ??  lp=0x0000000000000000}
            if (bRet == -1)
            {
                bRet = HRESULT_FROM_WIN32(GetLastError());
                MessageBox(NULL, L"Hurr  i am the error",L"Error",MB_ICONERROR | MB_OK);
            }

            else if (!IsDialogMessage (m_hwndPreview, & msg))
            {
                 TranslateMessage (&msg); //on debugging  TranslateMessage = 0x000007feee615480 TranslateMessage
                 DispatchMessage(& msg ); //but show nothing when I put cursor on this method to know the value that means it's not called

                 MessageBox(NULL, L"there is no error in receiving before dispatch the message",L"Error", 
                 MB_ICONERROR | MB_OK);//this messagebox  repeats again and again after the call to DialogProc function and I am not able to come out of the loop and here I need to restart my PC

在其他地方,我定义了createialog函数,如下所示 - //这个函数只在createDialogParam()函数上调用。然后控件转到getmessage,其中所有东西都循环。

BOOL CALLBACK AMEPreviewHandler::DialogProc(HWND m_hwndPreview, UINT Umsg, WPARAM wParam, LPARAM lParam) 
    { //this dialogProc function is declares Static some where in the code otherwise the createdialogparam will give error DLGPROC  is invalid conversion

//this Umsg alays creates strange value like 48 and 32 etc.. and Wparam contains a very big value like 12335423526 (I mean big and strange) and lparam contains 0.
        switch(Umsg)
        {
        case WM_INITDIALOG: 
            {
                MessageBox(NULL, L"Inside the WM_INITDIALOG function",L"Error", 
                MB_ICONERROR | MB_OK);

            return TRUE;
            }
            break;

        case WM_CREATE:
            {
            /////////////////
            MessageBox(NULL, L"Inside the WM_CREATE",L"Error", 
                MB_ICONERROR | MB_OK);
            /////////////////////////////////
            }
            break;

        case WM_COMMAND:
            {   //here are my two buttons created by me which should show messagebox on click
                int ctl = LOWORD(wParam);
                int event = HIWORD(wParam);//I don't know why this event is in blue colour ..  but its not the pqrt of problem right now.

                if (ctl == IDC_PREVIOUS && event == BN_CLICKED ) 
                {         
                    MessageBox(m_hwndPreview,L"Button  Clicked is next inside WM_COMMAND ",L"BTN WND",MB_ICONINFORMATION); 
                    return 0;
                }         

                if (ctl == IDC_NEXT && event == BN_CLICKED ) 
                {         
                    MessageBox(m_hwndPreview,L"Button  Clicked is previous inside WM_COMMAND",L"BTN WND",MB_ICONINFORMATION);
                    return 0;
                }         

                return FALSE;

            }break;


        case WM_DESTROY:
            {

                ////////////////::
                    MessageBox(NULL, L"Inside the WM_DESTROY",L"Error", 
                MB_ICONERROR | MB_OK);
                    //////////////////

                PostQuitMessage(0);
                return 0;

            }
            break;
            case WM_CLOSE:
                {
                    MessageBox(NULL, L"Inside the WM_CLOSE",L"Error", 
                MB_ICONERROR | MB_OK);

                    DestroyWindow (m_hwndPreview);
                    return TRUE;            

                }
                break;          

        }
                MessageBox(NULL, L"outside the DefWindowProc function",L"Error", 
                MB_ICONERROR | MB_OK);

        return 0;
    }

出现的问题是,当我首次亮相时,控件首先转到CreateDialogParam然后转到getmessage,控件不会从循环中退出导致重启问题。而且我在预览窗格中没有显示按钮和图像。如果一切顺利的话,我的期望是在调试后应该在预览窗格上显示图片,我有2个按钮" Next"和"以前"但它只显示一个空白窗口(我已经使用资源编辑器创建的按钮和照片......这是正确的我确信这一点)..但我不知道为什么我不会出来没有调用getmessage函数和dispatchmessage(因为我在调试时看到过)。

2 个答案:

答案 0 :(得分:0)

你应该写

return true;

就在DispatchMessage(msg)之后;

和tehn调试它并通知我有关调试的结果。

答案 1 :(得分:0)

所以现在你可以尝试注释getmessage部分代码,如果问题可能会出来,因为你正在使用IDD_MAINDIALOG创建按钮,你的createialogparam会直接调用你的dailogproc函数,你接收WM_COMMAND并且你的代码可以处理。< / p>