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

标签 c++ visual-studio winapi messagebox

我是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作为第一个参数也无济于事。

最佳答案

正如@IInspectable指出的那样,

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

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

感谢大家为我的问题提供帮助。

关于c++ - WM_COMMAND内的消息框不起作用! (WIN32 API),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59471442/

相关文章:

vb.net - 如何使用 GetAsyncKeyState 实现全局热键?

c++ - 如何在列表框窗口过程中捕获 VK_ESCAPE

C++ #pragma omp 并行 : big tasks or small ones?

c++ - 从指针中查找指针

c++ - 在if语句中将多个字符变量转换为数字

c++ - 什么链接到 MSVCRTD?

visual-studio - 使用 Visual Studio 2017 的 Azure 数据工厂项目

c# - 在 Visual Studio 中从 C# 调用 Python 函数 Python 支持 VS 2017

c++ - 调用 FindConnectionPoint 时访问冲突写入内存

c++ - Mac OSX 下的 SDL 中不显示图像和文本