Home > Software engineering >  C windows.h - window doesnt open
C windows.h - window doesnt open

Time:01-01

#include<windows.h>
#include<iostream>
#include<tchar.h>

LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);

TCHAR szClassName[ ] = _T("ClassName");

int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow)
{
    HWND hwnd;
    MSG messages;
    WNDCLASSEX wincl;
    wincl.hInstance = hThisInstance;
    wincl.lpszMenuName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;
    wincl.style = CS_DBLCLKS;
    wincl.cbSize = sizeof(WNDCLASSEX);
    wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
    wincl.cbClsExtra = 0;
    wincl.cbWndExtra = 0;
    wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
    if (!RegisterClassEx(&wincl))
        return 0;
    hwnd = CreateWindowEx(0, szClassName, _T("ClassName"), WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 544, 375, HWND_DESKTOP, NULL, hThisInstance, NULL);
    ShowWindow(hwnd, nCmdShow);
    while(GetMessage(&messages, NULL, 0, 0))
    {
        TranslateMessage(&messages);
        DispatchMessage(&messages);
    }
    return messages.wParam;
}

LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
        default:
            return DefWindowProc(hwnd, message, wParam, lParam);
    }
    return 0;
}

The window doesnt show up/open. I've tried for 6 hours straight to find the problem, it doesn't give me an error in the console, it just creates the .exe window but not the other window. I'm new to this, so idk what to do or how to reply to this.

CodePudding user response:

This statement is wrong:

wincl.lpszMenuName = szClassName;

It needs to be this instead:

wincl.lpszClassName = szClassName;

You are not checking if CreateWindowEx() fails (returns NULL):

hwnd = CreateWindowEx(...);
if (hWnd == NULL) ... // <-- ADD THIS

In this situation, it does fail, because you are not registering the class name correctly, so creating the window will report an ERROR_CANNOT_FIND_WND_CLASS (1407) error from GetLastError().

  • Related