gpt4 book ai didi

c++ - 为什么在这个函数中使用 LRESULT CALLBACK?

转载 作者:太空宇宙 更新时间:2023-11-03 10:45:20 26 4
gpt4 key购买 nike

最近,我开始学习 DX11,并尝试在 WinAPI 中创建消息循环。我从教程中看到了一个以前从未见过的 LRESULT CALLBACK 函数。它在窗口过程函数中被调用。这是 WndProc 函数和 MessageHandler 函数(我正在谈论的函数)。

WndProc:

LRESULT CALLBACK WndProc(HWND hwnd, UINT umessage, WPARAM wparam, LPARAM lparam)
{
switch(umessage)
{
// Check if the window is being destroyed.
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}

// Check if the window is being closed.
case WM_CLOSE:
{
PostQuitMessage(0);
return 0;
}

// All other messages pass to the message handler in the system class.
default:
{
return ApplicationHandle->MessageHandler(hwnd, umessage, wparam, lparam);
}
}
}

消息处理程序:

LRESULT CALLBACK SystemClass::MessageHandler(HWND hwnd, UINT umsg, WPARAM wparam, LPARAM lparam)
{
switch(umsg)
{
// Check if a key has been pressed on the keyboard.
case WM_KEYDOWN:
{
// If a key is pressed send it to the input object so it can record that state.
m_Input->KeyDown((unsigned int)wparam);
return 0;
}

// Check if a key has been released on the keyboard.
case WM_KEYUP:
{
// If a key is released then send it to the input object so it can unset the state for that key.
m_Input->KeyUp((unsigned int)wparam);
return 0;
}

// Any other messages send to the default message handler as our application won't make use of them.
default:
{
return DefWindowProc(hwnd, umsg, wparam, lparam);
}
}
}

我不明白的是,为什么我们要在 MessageHandler 函数中添加“LRESULT CALLBACK”部分?我知道,我们必须将它添加到 WndProc 函数中,但我不明白创建新函数并将其添加为调用约定的意义。如果我们不向 MessageHandler 函数添加任何调用约定会怎样?如果我们不创建 MessageHandler 函数并将 KEY_DOWN 监听器写入 WndProc 的 switch-case 语句会怎样?

这些代码在一个类中,ApplicationHandler 指针指向“this”。

最佳答案

没有明显的理由将 SystemClass::MessageHandler 声明为 CALLBACK,因为它不能用作 Windows 的消息处理程序,因为它不是 静态。在您显示的代码中,没有理由将 SystemClass::MessageHandler 声明为 CALLBACK

关于c++ - 为什么在这个函数中使用 LRESULT CALLBACK?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23503560/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com