1

我尝试使用简单的 C++ 代码(见下文)为 Windows 上所有打开的窗口检索一些信息(x、y、宽度、高度和标题):

#include <iostream>
#include <windows.h>
using namespace std;

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pCmdLine, int     iCmdShow)
{
    EnumWindows(EnumWindowsProc, NULL);
    //system("PAUSE");
    return 0;
}

BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam)
{
char class_name[255];
char title[255];
int tmpWidth;
int tmpHeight;
HWND currenthwnd;
RECT WindowRect;

GetClassName(hwnd,class_name, sizeof(class_name));
GetWindowText(hwnd,title,sizeof(title));
GetWindowRect(hwnd,&WindowRect);

//if(WindowRect.left>-50 && title != "" && title != NULL && strlen(title)>0){
    tmpHeight = WindowRect.bottom - WindowRect.top;
    tmpWidth = WindowRect.right - WindowRect.left;
    cout <<"@@##@@"<<title<<",(@@#@@)";
    cout <<WindowRect.left<<",(@@#@@)";
    cout <<WindowRect.top<<",(@@#@@)";
    cout <<tmpWidth<<",(@@#@@)";
    cout <<tmpHeight<<",(@@#@@)";
    currenthwnd=GetForegroundWindow();
    if (currenthwnd!=hwnd){
       cout <<title<<",(@@#@@)false";
    }else{
       cout <<title<<",(@@#@@)true";
    }
 //}

 return TRUE;
}

但是我在这段代码中遇到了一些问题,我也尝试使用带有 Get-Process 函数的 PowerShell,但是这个函数不会返回所有打开的窗口,而是返回所有现有的进程。

如何检索所有打开窗口的标题、x、y、高度?

谢谢你的帮助

4

1 回答 1

2

在 powershell 中,您可以使用WASP模块并编写如下内容:

Get-Process | Where-Object {$_.MainWindowTitle -ne ""}  | 
% {$_.processname + "-" + $_.mainwindowtitle;
       Get-WindowPosition -Window $_.handle }

这是我实际的 powershell windows 的结果示例:

powershell - Posh - Admin
Location : {X=4,Y=44}
Size     : {Width=885, Height=129}
X        : 4
Y        : 44
Width    : 885
Height   : 129
Left     : 4
Top      : 44
Right    : 889
Bottom   : 173
IsEmpty  : False
于 2012-09-21T11:49:54.810 回答