我正在尝试使应用程序在屏幕中央显示十字准线并保持其他所有内容.目的是在一些没有提供的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();
Run Code Online (Sandbox Code Playgroud)
我的猜测是游戏等应用程序可以更直接地控制显示设备.我正在寻找这个问题的任何解决方案(不是一个涉及透明的最顶层窗口的问题).另外,如果有人可以向我解释如何有效地使用GetLastError(),以及为什么游戏的行为与普通窗口不同,也会在旁边.
提前致谢.
来自SetWindowPos()文档:
通过将 hWndInsertAfter 参数设置为 HWND_TOPMOST 并确保未设置 SWP_NOZORDER 标志,或者通过设置窗口在 Z 顺序中的位置,使其位于任何现有的最顶层窗口之上,可以将窗口设为最顶层窗口。当非最顶层窗口变为最顶层时,其拥有的窗口也变为最顶层。然而,它的所有者没有改变。
也来自同一页面:
要使用 SetWindowPos 将窗口置于顶部,拥有该窗口的进程必须具有 SetForegroundWindow 权限。
不过,我猜该链接适用于 Windows Vista 及更高版本。