Why does clicking on edit box close win32 window(C++)?

Viewed 111

The code below should create a simple window with a menu, static text, and an edit box but When I try to create an edit box it either does not run at all (with no errors) or when it does run and I try to click on the edit box the window closes. I am really stuck and any help would be greatly appreciated! Side note: I am a beginner at winapi/gui's with c++ so please be respectful.

#include <Windows.h>
#include <iostream>

#define FILE_MENU_NEW 1
#define FILE_MENU_OPEN 2
#define FILE_MENU_EXIT 3

struct WindowPosition
{
    int x, y = 0;
} winPos;

struct WindowSize
{
    int width = 600;
    int height = 500;
} winSize;

HMENU hMenu;

// Window proc function
LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
void addMenus(HWND);
void AddControls(HWND);

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR args, int ncmdshow)
{
    WNDCLASSW wc = { 0 };

    wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hInstance = hInst;
    wc.lpszClassName = L"myWindowClass";
    wc.lpfnWndProc = WindowProcedure;

    if (!RegisterClassW(&wc))
    {
        return -1;
    }

    CreateWindowW(L"myWindowClass", L"My Window", WS_OVERLAPPEDWINDOW | WS_VISIBLE, winPos.x, winPos.y, winSize.width, winSize.height,
        NULL, NULL, NULL, NULL);

    // Window loop
    MSG msg = { 0 };
    while (GetMessage(&msg, NULL, NULL, NULL))
    {
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }

    return 0;
}

LRESULT CALLBACK WindowProcedure(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
    switch (msg)
    {
    case WM_COMMAND:
        if (wp == 1)
        {
            MessageBeep(MB_OK);
        }
        else if (FILE_MENU_EXIT)
        {
            DestroyWindow(hWnd);
        }
        break;
    case WM_CREATE:
        addMenus(hWnd);
        AddControls(hWnd);
        break;
    case WM_DESTROY:
        PostQuitMessage(0);
        break;
    default:
        return DefWindowProcW(hWnd, msg, wp, lp);
    }
}

void addMenus(HWND hWnd)
{
    hMenu = CreateMenu();
    HMENU hFileMenu = CreateMenu();

    AppendMenu(hFileMenu, MF_STRING, FILE_MENU_NEW, L"New");
    AppendMenu(hFileMenu, MF_STRING, FILE_MENU_OPEN, L"Open");
    AppendMenu(hFileMenu, MF_SEPARATOR, NULL, NULL);
    AppendMenu(hFileMenu, MF_STRING, FILE_MENU_EXIT, L"Exit");

    AppendMenu(hMenu, MF_POPUP, (UINT_PTR)hFileMenu, L"File");
    AppendMenu(hMenu, MF_STRING, 2, L"Help");

    SetMenu(hWnd, hMenu);
}

void AddControls(HWND hWnd)
{
    CreateWindowW(L"static", L"Username: ", WS_VISIBLE | WS_CHILD, 200, 100, 100, 50, hWnd, NULL, NULL, NULL);
    // This is where the problem is... (When I remove the second parameter and put L"Enter Username") the program does not run at all.
    CreateWindowW(L"edit", NULL, WS_VISIBLE | WS_CHILD, 0, 0, 100, 50, hWnd, NULL, NULL, NULL);
}
1 Answers

When you use else if (FILE_MENU_EXIT), this result is always true. You can try:

case WM_COMMAND:
    if (wp == 1)
    {
        MessageBeep(MB_OK);
    }
    else if (wp == FILE_MENU_EXIT)
    {
        DestroyWindow(hWnd);
    }
    break;

Also you can use:

case WM_COMMAND:
    switch (wp)
    {
    case FILE_MENU_NEW:
        MessageBeep(MB_OK);
        break;
    case FILE_MENU_EXIT:
        DestroyWindow(hWnd);
        break;
    }
    break;
Related