首页 > 解决方案 > 为什么单击编辑框会关闭 win32 窗口(C++)?

问题描述

下面的代码应该创建一个带有菜单、静态文本和编辑框的简单窗口,但是当我尝试创建一个编辑框时,它要么根本不运行(没有错误),要么当它运行时我尝试单击在编辑框上,窗口关闭。我真的被困住了,任何帮助将不胜感激!旁注:我是 winapi/gui 的 c++ 初学者,所以请尊重。

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

标签: c++winapi

解决方案


当您使用 时else if (FILE_MENU_EXIT),此结果始终为真。你可以试试:

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

您也可以使用:

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

推荐阅读