Windows 消息过程的函数指针数组

Array of function pointers for the Windows Messages Procedure

本文关键字:指针 数组 函数 消息 过程 Windows      更新时间:2023-10-16

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 中将指针传递给您的数组。