C++ 如何遍历所有打开的窗口
时间: 2023-11-16 07:05:46 浏览: 62
在 Windows 系统中,可以使用以下步骤遍历所有打开的窗口:
1. 引入 Windows API 头文件。
```c
#include <windows.h>
```
2. 定义枚举窗口的回调函数。
```c
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
// 处理窗口 hwnd
return TRUE;
}
```
其中,`hwnd` 表示枚举到的窗口句柄,`lParam` 表示传入的自定义参数,此处不需要使用,直接返回 `TRUE`。
3. 调用 Windows API 函数 `EnumWindows`。
```c
EnumWindows(EnumWindowsProc, 0);
```
其中,`EnumWindowsProc` 是回调函数的名称,`0` 表示传入的自定义参数,此处不需要使用。
完整示例代码:
```c
#include <windows.h>
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
// 处理窗口 hwnd
return TRUE;
}
int main()
{
EnumWindows(EnumWindowsProc, 0);
return 0;
}
```
相关问题
win32遍历该窗口的所有进程代码展示
以下是使用Win32 API遍历窗口的所有进程的示例代码:
```c++
#include <windows.h>
#include <tchar.h>
#include <psapi.h>
#include <iostream>
using namespace std;
BOOL CALLBACK EnumProcessModulesProc(HMODULE hModule, DWORD_PTR dwBaseAddress)
{
TCHAR szModName[MAX_PATH];
if (GetModuleFileNameEx(GetCurrentProcess(), hModule, szModName, sizeof(szModName) / sizeof(TCHAR)))
{
wcout << L" " << szModName << endl;
}
return TRUE;
}
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
DWORD dwProcessId;
GetWindowThreadProcessId(hwnd, &dwProcessId);
HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwProcessId);
if (hProcess)
{
TCHAR szProcessName[MAX_PATH];
if (GetModuleBaseName(hProcess, NULL, szProcessName, sizeof(szProcessName) / sizeof(TCHAR)))
{
wcout << L"Process Name: " << szProcessName << endl;
}
wcout << L"Process ID: " << dwProcessId << endl;
HMODULE hMods[1024];
DWORD cbNeeded;
if (EnumProcessModules(hProcess, hMods, sizeof(hMods), &cbNeeded))
{
for (int i = 0; i < (cbNeeded / sizeof(HMODULE)); i++)
{
EnumProcessModulesProc(hMods[i], (DWORD_PTR)hProcess);
}
}
}
CloseHandle(hProcess);
return TRUE;
}
int main()
{
EnumWindows(EnumWindowsProc, NULL);
return 0;
}
```
该示例使用两个回调函数来完成任务。`EnumWindowsProc`函数遍历所有窗口,并使用`GetWindowThreadProcessId`函数获取每个窗口所属的进程ID。然后,它打开进程句柄,并使用`GetModuleBaseName`函数获取进程名称。最后,它使用`EnumProcessModules`函数枚举进程的所有模块,并调用`EnumProcessModulesProc`回调函数来获取每个模块的名称。
`EnumProcessModulesProc`函数获取模块的名称,并输出到控制台。
请注意,此示例代码需要链接`psapi.lib`库。
wxWidgets,C++ 遍历文件中所有的坐标,赋值给GeographyPoint westpoint[n] = { westlakeline1,westlakeline2,westlakeline3,westlakeline4 ...westlakelinen};GeographyPoint westlakeline1(tlon1, tlat1);GeographyPoint westlakelinen(tlonn, tlatn);并与全部从float类型转为双类,完整示范例
好的,我来给您提供一个示例代码,使用 wxWidgets 库来实现读取文件和 GUI 界面的显示,示例代码如下:
```c++
#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <cmath>
using namespace std;
// 定义一个结构体,表示地理坐标点
struct GeographyPoint {
double longitude; // 经度
double latitude; // 纬度
};
// 将字符串类型的经纬度转换成浮点数类型的经纬度
double convertToDouble(string str) {
string::size_type sz;
double res = stod(str, &sz); // 将字符串转换成浮点数类型
return res;
}
// 读取文件中的所有坐标数据
vector<GeographyPoint> readPointsFromFile(string filename) {
vector<GeographyPoint> res; // 存储所有坐标点的容器
ifstream infile(filename); // 打开文件
string line;
while (getline(infile, line)) { // 逐行读取文件中的坐标点
string::size_type pos = line.find(","); // 查找逗号的位置,分割经纬度
if (pos == string::npos) { // 如果没有找到逗号,则说明该行数据不合法,跳过
continue;
}
string longitudeStr = line.substr(0, pos); // 截取经度字符串
string latitudeStr = line.substr(pos+1); // 截取纬度字符串
double longitude = convertToDouble(longitudeStr); // 将经度字符串转换成浮点数类型
double latitude = convertToDouble(latitudeStr); // 将纬度字符串转换成浮点数类型
GeographyPoint point;
point.longitude = longitude;
point.latitude = latitude;
res.push_back(point); // 将该坐标点存入容器中
}
infile.close(); // 关闭文件
return res;
}
class MyFrame : public wxFrame
{
public:
MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size);
private:
void OnOpen(wxCommandEvent& event);
void OnExit(wxCommandEvent& event);
void OnAbout(wxCommandEvent& event);
void OnConvert(wxCommandEvent& event);
wxDECLARE_EVENT_TABLE();
};
enum
{
ID_Open = 1,
ID_Convert,
ID_About,
ID_Quit
};
wxBEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_MENU(ID_Open, MyFrame::OnOpen)
EVT_MENU(ID_Convert, MyFrame::OnConvert)
EVT_MENU(ID_About, MyFrame::OnAbout)
EVT_MENU(ID_Quit, MyFrame::OnExit)
wxEND_EVENT_TABLE()
MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size)
: wxFrame(NULL, wxID_ANY, title, pos, size)
{
wxMenu *menuFile = new wxMenu;
menuFile->Append(ID_Open, "&Open\tCtrl-O", "Open an existing file");
menuFile->Append(ID_Convert, "&Convert\tCtrl-C", "Convert the coordinates");
menuFile->AppendSeparator();
menuFile->Append(ID_Quit, "E&xit\tCtrl-Q", "Quit the program");
wxMenu *menuHelp = new wxMenu;
menuHelp->Append(ID_About, "&About\tF1", "Show about dialog");
wxMenuBar *menuBar = new wxMenuBar;
menuBar->Append(menuFile, "&File");
menuBar->Append(menuHelp, "&Help");
SetMenuBar(menuBar);
CreateStatusBar();
SetStatusText("Welcome to the coordinate converter!");
}
void MyFrame::OnOpen(wxCommandEvent& event)
{
wxFileDialog openFileDialog(this, _("Open TXT file"), "", "", "TXT files (*.txt)|*.txt", wxFD_OPEN | wxFD_FILE_MUST_EXIST);
if (openFileDialog.ShowModal() == wxID_CANCEL)
return;
wxString filePath = openFileDialog.GetPath();
SetStatusText("Opened file: " + filePath);
}
void MyFrame::OnExit(wxCommandEvent& event)
{
Close(true);
}
void MyFrame::OnAbout(wxCommandEvent& event)
{
wxMessageBox("This is a coordinate converter application.",
"About the coordinate converter", wxOK | wxICON_INFORMATION);
}
void MyFrame::OnConvert(wxCommandEvent& event)
{
wxFileDialog openFileDialog(this, _("Open TXT file"), "", "", "TXT files (*.txt)|*.txt", wxFD_OPEN | wxFD_FILE_MUST_EXIST);
if (openFileDialog.ShowModal() == wxID_CANCEL)
return;
wxString filePath = openFileDialog.GetPath();
vector<GeographyPoint> points = readPointsFromFile(filePath.ToStdString()); // 读取文件中的所有坐标点
int n = points.size(); // 坐标点的个数
GeographyPoint westpoint[n]; // 定义一个GeographyPoint类型的数组
for (int i = 0; i < n; i++) {
westpoint[i].longitude = points[i].longitude; // 将经度赋值给westpoint数组
westpoint[i].latitude = points[i].latitude; // 将纬度赋值给westpoint数组
}
wxMessageBox("Converted " + to_string(n) + " coordinates from file: " + filePath,
"Coordinate converter", wxOK | wxICON_INFORMATION);
}
class MyApp : public wxApp
{
public:
virtual bool OnInit();
};
bool MyApp::OnInit()
{
MyFrame *frame = new MyFrame("Coordinate converter", wxPoint(50, 50), wxSize(450, 340));
frame->Show(true);
return true;
}
wxIMPLEMENT_APP(MyApp);
```
在上面的代码中,我们首先定义了一个 `GeographyPoint` 结构体,表示地理坐标点,其中包括了经度和纬度两个成员变量。然后,我们定义了一个 `convertToDouble` 函数,用于将字符串类型的经纬度转换成浮点数类型的经纬度,这里使用了 `stod` 函数进行转换。接着,我们定义了一个 `readPointsFromFile` 函数,用于从文件中读取所有的坐标点数据。该函数逐行读取文件中的坐标点,然后使用 `convertToDouble` 函数将字符串类型的经纬度转换成浮点数类型的经纬度,最后将该坐标点存入容器中。
在 wxWidgets 应用程序中,我们定义了一个 `MyFrame` 类,继承自 `wxFrame` 类,用于显示主窗口和处理用户事件。其中,我们实现了 `OnOpen` 函数和 `OnConvert` 函数,分别用于打开文件和进行坐标转换。在 `OnOpen` 函数中,我们使用 `wxFileDialog` 对话框来选择要打开的文件,然后显示该文件的路径。在 `OnConvert` 函数中,我们使用 `wxFileDialog` 对话框来选择要转换的文件,然后调用 `readPointsFromFile` 函数读取文件中的所有坐标点,并进行转换。最后,我们使用 `wxMessageBox` 对话框来显示转换后的坐标点数量和文件路径。
最后,在 `MyApp` 类中,我们重载了 `OnInit` 函数,用于创建主窗口并显示。