MMC Studios
MMC Studios

Reputation: 9

The window I created in C++ is not displayed

I tried to create a window in an Empty C++ Project in Visual Studio, but when I run it, it shows me no window. However, it doesn't give me any error too.

#include <Windows.h>

using namespace std;

int CALLBACK WinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR     lpCmdLine,
    int       nCmdShow ) 
{
    const auto pClassName = "TextClass";
    WNDCLASSEX wc = {0};
    wc.cbSize = sizeof(wc);
    wc.style = CS_OWNDC;
    wc.lpfnWndProc = DefWindowProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0;
    wc.hInstance = hInstance;
    wc.hIcon = nullptr;
    wc.hCursor = nullptr;
    wc.hbrBackground = nullptr;
    wc.lpszMenuName = pClassName;
    wc.hIconSm = nullptr;
    RegisterClassEx(&wc);
    HWND hWnd = CreateWindowEx(
        0,
        pClassName,
        "A sad Window",
        WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU,
        200, 200, 640, 480,
        nullptr, nullptr, hInstance, nullptr);
    ShowWindow(hWnd, SW_SHOW);
    while (true);
    return 0;
}

Upvotes: 0

Views: 431

Answers (1)

jwezorek
jwezorek

Reputation: 9525

You have a typo: you are setting

 wc.lpszMenuName = pClassName;

instead of wc.lpszClassName.

I believe just fixing that will get the window on the screen but the executable will then hang with it on the screen because nothing is fielding messages.

A minimal message loop instead of

while (true) ...

would be

MSG msg;
while( GetMessage( &msg, NULL, 0, 0 ) > 0 )
    DispatchMessage( &msg );

Upvotes: 2

Related Questions