在我的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‘。
我正在寻找这样的方式随时解决方案,当用户按下按钮,后台应用程序窗口标题应被检索。
有人能帮我修改代码吗?
发布于 2018-04-05 03:49:14
如果它是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;
}现在,如果我理解您的问题,您只需要将对getBackgroundWindowTitle的调用移动到您的按钮处理程序:
void MyWindow::OnPushbuttonPressed()
{
ui->labelBackgroundWindowTitle.setText(getBackgroundWindowTitle());
}https://stackoverflow.com/questions/49657358
复制相似问题