如何取得当前系统所有窗口的标题列表

steer1 2002-11-06 03:33:23
比如系统目前共打开3个程序:
1.notepad.exe
2.ie
3.bcb程序

则显示这3个程序的标题到一个文本框:

abc.txt - 记事本
abc - Microsoft Internet Explorer
C++ Builder6 - abc
...全文
189 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
steer1 2002-11-07
  • 打赏
  • 举报
回复
那位给个例子,上面的到底是不是bcb里用的?怎么不能用... :(
Behard 2002-11-07
  • 打赏
  • 举报
回复
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
#include <winuser.h>
#include <stdio.h>
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
//typedef

struct WINLIST
{
HANDLE hWnd;
char cWinBuf[256];
} ;
void CloseSpedia(void);
//BOOL CALLBACK EnumWindowsProc( HWND hWnd, LPARAM lParam );
//char buffer[256];
TForm1 *Form1;
struct WINLIST gWinList[256];
int giCountWin,j;
//-cpp------------
//---------------------------------------------------------
bool CALLBACK EnumWindowsProc( HWND hWnd, LPARAM lParam )
{
char buffer[256];

GetWindowText(hWnd, buffer, 256);
if ( strlen(buffer) )
{
if (giCountWin < 256)
{
gWinList[ giCountWin].hWnd = hWnd;
strcpy(gWinList[ giCountWin].cWinBuf,buffer);
giCountWin ++;
}
}
return true;
}
//----------------------------------------------------------
bool CALLBACK EnumChildProc( HWND hWnd, LPARAM lParam )
{
char buffer[256];

GetWindowText(hWnd, buffer, 256);
if ( strlen(buffer) )
{
if (giCountWin < 256)
{
gWinList[ giCountWin].hWnd = hWnd;
strcpy(gWinList[ giCountWin].cWinBuf,buffer);
giCountWin ++;
}
}
return true;
}
//-----------------------------------------------------
void CloseSpedia(void)//
{
bool geth=false;
HANDLE gethWnd1=0,gethWnd2=0;
giCountWin = 0;
EnumWindows( (WNDENUMPROC)EnumWindowsProc,0);
// ShowMessage(IntToStr(giCountWin).c_str());
for ( j = 0; j<giCountWin; j++)
{
// if(j>0)Form1->CheckListBox1->Items->Strings[j]=gWinList[j].cWinBuf;
if ( strcmp(gWinList[j].cWinBuf,"Default IME") != 0 )
Form1->ListBox1->Items->Add(gWinList[j].cWinBuf);
// ShowMessage(gWinList[j].cWinBuf);

// if ( strcmp(gWinList[j].cWinBuf,"MS-DOS 方式") == 0 )
// if ( strcmp(gWinList[j].cWinBuf,"金山毒霸 - 病毒防火墙") == 0 )
if ( strcmp(gWinList[j].cWinBuf,"金山毒霸 - 控制中心") == 0 )
{
if(!geth){gethWnd1=gWinList[j].hWnd;geth=true;}
else gethWnd2=gWinList[j].hWnd;
// ShowWindow(gWinList[j].hWnd,SW_MAXIMIZE); //重新打开窗口
// ShowMessage(gWinList[j].cWinBuf);
// PostMessage( gWinList[j].hWnd,WM_SHOWWINDOW,0,0);
// break;
}

if ( strcmp(gWinList[j].cWinBuf,"MS-DOS 方式") == 0 )
{
PostMessage( gWinList[j].hWnd,WM_CLOSE,0,0); //Close the window
}
if ( strcmp(gWinList[j].cWinBuf,"弹出广告 - Microsoft Internet Explorer") == 0 )
{
//ShowWindow(gWinList[j].hWnd,SW_HIDE); //
//PostMessage( gWinList[j].hWnd,WM_CLOSE,0,0); //Close the window
}

}

/*
giCountWin = 0;
EnumChildWindows( gethWnd1 , (WNDENUMPROC)EnumChildProc , 0 ) ;
// ShowMessage(IntToStr(giCountWin).c_str());

for ( j = 0; j<giCountWin; j++)
{
Form1->ListBox1->Items->Add(gWinList[j].cWinBuf);
}

giCountWin = 0;
EnumChildWindows( gethWnd2 , (WNDENUMPROC)EnumChildProc , 0 ) ;
// ShowMessage(IntToStr(giCountWin).c_str());

for ( j = 0; j<giCountWin; j++)
{
Form1->ListBox1->Items->Add(gWinList[j].cWinBuf);
if ( strcmp(gWinList[j].cWinBuf,"金山毒霸 - 病毒防火墙") == 0 )
// if ( strcmp(gWinList[j].cWinBuf,"金山毒霸 - 控制中心") == 0 )
{
// gethWnd=gWinList[j].hWnd
ShowWindow(gWinList[j].hWnd,SW_HIDE); //重新打开窗口
// ShowMessage(gWinList[j].cWinBuf);
}
}
*/
}
//

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
CloseSpedia();
}
//---------------------------------------------------------------------------
hatumei 2002-11-07
  • 打赏
  • 举报
