WM_COMMAND内的消息框不起作用! (WIN32 API)

问题描述 投票:0回答:1

我是Win32 API的新手,正在尝试学习它。我成功创建了一个窗口,它运行完美。我向其添加了一个按钮,并希望在单击时显示一个消息框。该按钮可以正常工作,但是WM_COMMAND中的消息框根本不会出现,并且消息框下面的代码也无法执行。

我已经在线检查了操作方法,它似乎对他们有用,但对我却无效。这是代码

#include <Windows.h>
#include <tchar.h>
#include <stdlib.h>
#include <string.h>


WNDCLASSEX wcex;
static TCHAR szWindowClass[] = _T("DesktopApp");
static TCHAR szTitle[] = _T("First Application");

HINSTANCE hInst;

LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

int CALLBACK WinMain(_In_ HINSTANCE hInstance,_In_opt_ HINSTANCE hPrevInstance,_In_ LPSTR lpCmdLine,_In_ int nCmdShow)
{
    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc = WndProc;
    wcex.cbClsExtra = 0;
    wcex.cbWndExtra = 0;
    wcex.hInstance = hInstance;
    wcex.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wcex.lpszMenuName = NULL;
    wcex.lpszClassName = szWindowClass;
    wcex.hIconSm = LoadIcon(wcex.hInstance, IDI_APPLICATION);

    if (!RegisterClassEx(&wcex))
    {
        MessageBox(NULL, _T("Call to Register Failed"), _T("Windows Desktop Guided Tour"), NULL);

        return 1;
    }

    hInst = hInstance;

    HWND hwnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 500, NULL, NULL, hInstance, NULL);

    if (!hwnd)
    {
        MessageBox(NULL, _T("Failed to create a window"), _T("Windows Desktop Guided Tour"), NULL);
        return 1;
    }

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

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

    return (int)msg.wParam;
}

HWND button;


LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    PAINTSTRUCT ps;
    HDC hdc;
    TCHAR greeting[] = _T("Hello world! This is the first ever application window created by dumb Bhavin.");

    switch (message)
    {
    case WM_CREATE:
        button = CreateWindow(_T("BUTTON"),_T("1") ,WS_TABSTOP | WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON, 100, 40, 50, 30, hWnd, (HMENU)1, NULL, NULL);
        break;

//////////////////////////////////THIS IS WHERE THE ISSUE IS///////////////////////////////////////////
    case WM_COMMAND:
    {
        if (LOWORD(wParam) == 1)
        {
            OutputDebugString(_T("The compiler executes this! That means the button is working"));                           
            MessageBox(NULL, L"Here it is", L"ok", NULL);                   //Message box does not appear at all. The code below it does not execute at all.
            OutputDebugString(_T("The compiler DOES NOT execute this!"));
        }
        break;
    }
//////////////////////////////////////////////////////////////////////////////////////////////////////
    case WM_PAINT:
    //  hdc = BeginPaint(hWnd, &ps);
    //  TextOut(hdc, 5, 5, greeting, _tcslen(greeting));

        EndPaint(hWnd, &ps);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
        break;
    }
    return 0;
}




编辑1:这是一小段视频,介绍发生了什么。Error Video

注意:我在此传递了hWnd参数,而不是NULL。传递hWnd作为第一个参数也无济于事。

c++ visual-studio winapi messagebox
1个回答
0
投票

如果使用

MessageBox( hWnd, L"Here it is", L"ok", NULL );

它的确显示了,尽管我还没有找到很好的解释。假设它与它的模式性质以及多个UI线程有关。


0
投票

正如@IInspectable指出的,

问题是我一半执行的WM_Paint。取消注释BeginPaint行即可解决此问题。

或将其作为return DefWindowProc(hWnd, message, wParam, lParam);直接传递给DefWindowProc也可以。

感谢大家帮助我解决我的问题。

© www.soinside.com 2019 - 2024. All rights reserved.