c++ - Win32 API CreateWindowW 与菜单创建冲突

标签 c++ winapi windows-10-desktop

我正在尝试将一些应用程序控件添加到我的 Windows API 代码中(注意:我使用的是 Visual Studio)我遇到了一个问题,我尝试添加一个 CreateWindowW() 函数来生成文本和一个字段(“静态”和“编辑”)和一个菜单。菜单本身可以正常工作(“计算”):

Without the CreateWindowW() function

但是,添加 CreateWindow() 函数会完全“删除”菜单,但会产生 CreateWindowW() 输出(也有点闪烁):

With the CreateWindowW() function

我现在的代码是这样的(菜单函数和 CreateWindowW() 函数在底部):

#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
//
//
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:
        {
            WinControls(hWnd);
            WinMenu(hWnd);
            break;
        }/*
    case WM_PAINT:
        {
            PAINTSTRUCT ps;
            HDC hdc = BeginPaint(hWnd, &ps);
            // TODO: Add any drawing code that uses hdc here...
            TextOut(hdc, 10, 10, rekt, _tcslen(rekt));
            TextOut(hdc, 10, 40, reverse, _tcslen(reverse));
            EndPaint(hWnd, &ps);
            break;
        }*/
    case WM_DESTROY:
    {
        PostQuitMessage(0);
        break;
    }
    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}

void WinMenu(HWND hWnd) {

    hMenu = CreateMenu();
    HMENU hFileMenu = CreateMenu();
    HMENU hSubMenu = CreateMenu();
    HMENU hSubMenu2 = CreateMenu();

    //XP Calculations
    AppendMenuW(hSubMenu, MF_STRING, NULL, L"Rank -> XP");
    AppendMenuW(hSubMenu, MF_STRING, NULL, L"XP -> Rank");

    //Credit Calculations
    AppendMenuW(hSubMenu2, MF_STRING, NULL, L"Rank -> Cred");
    AppendMenuW(hSubMenu2, MF_STRING, NULL, L"Cred -> Rank");



    AppendMenuW(hFileMenu, MF_POPUP, (UINT_PTR)hSubMenu, L"Points"); //option that popups submenu of hSubMenu
    AppendMenuW(hFileMenu, MF_POPUP, (UINT_PTR)hSubMenu2, L"Credits"); //option that popups submenu of hSubMenu
    AppendMenuW(hFileMenu, MF_SEPARATOR, NULL, NULL); // separator
    AppendMenuW(hFileMenu, MF_STRING, FILE_MENU_ABOUT, L"About");
    AppendMenuW(hFileMenu, MF_STRING, FILE_MENU_DESTROY, L"Exit"); // option 

    AppendMenuW(hMenu, MF_POPUP, (UINT_PTR)hFileMenu, L"Calculations"); //popups up submenu of hFileMenu

    SetMenu(hWnd, hMenu);
}

void WinControls(HWND hWnd) {
    CreateWindowW(L"Static", L"Enter text here: ", WS_VISIBLE | WS_CHILD | WS_BORDER | SS_CENTER, 400, 100, 100, 50, hWnd, NULL, NULL, NULL);
    CreateWindowW(L"Edit", L"...", WS_VISIBLE | WS_CHILD, 400, 155, 100, 50, hWnd, NULL, NULL, NULL);
}

请注意,该设置是该程序的 Visual Studio 基本设置。
任何帮助表示赞赏!

最佳答案

我做了一些调试,发现是 CreateWindowW()我用于静态和编辑控件与窗口属性的定义冲突。

由于窗口是由 WNDCLASSEXW wcex 定义的,我只需要更改 CreateWindowW()CreateWindowExW() .

唯一的新问题是静态控件(和其他控件)可以工作,但编辑控件不能工作,这会杀死菜单。

关于c++ - Win32 API CreateWindowW 与菜单创建冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60909835/

相关文章:

c++ - 在 C++ 中将图像读入像素矩阵

visual-c++ - 为什么没有定义 PCTSTR 但定义了 LPCTSTR?

c++ - 形状中的 Flash "CurveTo"和 "LineTo"

c++ - 自定义 QDialog 在 exec() 时导致段错误

c++ - TextBox 设 '\n' 为回车

win-universal-app - 桌面应用转换器 "Fatal Error 1000"

sql-server - 如何授予 MSSQL$SQL2016 写入文件夹的权限

c# - Winforms - C# 任务栏进度指示器 (Windows 10)

c++ - 覆盖默认标题搜索路径

c++ - Qt ActiveX 动态调用 : bad parameter count