如何使 window 在 SFML 中始终位于最前面?

How to make window always on top in SFML?

我正在尝试使用 window 始终保持在最前面的 SFML 制作一个程序。我如何使用 SFML 实现这一目标?我四处寻找,但无济于事。

"Program" 是屏幕中间的一个小红点,它会模仿十字准线,我需要它位于所有内容之上,因为真正的游戏会在后台(游戏没有准星,仅目击)。

我唯一的想法是使用 SFML 的方法 getSystemHandle(),它会给我 OS-特定的 window 句柄。我将 Ubuntu 16.04 与 Gnome 和 X 一起使用,我不太确定在获得句柄后如何编写该功能。

当前版本的 SFML 是不可能的,但由于到目前为止您只需要 X,您可以使用 snippet from this old/rejected pull request.

自己实现它
void WindowImplX11::setTopmost(bool topmost)
{
    static Atom wmStateAbove = XInternAtom(m_display, "_NET_WM_STATE_ABOVE", 1);
    static Atom wmNetWmState = XInternAtom(m_display, "_NET_WM_STATE", 1);

    if (wmStateAbove)
    {
        XClientMessageEvent emsg;
        memset(&emsg, 0, sizeof(emsg));
        emsg.type = ClientMessage;
        emsg.window = m_window;
        emsg.message_type = wmNetWmState;
        emsg.format = 32;
        emsg.data.l[0] = topmost;
        emsg.data.l[1] = wmStateAbove;
        XSendEvent(m_display, RootWindow(m_display, m_screen), false, SubstructureRedirectMask | SubstructureNotifyMask, (XEvent*)&emsg);
    }
}

您必须自己检索 m_displaym_window 等 and/or 将拉取请求重新实现到您的源版本中。