我在C++ 17中使用VisualStudio2022社区(17.1.3)
从原始的OOP API管理菜单是很痛苦的,但是在OOP的帮助下,我已经成功地封装了所有与菜单相关的函数,所以如果我想对菜单中的某个项目做一些事情,我就不需要再关心索引了。
但是,当鼠标光标击中某一项时,VS将抛出以下异常:
wil::ResultException at memory location 0x[Address].
[Rethrow] at memory location 0x0000000000000000.
该应用程序仍然可以运行(它不会崩溃,异常实际上在VS!的输出窗口中),在执行结束时,GetLastError()
返回0。我还可以检索用户使用WM_COMMAND
单击的项目,并且运行良好。
我不知道这是否正常,但我也尝试编译模板项目和我在过去使用Win32 API完成的另一个项目,并且猜怎么着,也会发生同样的情况。因此,我认为这不是一个与我的代码相关的具体问题(尽管我在C++方面没有多少经验,考虑到我开始学习它才两年),但这是IDE本身的一个问题。
main.cpp
// ---------- main.cpp
#include <Windows.h>
#include "resource.h"
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam,
LPARAM lParam);
// Globals
HINSTANCE inst;
HWND mainWnd;
HMENU menuBar;
HMENU subMenu1;
HMENU subMenu2;
int WINAPI wWinMain(HINSTANCE p_hInst, HINSTANCE p_hPrevInst, LPWSTR p_lpszArgs, int p_nCmdShow)
{
inst = p_hInst;
// register the main window class
WNDCLASSEX wcex = { 0 };
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.lpfnWndProc = WindowProc;
wcex.hInstance = inst;
wcex.lpszClassName = L"Menus";
if (!RegisterClassExW(&wcex))
{
// handle error
}
mainWnd = CreateWindowExW(NULL, L"Menus", L"Using menus",
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 500, 500, NULL, NULL, inst, NULL);
if (NULL == mainWnd)
{
// Handle error
}
ShowWindow(mainWnd, p_nCmdShow);
UpdateWindow(mainWnd);
// Create a message structure and start the main loop
MSG message;
while (GetMessage(&message, NULL, NULL, NULL))
{
TranslateMessage(&message);
DispatchMessage(&message);
}
return (int)message.wParam;
}
LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_CREATE:
// Create and set the menu
menuBar = CreateMenu();
subMenu1 = CreatePopupMenu();
subMenu2 = CreatePopupMenu();
AppendMenu(subMenu1, MF_STRING, ID_ITM1, L"Item 01");
AppendMenu(subMenu1, MF_STRING, ID_ITM2, L"Item 02");
AppendMenu(subMenu1, MF_SEPARATOR, NULL, NULL);
AppendMenu(subMenu1, MF_STRING, ID_ITMCLOSE, L"Close");
AppendMenu(subMenu2, MF_STRING, ID_ITMABOUT, L"About...");
AppendMenu(menuBar, MF_POPUP, (UINT_PTR)subMenu1, L"Menu");
AppendMenu(menuBar, MF_POPUP, (UINT_PTR)subMenu2, L"Help");
SetMenu(hWnd, menuBar);
break;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case ID_ITMABOUT:
MessageBox(mainWnd, L"Using menus -- Just an example for Stack Overflow", L"About...", MB_OK);
}
break;
case WM_DESTROY:
PostQuitMessage(GetLastError());
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return 0;
}
Resourcee.h
#pragma once
#define ID_ITM1 5000
#define ID_ITM2 5100
#define ID_ITM3 5200
#define ID_ITM4 5300
#define ID_ITMCLOSE 5400
#define ID_ITMABOUT 5500
/* -------------------------------------------------------------
(Don't forget to add an empty line to avoid the "unexpected
end of line error of VS if you don't use the integrated resource
editor.")
------------------------------------------------------------- */
此示例包含创建空窗口并为其分配菜单的最低限度代码。
如果您高亮显示其中一项或单击“关于”项,并查看VS的输出窗口,异常将开始抛出而没有任何原因,但应用程序将继续运行良好。请注意,这需要一些时间,耐心点。我的输出窗口如下所示。
编辑:
发布于 2022-04-30 09:29:33
VS2022中的菜单确实存在问题(17.1.4,std/C++17标志)。当我编译上面的代码(它不包含任何bug)时,它会将wil::ResultException
抛到我的输出窗口中,但是程序本身仍然运行良好。
https://stackoverflow.com/questions/72060936
复制相似问题