Win32 MessageBox没有出现

时间:2010-11-29 00:08:22

标签: messagebox winapi

我遇到了一个奇怪的问题。我正在VC ++ 2008中创建一个Win32应用程序,在调用MessageBox时创建一个封装大部分工作的类,以便轻松重复。消息框`已创建(我认为),但除非我按下Alt键,否则不会显示!

究竟发生了什么:

  1. 我运行程序

  2. 按Enter键

  3. 主窗口失去焦点

  4. 当我点击主窗口时发出哔声,好像有一个模态MessageBox存在

  5. 按Escape ...获得焦点或按Alt键,然后按下Alt键出现MessageBox(即菜单会掉落)!!!!!!

  6. P.S。它工作正常,但突然发生了这种情况。我没有发现任何差异 - 我甚至做了一个新项目!

    这应该是主程序:

    int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR    lpCmdLine, int       nCmdShow)
    {
        MSG msg;
        CWnd    cMainWindow(TEXT("DentoMan"), TEXT("Bejkoman")); // pass The class name and window name to the constructor
    
        cMainWindow.CreateDef(); //Create the Window
        while (GetMessage(&msg, NULL, 0, 0))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        return (int)msg.wParam;
    }
    

    虽然这是类文件

    CWnd::CWnd() {
    };
    
    CWnd::CWnd(LPTSTR lpszClassName, LPTSTR lpszWindowName) {
        CWnd::lpszClassName     = lpszClassName;
        CWnd::lpszWindowName    = lpszWindowName;
    };
    
    CWnd::~CWnd() {
    };
    
    // Create the window with default parameters
    HWND CWnd::CreateDef(void) {
        WNDCLASSEX wcex;
    
        wcex.cbSize = sizeof(WNDCLASSEX);
    
        wcex.style          = CS_HREDRAW | CS_VREDRAW;
        wcex.lpfnWndProc    = StaticWndProc;
        wcex.cbClsExtra     = 0;
        wcex.cbWndExtra     = 0;
        wcex.hInstance      = (HINSTANCE)GetModuleHandle(NULL);
        wcex.hIcon          = 0;
        wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
        wcex.hbrBackground  = (HBRUSH)(COLOR_WINDOW + 4);
        wcex.lpszMenuName   = 0;
        wcex.lpszClassName  = lpszClassName;
        wcex.hIconSm        = 0;
    
        RegisterClassEx(&wcex);
        g_hWnd = CreateWindowEx(0,lpszClassName, lpszWindowName, WS_OVERLAPPEDWINDOW,
            CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, wcex.hInstance, this);
        hInst   =   wcex.hInstance;  //Store hInstance in the class hInst variable
    
        if (!g_hWnd) return false;
        ShowWindow(g_hWnd, SW_SHOW);
        UpdateWindow(g_hWnd);
    
        return g_hWnd;
    }
    
    LRESULT CALLBACK CWnd::StaticWndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam) {
        /* The Only Message we take here so we store the 'this' pointer within the window to identify messages 
        comming from it by the 'this' pointer*/
        if ( Message == WM_CREATE ) {
            SetWindowLong( hWnd, GWL_USERDATA, (LONG)((CREATESTRUCT FAR *)lParam)->lpCreateParams);
        }
    
        /* Store the window pointer in the class pointer we just created in order to run the right public WndPRoc */
        CWnd *Destination = (CWnd*)GetWindowLong( hWnd, GWL_USERDATA );
    
        // If the hWnd has a related class, pass it through
        if (Destination) {
            return Destination->WndProc( hWnd, Message, wParam, lParam );
        }
    
        // No destination found, defer to system...
        return DefWindowProc( hWnd, Message, wParam, lParam );
    };
    
    LRESULT CWnd::WndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam) {
        // Determine message type
        switch (Message) {
            case WM_LBUTTONDOWN:
                {
                    /* this is a common trick for easy dragging of the window.this message fools windows telling that the user is
                     actually dragging the application caption bar.*/
                     SendMessage(hWnd, WM_NCLBUTTONDOWN, HTCAPTION,NULL);
                    break;
                }
    
            /*case WM_CREATE:
                break;
        */
    
            case WM_CLOSE:
                PostQuitMessage(0);
                break;
    
            case WM_DESTROY:
                UnregisterClass(lpszClassName, hInst);
                PostQuitMessage(0);
                break;
    
            case WM_KEYDOWN:    //KeyBoard keys
                // Which key was pressed?
                switch (wParam) {
                    case VK_ESCAPE: //close through escape key
                        PostQuitMessage(0);
                        return 0;
                    case VK_RETURN:
                        MessageBox(hWnd, TEXT("DFGDGD"), TEXT("DFGDFG"), NULL);
                        return 0;
                } // End Switch
    
                break;
    
            case WM_COMMAND:
                /*switch(LOWORD(wParam))
            {
            }*/
            break;
    
            case WM_PAINT:
                break;
    
            default:
                return DefWindowProc(hWnd, Message, wParam, lParam);
    
        } // End Message Switch
    
    return 0;
    };
    

    班级标题:

    class CWnd {
        public:
            CWnd();
            CWnd(LPTSTR lpszClassName, LPTSTR lpszWindowName);
            virtual ~CWnd();
            virtual HWND CreateDef(void);           // Create the window with default parameters
            virtual LRESULT     WndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam );
    
        private:
            static LRESULT CALLBACK StaticWndProc(HWND hWnd, UINT Message, WPARAM wParam, LPARAM lParam);
            HWND        g_hWnd;     //Global window handle for this window
            HINSTANCE   hInst;      //Global instance for this window
    
            LPTSTR          lpszClassName;
            LPTSTR          lpszWindowName;
    };
    

    P.S。我包含了所有需要的头文件,除MessageBox

    外一切正常

    这也是here

    上代码的链接

4 个答案:

答案 0 :(得分:9)

Ohhhhhhh最后我找到了这个问题的解决方案......并且让每个人都受益于问题是在WMPAINT的WndProc(.......)消息我在其中编写了一些代码并删除了所有代码使用BeginPaint和EndPaint函数,所以程序进入一个冻结期间,一旦任何东西被涂在它上面,包括MessageBox,但它只显示当我按下Alt我认为coz控件转移到系统的那一步显示系统菜单(i想)

解决方案是删除WM_PAINT消息处理程序或添加正常的BeginPaint和EndPaint函数

感谢所有传递我问题的人

答案 1 :(得分:3)

如果有人仍然感兴趣,这种方法有效:

MessageBox(NULL,L"error",L"Error",MB_ICONERROR|MB_DEFAULT_DESKTOP_ONLY);

答案 2 :(得分:3)

我有一个类似的问题,由WM_PAINT引起,如上所述。通过在那里添加return DefWindowProc(hWnd, Message, wParam, lParam);解决了这个问题。

答案 3 :(得分:2)

创建MessageBox时,您应该在CreateWindowEx中传递WS_CHILD 编辑2:
好的,试试吧。

MessageBox(hWnd, TEXT("DFGDGD"), TEXT("DFGDFG"), MB_OK);