1
votes

I am new to Win32 API and trying to learn it. I was successful to create a window and it works perfectly. I added a button to it and want to show a message box when clicked. The button works perfectly but the message box in WM_COMMAND does not appear at all and the code below message box does not get executed as well.

I have checked online for how to do it and it seems to work for them but not me. Here is the code

#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;
}




Edit 1: Here is a small video of what exactly happens. Error Video

Note : I passed hWnd parameter in this instead of NULL. Passing hWnd as first Parameter does not help either.

1
Any chance the message box does show but you don't see it? Would explain why the line after it doesn't execute. Though there can be other reasons.. Can you attach the debugger and see what the program is really doing at that point? - stijn
@stijn No. I double-checked. Nothing pops up. I even moved the screen but nothing was behind it. - Bhavin Lathia
The issue seems to be related to the half-implemented WM_PAINT handler. You can either comment in the first line (with BeginPaint), or pass it to DefWindowProc without handling it. I don't know, why it causes the effect it does. P.S.: The video link doesn't work for me. - IInspectable
@IInspectable Wow. That actually worked. I uncommented the BeginPaint! why was it causing the problem though? IDK. Thank you so much. - Bhavin Lathia
I've rolled back your edit. It is inappropriate to edit SOLVED into the title, or add a solution to the question. If one of the answers (including yours) provided the solution, you can indicate that by checking that answer as accepted. - Ken White

1 Answers

2
votes

The issue was my half-implemented WM_Paint. Uncommenting the BeginPaint line solves the problem.

or, passing it directly to DefWindowProc as return DefWindowProc(hWnd, message, wParam, lParam); works too.