本文介绍了获取 SDL 2 应用程序的窗口句柄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取 SDL2 窗口的句柄,以便与 WinApi 一起使用.

I would like to get the handle of a SDL2 window, to use it with WinApi.

我使用以下代码检索该句柄:

I retrieve that handle with the following code :

/* All the SDL initalisation... */
SDL_Window* window = SDL_CreateWindow("My Window", SDL_WINDOWPOS_UNDEFINED,
                        SDL_WINDOWPOS_UNDEFINED, RESX, RESY, SDL_WINDOW_SHOWN);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (window == NULL || renderer == NULL) {
    MessageBox(NULL, L"SDL initialisation error", NULL, MB_OK);
    exit(-1);
}

SDL_SysWMinfo wmInfo;
SDL_GetWindowWMInfo(window, &wmInfo);
HWND hwnd = wmInfo.info.win.window;

但此时hwnd地址为0xcccccccc(未使用).

But at this point, hwnd adress is 0xcccccccc (unused).

我做错了吗?

推荐答案

SDL Wiki 页面 备注部分说 info.version 必须在使用前初始化.代码示例建议在查询 WM 信息之前使用 SDL_VERSION(&info.version);.

SDL Wiki page in remarks section says that info.version must be initialised before usage. Code example suggests using SDL_VERSION(&info.version); before querying WM info.

SDL_SysWMinfo wmInfo;
SDL_VERSION(&wmInfo.version);
SDL_GetWindowWMInfo(window, &wmInfo);
HWND hwnd = wmInfo.info.win.window;

这篇关于获取 SDL 2 应用程序的窗口句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 08:52