Windows 消息过程的函数指针数组
Array of function pointers for the Windows Messages Procedure
everyone(大家)我想尝试一些东西,我需要一点帮助。我想做的是创建一些函数,将它们的指针存储到数组中,然后在 Windows 消息过程中调用它们。例如:
int create_functions[10];
int paint_functions[10];
int func1() {
// code
}
void func2(int arg1) {
// code
}
create_functions[0] = *func1; // add pointer of func1 to the first array
create_functions[1] = *func2; // add pointer of func2 to the second array
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
switch (message) {
case WM_CREATE:
// execute create_functions[0] - How do I do it?
case WM_PAINT:
// execute paint_functions[0] - How do I do it?
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
break;
}
return 0;
}
我知道有人会问:为什么不直接执行func1/func2。因为我将决定在运行时执行哪些函数。感谢大家的帮助!
编辑:回调函数呢?我不太明白如何使用它们?
如果问题是:我如何做函数指针,我建议你看看关于这个主题的文章,比如 http://www.newty.de/fpt/index.html
如果这不是您的问题,请您编辑并添加更多详细信息。
int create_functions[10];
int paint_functions[10];
应该是
void (*create_functions[10])(void);
void (*create_functions[10])(int);
和
// execute create_functions[0] - How do I do it?
// execute paint_functions[0] - How do I do it?
应该是
create_functions[0]();
paint_functions[0](some_integer_here);
也
create_functions[0] = *func1; // add pointer of func1 to the first array
create_functions[1] = *func2; // add pointer of func2 to the second array
应该是
create_functions[0] = func1; // add pointer of func1 to the first array
create_functions[1] = func2; // add pointer of func2 to the second array
或
create_functions[0] = &func1; // add pointer of func1 to the first array
create_functions[1] = &func2; // add pointer of func2 to the second array
根据您的口味或心情。
您可以在 wParam 中将指针传递给您的数组。
相关文章:
- 指向指向字符数组的指针数组的指针
- 通过指向指针数组的指针访问子类的属性
- C++,指针数组,指向双链表中的条目
- 在C++中,如何初始化指向wchar_t*的指针数组(生成wchar_t**)
- C++从函数指针数组调用函数
- 关于指向指针数组的指针
- 将返回值存储在函数指针数组的指针中是如何工作的?
- 将链表转换为指针数组时出错
- C++ 对象指针数组的复制构造函数
- C++ - 循环访问指针数组会导致错误
- 删除指针数组 (C++) 中的元素
- 如何循环访问 cpp 中的函数返回的字符指针数组
- 将函数指针数组中的函数指针作为模板参数传递
- Google Or-Tools Glop:如何创建指向 const 对象的指针数组?
- 具有推导参数的模板函数指针数组变量
- 如何模板化堆栈分配的多态指针数组到接口,包括派生类型的相应点?
- C++指针数组到字符数组中的特定位置
- 如何在C++中复制指针数组的数据
- 初始化类中的指针数组,并在另一个类中检索它
- 尽管直接设置了指针数组,但仍为空