我正在尝试将 glfw 窗口附加到电子窗口上。
我已在 glfw 应用程序中成功检索了电子窗口句柄 (electronWindow.getNativeWindowHandle()
),然后使用 win32 api 尝试将它们相互附加:
GLFWwindow* createWindow(HWND parentWindow)
{
// Create an invisible window
fprintf(stdout, "Creating window as child of window 0x%016x\n", parentWindow);
glfwDefaultWindowHints();
glfwWindowHint(GLFW_RESIZABLE, FALSE);
glfwWindowHint(GLFW_DECORATED, GLFW_FALSE);
glfwWindowHint(GLFW_VISIBLE, GLFW_TRUE);
GLFWwindow* window = glfwCreateWindow(800, 600, "", nullptr, nullptr);
// That we attach as a child to an other one
HWND hw = glfwGetWin32Window(window);
HWND previousParent = SetParent(hw, parentWindow);
if (!previousParent) {
fprintf(stderr, "Couldn't set window parent: %s\n", lastWindowsError().c_str());
glfwDestroyWindow(window);
return nullptr;
}
ShowWindow(hw, SW_SHOW);
return window;
}
SetParent 调用不会失败,因此我假设连接已建立。
然后我有我的(非常经典的)主 glfw 循环,如下所示:
while (!glfwWindowShouldClose(window))
{
glClearColor(0.3, 0.4, 0.8, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glfwPollEvents();
glfwSwapBuffers(window);
}
所以我得到的“错误”行为如下:
- 电子应用程序冻结。没有任何移动,进程“没有响应”
- 我的 glfw 窗口没有显示在任何地方
我没有在 glfw 端进行 win32 事件处理,这可能是问题所在吗?比如我必须处理一些特定事件?
这是电子应用程序不将事件传递到 glfw 窗口从而阻止一切的问题吗?
注意:我这样做是因为我想使用我们已有的引擎来显示本机 opengl。
每次电子窗口移动时,我都天真地尝试过 glfwSetWindowPos
,但它非常缓慢(可能是 Windows 阻止了这种行为),所以我正在尝试其他方法(此处:将 opengl 窗口作为子窗口附加)电子)。
I'm trying to attach a glfw window to an electron one.
I've succesfully retrieved the electron window handle (electronWindow.getNativeWindowHandle()
) in my glfw app, and then used win32 api to try and attach them to one another :
GLFWwindow* createWindow(HWND parentWindow)
{
// Create an invisible window
fprintf(stdout, "Creating window as child of window 0x%016x\n", parentWindow);
glfwDefaultWindowHints();
glfwWindowHint(GLFW_RESIZABLE, FALSE);
glfwWindowHint(GLFW_DECORATED, GLFW_FALSE);
glfwWindowHint(GLFW_VISIBLE, GLFW_TRUE);
GLFWwindow* window = glfwCreateWindow(800, 600, "", nullptr, nullptr);
// That we attach as a child to an other one
HWND hw = glfwGetWin32Window(window);
HWND previousParent = SetParent(hw, parentWindow);
if (!previousParent) {
fprintf(stderr, "Couldn't set window parent: %s\n", lastWindowsError().c_str());
glfwDestroyWindow(window);
return nullptr;
}
ShowWindow(hw, SW_SHOW);
return window;
}
The SetParent call doesn't fail so I'm assuming the connection is made.
Then I have my (very classical) main glfw loop like this :
while (!glfwWindowShouldClose(window))
{
glClearColor(0.3, 0.4, 0.8, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glfwPollEvents();
glfwSwapBuffers(window);
}
So the "faulty" behavior I've got is the following :
- The electron app freezes. Nothing moves, the process is "not responding"
- My glfw window doesn't show up anywhere
I haven't done an win32 event handling on glfw side, could this be the issue ? Like some specific event I have to take care of ?
Is this an issue with the electron app not passing events to the glfw window, hence blocking everything ?
Note: I'm doing this because I want to display native opengl using a pre-existing engine we've got.
I've tried naively doing glfwSetWindowPos
each time the electron window moves, but it's sluggish as hell (probably Windows preventing this behavior) so I'm trying other approaches (here : attaching the opengl window as a child of electron).
发布评论
评论(1)
所以看来这不是一个很好的主意哈哈+我对win32 api不太擅长所以我不能真正说出什么不起作用(参见@iinspectable评论)
我在电子的github上深入研究了这个问题:https://github.com/electron/electron/issues/10547 并找到了一种在 Electron 窗口顶部显示 OpenGL 框架的方法。
我通过将窗口创建为电子应用程序的子进程并使用 HWND 作为启动参数来规避这个问题:
然后从 C++ 端将窗口创建为子进程:
最后我可以通过
wglCreateContext
创建 OpenGL 上下文调用,并使用 OpenGL 调用渲染帧:)So it seems it's not a very good idea haha + I'm not that good with win32 api so I can't really tell what's not working (cf. @iinspectable comments)
I delved into this issue on electron's github : https://github.com/electron/electron/issues/10547 and found a way to display an OpenGL frame on top of an Electron window.
I circumvented the issue by creating the window as a child process of the electron app with the HWND as startup argument :
Then creating the window as a child from C++ side :
Finally I could create the OpenGL context through
wglCreateContext
calls, and render a frame with OpenGL calls :)