在WinMain中可以将主窗口过程作为lambda吗? [英] can I have main window procedure as a lambda in WinMain?
问题描述
我有一个带有声明的主窗口回调过程的简单窗口应用程序:
I have a simple window application with declared main window callback procedure:
WNDCLASSEXW wcx;
/* ... */
wcx.lpfnWndProc = MainWndProc;
,并且在WinMain
之后我声明了LRESULT CALLBACK MainWndProc(HWND mainWindow, UINT msg, WPARAM wparam, LPARAM lparam) { /* ... */}
并且一切正常,但是我想知道是否可以将此MainWndProc
作为lambda 内置在 WinMain中?
and after the WinMain
I declared LRESULT CALLBACK MainWndProc(HWND mainWindow, UINT msg, WPARAM wparam, LPARAM lparam) { /* ... */}
and all is working ok, but I wonder is it possible to have this MainWndProc
as a lambda inside WinMain ?
推荐答案
您可以使用lambda,只要它没有捕获,然后就隐式转换为函数指针:
You can use a lambda, provided it has no captures then it has an implicit conversion to function pointer:
#include <iostream>
typedef void (*func)();
static func some_func;
int global;
int main() {
some_func = [](){ std::cout << "Hello\n"; }; // Fine
some_func();
int local;
some_func = [&](){ local = 1; }; // Illegal - No conversion
some_func = [](){ global = 1; }; // Fine
}
问题实际上是,您可以在lambda中做多少事来作为没有捕获的回调.您仍然可以使用全局变量",就像使用常规函数作为回调一样.
The problem really is how much you can usefully do in a lambda as a callback without captures. You can still resort to "globals", in the same way you might with a regular function as the callback.
这篇关于在WinMain中可以将主窗口过程作为lambda吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!