将上述程序放入窗口程序中
时间: 2023-09-24 18:11:25 浏览: 51
以下是将获取所有窗口句柄的程序嵌入到一个窗口程序中的示例代码:
```c++
#include <QApplication>
#include <QVector>
#include <Windows.h>
#include <QWidget>
#include <QVBoxLayout>
#include <QTextEdit>
class MainWindow : public QWidget
{
public:
MainWindow(QWidget *parent = nullptr) : QWidget(parent)
{
QVBoxLayout* layout = new QVBoxLayout(this);
QTextEdit* textEdit = new QTextEdit(this);
layout->addWidget(textEdit);
setLayout(layout);
QVector<HWND> windows;
EnumWindows(EnumWindowsProc, reinterpret_cast<LPARAM>(&windows));
for (auto hwnd : windows)
{
QString title;
title.resize(GetWindowTextLength(hwnd));
GetWindowText(hwnd, title.data(), title.size());
textEdit->append(title);
}
}
private:
static BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
QVector<HWND>* windows = reinterpret_cast<QVector<HWND>*>(lParam);
if (IsWindowVisible(hwnd))
{
windows->push_back(hwnd);
}
return TRUE;
}
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
```
在这个示例程序中,我们创建了一个继承自QWidget的MainWindow类,并在构造函数中进行窗口句柄的遍历。遍历过程中,我们使用了GetWindowText函数来获取每个窗口的标题,并将其添加到QTextEdit控件中。最后,在main函数中创建MainWindow对象并显示它。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)