如何使一个窗口出现在一切之上(甚至是全屏游戏!)c++/Q

2024-06-28 20:24:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试制作一个应用程序,在屏幕中央显示一个十字线,并保持在其他一切之上。 其目的是在一些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(),以及为什么游戏的行为与普通窗口不同,也会引起注意。

提前谢谢。


Tags: theto游戏ifheremyiterror
3条回答

我发现c++库DirectDrawOverlayLib应该能够做到这一点。引用网站:

An unmanaged C++ library to create, manage and draw to DirectDraw overlays. A C++/CLI wrapper for .NET clients is included.

DirectDraw overlays are special DirectDraw surfaces that are shown over everything else, including full-screen games and applications. They can be used to implement programs like XFire that display information during fullscreen game operation.

SetWindowPos()文档中:

A window can be made a topmost window either by setting the hWndInsertAfter parameter to HWND_TOPMOST and ensuring that the SWP_NOZORDER flag is not set, or by setting a window's position in the Z order so that it is above any existing topmost windows. When a non-topmost window is made topmost, its owned windows are also made topmost. Its owners, however, are not changed.

同样来自同一页:

To use SetWindowPos to bring a window to the top, the process that owns the window must have SetForegroundWindow permission.

不过,我想这个链接是针对WindowsVista和更高版本的。

我知道这对你没什么帮助,但是请看一下陈雷蒙的this postHow do I create a topmost window that is never covered by other topmost windows?

相关问题 更多 >