我目前正在使用SDL2 + windows.h库在Visual C / C ++中开发GameBoy / GameBoyColor模拟器,以提供其他功能。我已经成功地将菜单项添加到了我的SDL创建的窗口中,但是在单击菜单项时尝试处理由窗口发送的消息时遇到麻烦... Image of running application with window menu items
在我的设计中,我决定创建一个单独的源文件,该文件的名称空间包含各种程序实用程序,例如窗口管理功能。因此,我创建了一个名为getMenuEvent()
的函数,该函数包含窗口实例的消息循环,从技术上讲,该函数应在同一命名空间中调用我定义的WndProc()
函数,以查看{{1 }}是...
这是我写的实用程序名称空间源文件
wParam
这是我的主要爱好:
HWND getSDLWinHandle(SDL_Window* win)
{
SDL_SysWMinfo infoWindow;
SDL_VERSION(&infoWindow.version);
if (!SDL_GetWindowWMInfo(win, &infoWindow))
{
std::cout << "test";
return NULL;
}
return (infoWindow.info.win.window);
}
//Initializes the native windows drop down menu elements of the window
void ActivateMenu(HWND windowRef)
{
hMenuBar = CreateMenu();
hFile = CreateMenu();
hEdit = CreateMenu();
hHelp = CreateMenu();
AppendMenu(hMenuBar, MF_POPUP, (UINT_PTR)hFile, "File");
AppendMenu(hMenuBar, MF_POPUP, (UINT_PTR)hEdit, "Edit");
AppendMenu(hMenuBar, MF_POPUP, (UINT_PTR)hHelp, "Help");
AppendMenu(hFile, MF_STRING, ID_LOADROM, "Load ROM");
AppendMenu(hFile, MF_STRING, ID_EXIT, "Exit");
AppendMenu(hEdit, MF_STRING, ID_CONTROLS, "Configure Controls");
AppendMenu(hHelp, MF_STRING, ID_ABOUT, "About");
SetMenu(windowRef, hMenuBar);
}
void getMenuEvent(MSG* message, HWND hwnd)
{
if (GetMessage(message, hwnd, 0, 0))
{
TranslateMessage(message);
DispatchMessage(message);
}
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wparam, LPARAM lparam)
{
std::cout << "test" << std::endl;
switch (message)
{
case WM_COMMAND:
if (LOWORD(wparam) == ID_EXIT)
{
exit(0);
}
break;
}
}
此刻,菜单栏会弹出并显示子菜单项(例如File-> Load ROM),但是当我单击子菜单项(如Exit)时什么也没有发生。 我如何让我的程序调用在实用程序名称空间中定义的WndProc()函数,以便处理来自菜单项单击的消息?我的方法可以吗?
P.S。我从多个在线资源中了解到DispatchMessage()函数会自动调用WndProc()函数,但就我而言似乎没有发生。
答案 0 :(得分:3)
找到了一个简单的修复程序,它也大量清理了代码!
结果是,您可以使用SDL的事件处理系统来检索winAPI消息。
这是已编辑的主要功能:
const char* title = "GBemu";
bool isRunning = true;
SDL_Event mainEvent;
HWND windowHandler;
//Initialize the SDL Subsystems
if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
{
SDL_Log("Unable to initialize SDL subsystems: %s", SDL_GetError());
return 1;
}
//Create a window pointer + allocate a window object to it.
SDL_Window* mainWindow = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1000, 1000, NULL);
windowHandler = utilities::getSDLWinHandle(mainWindow);
//Activate the menu bar of the window
utilities::ActivateMenu(windowHandler);
//Initialize Rendering Device
SDL_Renderer* mainRenderer = SDL_CreateRenderer(mainWindow, -1, 0);
SDL_SetRenderDrawColor(mainRenderer, 0, 0, 0, 255);
SDL_RenderPresent(mainRenderer);
//Enable WinAPI Events Processing
SDL_EventState(SDL_SYSWMEVENT, SDL_ENABLE);
while (isRunning)
{
SDL_PollEvent(&mainEvent);
switch (mainEvent.type)
{
case SDL_WINDOWEVENT_CLOSE:
mainEvent.type = SDL_QUIT;
SDL_PushEvent(&mainEvent);
break;
case SDL_SYSWMEVENT:
if (mainEvent.syswm.msg->msg.win.msg == WM_COMMAND)
{
if (LOWORD(mainEvent.syswm.msg->msg.win.wParam) == ID_EXIT)
{
isRunning = false;
}
}
break;
case SDL_QUIT:
isRunning = false;
break;
};
}
return 0;
这是编辑的名称空间实用程序源文件:
//Namespace variables/Defines
#define ID_LOADROM 1
#define ID_ABOUT 2
#define ID_EXIT 3
#define ID_CONTROLS 4
static HMENU hHelp;
static HMENU hEdit;
static HMENU hFile;
static HMENU hMenuBar;
//Function which retrieves the address/Handle of an SDL window
//Also retrieves the specific subsystem used by SDL to create that window which is platform specific (Windows, MAC OS x, IOS, etc...)
HWND getSDLWinHandle(SDL_Window* win)
{
SDL_SysWMinfo infoWindow;
SDL_VERSION(&infoWindow.version);
if (!SDL_GetWindowWMInfo(win, &infoWindow))
{
return NULL;
}
return (infoWindow.info.win.window);
}
//Initializes the native windows drop down menu elements of the window
void ActivateMenu(HWND windowRef)
{
hMenuBar = CreateMenu();
hFile = CreateMenu();
hEdit = CreateMenu();
hHelp = CreateMenu();
AppendMenu(hMenuBar, MF_POPUP, (UINT_PTR)hFile, "File");
AppendMenu(hMenuBar, MF_POPUP, (UINT_PTR)hEdit, "Edit");
AppendMenu(hMenuBar, MF_POPUP, (UINT_PTR)hHelp, "Help");
AppendMenu(hFile, MF_STRING, ID_LOADROM, "Load ROM");
AppendMenu(hFile, MF_STRING, ID_EXIT, "Exit");
AppendMenu(hEdit, MF_STRING, ID_CONTROLS, "Configure Controls");
AppendMenu(hHelp, MF_STRING, ID_ABOUT, "About");
SetMenu(windowRef, hMenuBar);
}
为了使SDL检索这些消息,要包括两个重要步骤:
必须在事件检查之前执行对SDL_EventState()的调用。这是因为默认情况下,SDL在检查事件时会忽略本机API消息,以提高可移植性。
对SDL创建的窗口的句柄的引用必须在代码中的某个地方方便使用,以便向其中添加WinAPI菜单。可以通过调用SDL_GetWindowWMInfo()函数轻松地完成此操作,该函数将返回一个具有HWND窗口句柄数据成员的SDL_SysWMInfo对象。
使用此选项,当用户单击“文件”菜单中的“退出”选项时,程序成功退出。
希望这会帮助那些打算做类似事情的人!