回复
String caption[256];
HANDLE h=this->Handle;
int cnt=0;
h=GetWindow(h,GW_HWNDFIRST);
do
{
caption[cnt++]=GetWindowText(h);
}
while((h=GetWindow(h,GW_HWNDNEXT))!=NULL);
NowCan 2002-11-06
  • 打赏
  • 举报
回复

BOOL CALLBACK EnumIEPopupWindowsProc(
HWND hwnd, // handle to parent window
LPARAM lParam // application-defined value
)
{
//在这里通过句柄取得标题。
return TRUE;
}


EnumWindows((WNDENUMPROC)EnumIEPopupWindowsProc,0);
lingbin 2002-11-06
  • 打赏
  • 举报
回复
void ExePathFromProcID(DWORD idProc, char *szBuffer)
{
PROCESSENTRY32 process;
HANDLE hSnap;
BOOL f;

szBuffer[0] = 0;

// Get a "snapshot" of the currently running processes in the system
hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if ( hSnap == NULL )
return;
// Very important!! Set the size of the structure so that the enumeration
// functions know which version of the structure they are working with.

process.dwSize = sizeof(PROCESSENTRY32);
f = Process32First(hSnap, &process);

// Loop through all of the processes running in this snapshot
while ( f )
{
// Is this ours?
if ( process.th32ProcessID == idProc )
{
// Yes. Get the name of it and get out of here.
CloseHandle( hSnap );
strcpy( szBuffer, process.szExeFile );
return;
}
f = Process32Next(hSnap, &process);
}
// Make sure the handle is closed.
CloseHandle( hSnap );
}


// CallBack function for the EnumWindows Windows API function.
BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam )
{
char szBuffer[ 256 ];
char szFileName[ 256 ];

// Make sure this is a visible, or iconized, parent window.
if ( (GetWindowLong(hwnd,GWL_HWNDPARENT)==0) &&
(IsWindowVisible(hwnd) || IsIconic(hwnd)) )
{
GetWindowText( hwnd, szBuffer, 256 );

DWORD pid;
GetWindowThreadProcessId( hwnd, &pid );
ExePathFromProcID(pid, szFileName);

if ( strlen(szFileName) && strlen(szBuffer) )
{
AnsiString s = szBuffer;
s += " : ";
s += szFileName;
TMForm->ListBox1->Items->Add( s.c_str() );
}
}
// You return TRUE from a callback function to continue enumerating.
return TRUE;
}


void __fastcall TTMForm::FormCreate(TObject *Sender)
{
// Enumerate all windows.
EnumWindows( (WNDENUMPROC)EnumWindowsProc, 0);
}
Napoleo 2002-11-06
  • 打赏
  • 举报
回复
EnumWindows(
WNDENUMPROC lpEnumFunc, // callback function
LPARAM lParam // application-defined value
)
steer1 2002-11-06
  • 打赏
  • 举报
回复
如果可以取到当前所有窗口的句柄也行啊

1,221

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