[英]How to get background application window title in QT?
In my qt application when I press a button, I need to display the background application window title. 在我的qt应用程序中,当我按下按钮时,需要显示背景应用程序窗口标题。 Currently i used to manage this in such a way that, call 'getBackgroundWindowTitle' function before 'setupUi' call. 目前,我通常以这种方式进行管理,即在调用“ setupUi”之前调用“ getBackgroundWindowTitle”功能。 So this returns the background application window title for me. 因此,这为我返回了后台应用程序窗口标题。 My current implementation is as below 我当前的实现如下
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;
}
But, the issue with this code is, when the background application window changes, I need to restart the application to get the background title again because 'getBackgroundWindowTitle' need to be called before setupUi call. 但是,此代码的问题是,当后台应用程序窗口更改时,我需要重新启动应用程序以再次获取后台标题,因为需要在setupUi调用之前调用“ getBackgroundWindowTitle”。
I am looking for anytime solution in such a way that when user pressed the pushbutton, the background application window title should be retrieved. 我正在寻找随时解决方案,当用户按下按钮时,应该检索后台应用程序窗口标题。
Can somebody help me to modify my code ? 有人可以帮我修改我的代码吗?
If it's the previous (or next, see link) window in the Z-Order, you can use: GetNextWindow
, like so: 如果是Z顺序中的上一个(或下一个,请参见链接)窗口,则可以使用: 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;
}
Now, if I understand your question, you just need to move the call to getBackgroundWindowTitle
to your button handler: 现在,如果我理解了您的问题,您只需要将对getBackgroundWindowTitle
的调用移至按钮处理程序即可:
void MyWindow::OnPushbuttonPressed()
{
ui->labelBackgroundWindowTitle.setText(getBackgroundWindowTitle());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.