c++win32guicreatewindowex

Failed CreateWindowEx. How do I get my window (with the button) to actually pop up for once?


I can only assume most of this works because I can't get past the CreateWindowEx check. If someone would double check all of my fun button code that would be great too.

#include <windows.h>
#include <tchar.h> //I was told I needed this line but I don't believe I do.
#define ID_BTNENCRYPT 0

const char g_szClassName[] = "MyWindowClass";

HWND button;

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)       {
switch(msg) { //Fun button stuff
    case WM_CREATE: {
        button = CreateWindow("button",
                              "Encrypt Message",
                              WS_VISIBLE | WS_CHILD | BS_DEFPUSHBUTTON,
                              450, 620, 200, 30,
                              hwnd, (HMENU) ID_BTNENCRYPT, GetModuleHandle(NULL), NULL);

        break;
    }
    case WM_COMMAND: { //More fun button stuff
        switch (LOWORD(wParam)){
            case ID_BTNENCRYPT: {
                ::MessageBox(hwnd, "This will be your message once I get my $h!t together", "Encrypted Message", MB_OK);
            }
        }
        break;
    }
    case WM_CLOSE:
        DestroyWindow(hwnd);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        DefWindowProc(hwnd, msg, wParam, lParam);

}

return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR     lpCmdLine, int nCmdShow) {
FreeConsole();

WNDCLASSEX wc;
HWND hwnd;
MSG Msg;

wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
if (!RegisterClassEx(&wc)) {
    ::MessageBox(NULL, "Window Registration Status: Hopelessly F***ed", "", MB_OK);
    return 0;
} //No apparent error in Window Registration

Here's where I need help

hwnd = CreateWindowEx(
    WS_EX_CLIENTEDGE,
    g_szClassName,
    "Great Window",
    WS_OVERLAPPEDWINDOW,
    300, 300,
    350, 350,
    NULL, NULL, hInstance, NULL);
if (hwnd == NULL) {
    ::MessageBox(NULL,"Window Creation Status: Gone to $h!t", "", MB_OK);
}

I unfortunately get the error message that yes, my window creation has failed.

ShowWindow(hwnd, nCmdShow); //Just the end of my code from here on out.
UpdateWindow(hwnd); //Hopefully there aren't any fatal errors.

while(GetMessage(&Msg, NULL, 0, 0) > 0) {
    TranslateMessage(&Msg);
    DispatchMessage(&Msg);
}

return Msg.wParam;
}

Solution

  • Your WndProc() is not returning the return value of DefWindowProc() for unhandled messages. There is a missing return statement, so you end up falling to return 0 for all messages. When WM_NCCREATE returns 0, CreateWindowEx() fails:

    If an application processes this message, it should return TRUE to continue creation of the window. If the application returns FALSE, the CreateWindow or CreateWindowEx function will return a NULL handle.

    You need to change this:

    default:
        DefWindowProc(hwnd, msg, wParam, lParam);
    

    To this:

    default:
        return DefWindowProc(hwnd, msg, wParam, lParam);