如何使窗口出现在所有内容(甚至是全屏游戏!)之上(甚至是全屏游戏!)c ++ / Qt
How to make a window appear on top of everything (even full screen games!) c++/Qt
我正在尝试使应用程序在屏幕中心显示十字准线并保持在其他所有内容之上。目的是在一些不提供十字准线的 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(),以及为什么游戏的行为与普通窗口不同。
提前谢谢。
来自 SetWindowPos() 文档:
可以通过设置 hWndInsertAfter 参数HWND_TOPMOST并确保 SWP_NOZORDER标志未设置,或者通过在 Z 顺序,使其位于任何现有最顶层窗口的上方。当一个 非最顶层的窗口被做成最上面的,它拥有的窗口也被做成 上面。但是,其所有者不会更改。
同样来自同一页面:
要使用 SetWindowPos 将窗口带到顶部,该过程 拥有该窗口必须具有 SetForegroundWindow 权限。
但是,我想该链接适用于Windows Vista及更高版本。
我发现c++库DirectDrawOverlayLib应该能够做到这一点。引用网站:
用于创建、管理和绘制到 DirectDraw 叠加的非托管C++库。一个 包括 .NET 客户端的 C++/CLI 包装器。
直接绘制叠加是显示的特殊直接绘制表面 凌驾于其他一切之上,包括全屏游戏和应用程序。 它们可用于实现像 XFire 这样的程序,这些程序在全屏游戏操作期间显示信息。
- 在C++游戏中与库存系统作斗争
- 换位表导致测试失败(但在游戏中运行良好)
- Craps游戏问题,忽略if语句
- 对于C++,方括号中的标题必须总是在引号中的标题之上吗
- 矢量下标超出SFML游戏中的范围
- 我是 c++ 的新手.学习基本知识后,我想做井字游戏.对于印刷板,我在下面写代码,但它显示错误
- 我在贪吃蛇游戏中收到了错误代码 -1073741571
- 我应该在简单的策略游戏中为各个派系使用类吗 - C++
- 游戏体验和升级
- 我的代码运行良好,但在游戏循环中中断
- 如何使用 SFML 在贪吃蛇游戏中定义游戏结束?
- 我可以使用任何好的逻辑来阻止计算机将 O 放在井字游戏中的现有 X 上
- 将鼠标和键盘输入发送到 unity3d 游戏 (Rust)
- 在猜谜游戏 c++ 中不计算尝试次数
- 在C++不适用于猜数字游戏的情况下再次播放选项
- 在为我的基于文本的 RPG 游戏制作库存时遇到困难
- 在定义字符数组(井字游戏)的 for 循环中应用输入限制
- Frank Luna 在他的书"使用 DirectX12 进行 3D 游戏编程"的介绍中盒子示例的问题
- 如何在Linux上正确发布C++软件(游戏)
- 如何使窗口出现在所有内容(甚至是全屏游戏!)之上(甚至是全屏游戏!)c ++ / Qt