将 glfw 窗口连接到电子手柄会冻结电子窗口

发布于 2025-01-12 11:32:52 字数 1651 浏览 4 评论 0 原文

我正在尝试将 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);
}

所以我得到的“错误”行为如下:

  1. 电子应用程序冻结。没有任何移动,进程“没有响应”
  2. 我的 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 :

  1. The electron app freezes. Nothing moves, the process is "not responding"
  2. 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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我太投入 2025-01-19 11:32:52

所以看来这不是一个很好的主意哈哈+我对win32 api不太擅长所以我不能真正说出什么不起作用(参见@iinspectable评论)

我在电子的github上深入研究了这个问题:https://github.com/electron/electron/issues/10547 并找到了一种在 Electron 窗口顶部显示 OpenGL 框架的方法。
我通过将窗口创建为电子应用程序的子进程并使用 HWND 作为启动参数来规避这个问题:

const { app, BrowserWindow } = require('electron')
const { endianness } = require('os')
const { spawn } = require('child_process')

const nativeExecutablePath = 'src/overlay/build/Release/kf-overlay.exe'
const indexPath = 'src/ui/html/index.html'

const startChildProcess = (hwnd, target) => {
    const data = endianness() == 'LE'
        ? hwnd.readInt32LE()
        : hwnd.readInt32BE()

    const p = spawn(target, [data], { cwd: process.cwd() })
    p.stdout.on('data', data => console.log(`[native] ${data}`))
    p.stderr.on('data', data => console.log(`[native] ${data}`))
}

const createWindow = () => {
    const win = new BrowserWindow({
        width: 800,
        height: 600
    })
    // win.loadFile(indexPath)
    win.loadURL('https://www.google.com')
    win.webContents.setFrameRate(60)

    console.log(`Starting child process ${nativeExecutablePath}`)
    const hwnd = win.getNativeWindowHandle()
    startChildProcess(hwnd, nativeExecutablePath)
}

app.whenReady().then(() => {
    createWindow()
})

然后从 C++ 端将窗口创建为子进程:

HWND createChildWindow(HWND parent)
{
    RECT rect;
    GetWindowRect(parent, &rect);
    int parentWidth = rect.right - rect.left;
    int parentHeight = rect.bottom - rect.top;

    DWORD style = WS_VISIBLE | WS_CHILD | WS_POPUP;
    auto child = CreateWindowEx(
        WS_EX_LAYERED | WS_EX_TOPMOST | WS_EX_TRANSPARENT | WS_EX_COMPOSITED,
        WINDOW_CLASS_NAME,
        nullptr,
        style,
        parentWidth / 2 - 512,
        parentHeight / 2 - 512,
        512,
        512,
        parent,
        nullptr,
        nullptr,
        nullptr
    );
    SetParent(child, parent);
    return child;
} 

最后我可以通过 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 :

const { app, BrowserWindow } = require('electron')
const { endianness } = require('os')
const { spawn } = require('child_process')

const nativeExecutablePath = 'src/overlay/build/Release/kf-overlay.exe'
const indexPath = 'src/ui/html/index.html'

const startChildProcess = (hwnd, target) => {
    const data = endianness() == 'LE'
        ? hwnd.readInt32LE()
        : hwnd.readInt32BE()

    const p = spawn(target, [data], { cwd: process.cwd() })
    p.stdout.on('data', data => console.log(`[native] ${data}`))
    p.stderr.on('data', data => console.log(`[native] ${data}`))
}

const createWindow = () => {
    const win = new BrowserWindow({
        width: 800,
        height: 600
    })
    // win.loadFile(indexPath)
    win.loadURL('https://www.google.com')
    win.webContents.setFrameRate(60)

    console.log(`Starting child process ${nativeExecutablePath}`)
    const hwnd = win.getNativeWindowHandle()
    startChildProcess(hwnd, nativeExecutablePath)
}

app.whenReady().then(() => {
    createWindow()
})

Then creating the window as a child from C++ side :

HWND createChildWindow(HWND parent)
{
    RECT rect;
    GetWindowRect(parent, &rect);
    int parentWidth = rect.right - rect.left;
    int parentHeight = rect.bottom - rect.top;

    DWORD style = WS_VISIBLE | WS_CHILD | WS_POPUP;
    auto child = CreateWindowEx(
        WS_EX_LAYERED | WS_EX_TOPMOST | WS_EX_TRANSPARENT | WS_EX_COMPOSITED,
        WINDOW_CLASS_NAME,
        nullptr,
        style,
        parentWidth / 2 - 512,
        parentHeight / 2 - 512,
        512,
        512,
        parent,
        nullptr,
        nullptr,
        nullptr
    );
    SetParent(child, parent);
    return child;
} 

Finally I could create the OpenGL context through wglCreateContext calls, and render a frame with OpenGL calls :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文