MessageBox没有出现

时间:2017-12-07 14:42:02

标签: c++ winapi codeblocks

我在codeblocks中编码。 编译器= gcc版本4.9.2(tdm-1) 我在WM_COMMAND中调用MessageBox。 当我按下按钮时,主窗口停止,但MessageBox不显示。 我确定WindowProc收到按钮事件!

但是如果我创建一个线程,并在线程函数中调用MessageBox,则当我按下按钮时,MessageBox将显示。但是如果我在线程中调用MessageBox,那么MessageBox就不能停止主窗口。

我如何解决这个问题?

#define IDI_BUTTON_1  10001
#define IDI_BUTTON_2  10002
#ifndef UNICODE
#define UNICODE
#endif
#include "resource.h"
#include "main.h"
#include <windows.h>
#include <stdio.h>

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR pCmdLine, int nCmdShow)
{
    // Register the window class.
    const wchar_t CLASS_NAME[]  = L"mywindow";

    WNDCLASS wc = { };

    wc.lpfnWndProc   = WindowProc;
    wc.hInstance     = hInstance;
    wc.lpszClassName = CLASS_NAME;
    wc.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MYICON));
    //wc.style = CS_HREDRAW|CS_VREDRAW;

    RegisterClass(&wc);

    // Create the window.

    HWND hwnd = CreateWindow( CLASS_NAME,
                              L"Mywindow",
                              WS_OVERLAPPEDWINDOW,
                              CW_USEDEFAULT,
                              CW_USEDEFAULT,
                              250,
                              200,
                              NULL,
                              NULL,
                              hInstance,
                              NULL
                             );

    if (hwnd == NULL)
    {
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    // Run the message loop.

    MSG msg = { };
    while (GetMessage(&msg, NULL, 0, 0))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    int i;

    switch (uMsg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;

    case WM_CREATE:
        {

            HWND hwndButton1 = CreateWindow( L"button" ,L"button1",
                      WS_CHILD|WS_VISIBLE,
                      10,
                      10,
                      75,
                      50,
                      hwnd,
                      (HMENU)IDI_BUTTON_1,
                      ((LPCREATESTRUCT)lParam)->hInstance,
                      NULL);


        HWND hwndButton2 = CreateWindow( L"button" ,L"button2",
                      WS_CHILD|WS_VISIBLE,
                      120,
                      10,
                      75,
                      50,
                      hwnd,
                      (HMENU)IDI_BUTTON_2,
                      ((LPCREATESTRUCT)lParam)->hInstance,
                      NULL);



            return 0;
        }

    case WM_COMMAND:
        {
            switch (LOWORD(wParam))
            {
            case IDI_BUTTON_1:
                    MessageBoxW( hwnd, L"button1", L"button1", MB_ICONERROR|MB_DEFAULT_DESKTOP_ONLY );
                    break;
            case IDI_BUTTON_2:
                    MessageBoxW( hwnd, L"button2", L"button2", MB_ICONERROR|MB_DEFAULT_DESKTOP_ONLY );
                    break;

            }

            return 0;
        }

    case WM_PAINT:
        {
            return 0;
        }

    }
    return DefWindowProc(hwnd, uMsg, wParam, lParam);
}

0 个答案:

没有答案