gpt4 book ai didi

c++ - QT中如何获取后台应用程序窗口标题?

转载 作者:太空宇宙 更新时间:2023-11-04 12:49:35 25 4
gpt4 key购买 nike

在我的 qt 应用程序中,当我按下一个按钮时,我需要显示后台应用程序窗口标题。目前我曾经以这样的方式管理它,即在调用“setupUi”之前调用“getBackgroundWindowTitle”函数。所以这会为我返回后台应用程序窗口标题。我目前的实现如下

MyWindow::MyWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MyWindow)
{
g_window_title = getBackgroundWindowTitle();
ui->setupUi(this);
}

void MyWindow::OnPushbuttonPressed()
{
ui->labelBackgroundWindowTitle.setText(g_window_title);
}

QString MyWindow::getBackgroundWindowTitle()
{
char buff[256];
HWND hwnd = GetForegroundWindow(); //currently this gives my current application window title
GetWindowText(hwnd, (LPWSTR) buff, 254);
QString title = QString::fromWCharArray((const wchar_t *)buff);
return title;
}

但是,此代码的问题是,当后台应用程序窗口更改时,我需要重新启动应用程序以再次获取背景标题,因为需要在调用 setupUi 之前调用“getBackgroundWindowTitle”。

我正在寻找任何时候的解决方案,当用户按下按钮时,应检索后台应用程序窗口标题。

有人可以帮我修改我的代码吗?

最佳答案

如果它是 Z-Order 中的上一个(或下一个,请参见链接)窗口,您可以使用:GetNextWindow ,像这样:

QString MyWindow::getBackgroundWindowTitle()
{
char buff[256];
HWND myHwnd = GetForegroundWindow();
HWND hwnd = GetNextWindow(myHwnd, GW_HWNDNEXT);
GetWindowText(hwnd, (LPWSTR) buff, 254);
QString title = QString::fromWCharArray((const wchar_t *)buff);
return title;
}

现在,如果我理解你的问题,你只需要将对 getBackgroundWindowTitle 的调用移动到你的按钮处理程序:

void MyWindow::OnPushbuttonPressed()
{
ui->labelBackgroundWindowTitle.setText(getBackgroundWindowTitle());
}

关于c++ - QT中如何获取后台应用程序窗口标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49657358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com