繁体   English   中英

添加编辑控件后不出现 WinAPI 菜单

[英]WinAPI menu doesn't appear after adding edit control

我正在学习如何将 WinAPI 与 C++ 一起使用,并且正在创建一个应用程序,该应用程序目前只有一个基本的编辑控件供某人输入其用户名。

void AddMenus(HWND hWnd)
{
    hMenu = CreateMenu();
    hSubMenu = CreateMenu();

    AppendMenu(hMenu, MF_POPUP, (UINT_PTR)hSubMenu, L"File");

    AppendMenu(hSubMenu, MF_STRING, FILE_MENU_EXIT, L"Exit");

    SetMenu(hWnd, hMenu);
}

void AddControls(HWND hWnd)
{
    hUNameS = CreateWindowW(L"static", L"Username: ", WS_VISIBLE | WS_CHILD | ES_CENTER,
        100, 50, 100, 25, hWnd, NULL, NULL, NULL);
    hUName = CreateWindowW(L"edit", L" ", WS_VISIBLE | WS_CHILD | WS_BORDER,
        202, 50, 100, 25, hWnd, NULL, NULL, NULL);
}

使用以下代码块创建窗口时,控件和菜单都不会出现。

case WM_CREATE:
        AddControls(hWnd);
        AddMenus(hWnd);
        break;

如果我注释掉 AddControls,则菜单显示正常,但如果我将它们都保留为如图所示,则菜单不会出现。 如果我交换函数调用的顺序,则没有任何变化。

如果我将编辑控件的第二个参数设置为 NULL,菜单和控件都会出现,但是我遇到了一个不同的问题,我在控件中键入的任何文本在我单击它之前都是不可见的,然后在我继续时再次消失输入。 我发现解决此问题的唯一方法是使用占位符,然后删除菜单。

TLDR:我无法同时显示菜单和控件,并且如果没有设置编辑控件的第二个参数,则文本开始时是不可见的。

你的第一个问题的代码:

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_COMMAND:
    {
        int wmId = LOWORD(wParam);
        // Parse the menu selections:
        switch (wmId)
        {
        case FILE_MENU_DESTROY:
        {
            DestroyWindow(hWnd);
            break;
        }
        case FILE_MENU_ABOUT:
        {
            MessageBox(hWnd, L"about", L"About", MB_OK);
            break;
        }
        break; //this break will not break outside case WM_COMMAND, but only break outside the switch (wmId).
        }
    }
    case WM_CREATE:
        AddControls(hWnd);
        AddMenus(hWnd);
        break;
    ...
}

需要移动break; WM_CREATE之上打破WM_COMMAND情况。 否则,当您创建窗口并接收WM_COMMANDWinMenu (在这种情况下为AddMenus )将被执行多次。 我可以用这段代码重现。

修改为:

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_COMMAND:
    {
        int wmId = LOWORD(wParam);
        // Parse the menu selections:
        switch (wmId)
        {
        case FILE_MENU_DESTROY:
        {
            DestroyWindow(hWnd);
            break;
        }
        case FILE_MENU_ABOUT:
        {
            MessageBox(hWnd, L"about", L"About", MB_OK);
            break;
        }

        }
    }
    break;
    case WM_CREATE:
        AddControls(hWnd);
        AddMenus(hWnd);
        break;
    ...
}

这对我和整个项目都有效:

#define MAX_LOADSTRING 100
#define FILE_MENU_DESTROY 1
#define FILE_MENU_ABOUT 2

// Global Variables:
HINSTANCE hInst;                                // current instance
WCHAR szTitle[MAX_LOADSTRING];                  // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING];            // the main window class name

// Forward declarations of functions included in this code module:
ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
//INT_PTR CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

//Custom Application Function Forwrd Declarations
void WinControls(HWND);
void WinMenu(HWND);
HMENU hMenu;

int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
    _In_opt_ HINSTANCE hPrevInstance,
    _In_ LPWSTR    lpCmdLine,
    _In_ int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // TODO: Place code here.

    // Initialize global strings
    LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
    LoadStringW(hInstance, IDC_RANKTOOLADVANCED, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance); //Generates an instance of a window class

    // Perform application initialization:
    if (!InitInstance(hInstance, nCmdShow))
    {
        return FALSE;
    }
    HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_RANKTOOLADVANCED));

    MSG msg;

    // Main message loop:
    while (GetMessage(&msg, nullptr, 0, 0))
    {
        if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
    }

    return (int)msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
ATOM MyRegisterClass(HINSTANCE hInstance)//Class properties of the window
{
    WNDCLASSEXW wcex;

    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc = WndProc; //assigns a window to the instance of the class
    wcex.cbClsExtra = 0;
    wcex.cbWndExtra = 0;
    wcex.hInstance = hInstance; //creates an instance
    wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_RANKTOOLADVANCED));
    wcex.hCursor = LoadCursor(nullptr, IDC_ARROW); //defines a type of cursor (arrow, help, cross, etc.)
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_RANKTOOLADVANCED);
    wcex.lpszClassName = szWindowClass;
    wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

    return RegisterClassExW(&wcex);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{//initializes instance of window class (invocation)
    hInst = hInstance; // Store instance handle in our global variable

    HWND hWnd = CreateWindowW(szWindowClass, L"test", WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, 0, 900, 600, nullptr, nullptr, hInstance, nullptr);

    if (!hWnd)
    {
        return FALSE;
    }

    ShowWindow(hWnd, nCmdShow);
    UpdateWindow(hWnd);

    return TRUE;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE: Processes messages for the main window.
//
//  WM_COMMAND  - process the application menu
//  WM_PAINT    - Paint the main window
//  WM_DESTROY  - post a quit message and return
//
//
void AddMenus(HWND hWnd)
{
    hMenu = CreateMenu();
    HMENU hSubMenu = CreateMenu();

    AppendMenu(hMenu, MF_POPUP, (UINT_PTR)hSubMenu, L"File");

    AppendMenu(hSubMenu, MF_STRING, FILE_MENU_DESTROY, L"Exit");

    SetMenu(hWnd, hMenu);
}

void AddControls(HWND hWnd)
{
    HWND hUNameS = CreateWindowW(L"static", L"Username: ", WS_VISIBLE | WS_CHILD | ES_CENTER,
        100, 50, 100, 25, hWnd, NULL, NULL, NULL);
    HWND hUName = CreateWindowW(L"edit", L" ", WS_VISIBLE | WS_CHILD | WS_BORDER,
        202, 50, 100, 25, hWnd, NULL, NULL, NULL);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
    case WM_COMMAND:
    {
        int wmId = LOWORD(wParam);
        // Parse the menu selections:
        switch (wmId)
        {/*
        case IDM_ABOUT:
            DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
            break;
        case IDM_EXIT:
            DestroyWindow(hWnd);
            break;
        default:
            return DefWindowProc(hWnd, message, wParam, lParam);
        }
        case FILE_MENU_NEW:
        {
            MessageBox(hWnd, L"task failed successfully", L"you done goofed", MB_OK | MB_ICONEXCLAMATION);
            break;
        }*/
        case FILE_MENU_DESTROY:
        {
            DestroyWindow(hWnd);
            break;
        }
        case FILE_MENU_ABOUT:
        {
            MessageBox(hWnd, L"about", L"About", MB_OK);
            break;
        }
        }
    }
    break;
    case WM_CREATE:
        AddControls(hWnd);
        AddMenus(hWnd);
        break;
    case WM_DESTROY:
    {
        PostQuitMessage(0);
        break;
    }
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM