如何使窗口出现在所有内容之上(甚至是全屏游戏!)c++/Qt

2023-11-25

我正在尝试制作一个在屏幕中心显示十字准线并保持在其他所有内容之上的应用程序。 目的是在一些不提供十字准线的 FPS 游戏中添加十字准线。 我已经成功地将我的窗口设置为除了游戏之外的所有内容的最上面:/

这是我的代码:(一切都在主要部分,因为我只测试我的应用程序的核心功能,我对其进行了广泛的评论,以尝试使我的问题更容易理解)

QApplication app(argc, argv);

DWORD error;
QWidget window;
QLabel *label = new QLabel(&window);
label->setText("<strong>O<strong>");//I'm using an "O" as a crosshair until I can figure out how to display image transparency.
window.setGeometry(960-label->width()/2,540-label->height()/2,label->width(),label->height());//here I'm making my window appear in the center of my screen
window.setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);// here making the window frameless and topMost via qt
window.setAttribute(Qt::WA_TranslucentBackground);//making the window see through
window.setWindowTitle("Crosshair");
window.show();

//in this next part I tried using windows api to make my window appear on top of the game.
HWND handle, myHandle;
myHandle = FindWindow(NULL,TEXT("Crosshair"));//retieving my own application window handle
if(myHandle == 0){cout << "no own handle" << endl;}//it successfully retrieves it

handle = FindWindow(NULL,TEXT("Killing Floor"));//retrieving a game window handle
if(handle == 0){cout << "no KF handle" << endl;}//it successfully retrieves it

if(handle != 0 && myHandle != 0)
{
    if(SetWindowPos(handle,myHandle,0,0,0,0,SWP_NOSIZE | SWP_NOMOVE) == 0){cout << "couldnt set notopmost" << endl;}//here SetWindowPos returns 0 (function failed)
}
          // I've also tried using SetWindowPos to set the game to Not TOPMOST, it didnt work either.
          // here was my code : SetWindowPos(handle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);

error = GetLastError();// i tried using GetLastError to understand what was happening
cout << error << endl;// but it only returns "5", I've read that you can look in WINNT.H for information about the meanings of error codes
                      // however its a pretty big file and I wasn't able to understand where the relevant part was.


return app.exec();

我的猜测是游戏等应用程序对显示设备有更直接的控制。 我正在寻找解决此问题的任何解决方案(不一定涉及透明的最上面窗口)。 另外,如果有人可以向我解释如何有效地使用 GetLastError(),以及为什么游戏的行为与普通窗口不同。

提前致谢。


来自设置窗口位置()文档:

可以通过设置将窗口设为最顶层窗口 hWndInsertAfter 参数设置为 HWND_TOPMOST 并确保 未设置 SWP_NOZORDER 标志,或者通过设置窗口的位置 Z 顺序,使其位于任何现有最顶层窗口之上。当一个 非最顶层窗口被设为最顶层,其拥有的窗口也被设为最顶层 最上面。然而,它的所有者没有改变。

也来自同一页面:

要使用 SetWindowPos 将窗口置于顶部,该过程 拥有该窗口必须具有 SetForegroundWindow 权限。

不过,我猜该链接适用于 Windows Vista 及更高版本。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使窗口出现在所有内容之上(甚至是全屏游戏!)c++/Qt 的相关文章

随机推荐