如何枚举当前会话中的所有窗口句柄,并从其获得该窗口的Caption?

darkstar 2000-06-13 12:27:00
...全文
398 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
robo 2000-06-19
  • 打赏
  • 举报
回复
HWND hWnd=m_hWnd;//FOCUS WIN
while(hWnd)
{
char buf[MAX_PATH];
GetWindowText(hWnd,buf,MAX_PATH);
hWnd = GetParent(hWnd);
}
fenger 2000-06-16
  • 打赏
  • 举报
回复

BOOL CALLBACK EnumWindowProc (HWND hWnd, LPARAM lParam)
{
static char szBuffer[255];
CString strMsg;


// if (!IsWindowVisible (hWnd))
// return (TRUE);

strMsg.Format ("0x%08lX",hWnd);

GetClassName (hWnd, szBuffer, sizeof(szBuffer));
);

GetWindowText(hWnd, szBuffer, sizeof(szBuffer));
);

return (TRUE) ;
}

EnumWindows (EnumWindowProc, (LPARAM) &m_oLstWin);
darkstar 2000-06-16
  • 打赏
  • 举报
回复
感谢jy,现将代码贴出来共享
enumwindow.h
// EnumWinModules.h: interface for the CEnumWinModules class.
//
//////////////////////////////////////////////////////////////////////
// By Mike Ryan (mike@codexia.com)
// Copyright (c) 2000
// 4-30-2000
// Free usage granted in all applications including commercial.
// Do NOT distribute without permission from me. I can be reached
// at mike@codexia.com, http://www.codexia.com
// Please feel free to email me about this class.
//
// Special thanks to Jeff Kay (jeff@codexia.com) for helping with the
// PSAPI functions.
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_ENUMWINMODULES_H__86C7CD44_E90B_4844_97DB_FE2043DC6CBF__INCLUDED_)
#define AFX_ENUMWINMODULES_H__86C7CD44_E90B_4844_97DB_FE2043DC6CBF__INCLUDED_

#include <afxtempl.h> // for carray
#include <psapi.h> // for the process handling, needs psapi.dll which is not distributed with windows.
// note: psapi is a windows NT/2000 specific DLL
// Windows 98 functionality is built in to the class CEnumWinModules98 (included w/this example).

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

//// FILTERS //// // FOR ALL WINDOWS USE 0 (zero)
#define FILTER_VISIBLEONLY 1 // not compatible with NONVISIBLEONLY
#define FILTER_NONVISIBLEONLY 2 // not compatible with VISIBLEONLY
#define FILTER_PARENTONLY 4 // not compatible with CHILDONLY
#define FILTER_CHILDONLY 8 // not compatible with PARENTONLY
#define FILTER_APPS 16 // filter out programs by class name on the exclude list, use AddExclusion

typedef struct sWindowInfo
{
CString strWindowTitle;
CString strClassName;
CString strModuleName;
DWORD dwPID;
HWND hWnd;
} sWindowInfo;

class CEnumWinModules
{
public:
//// FUNCTIONS ////
CEnumWinModules();
virtual ~CEnumWinModules();
void Clear(void);

int Process(int nFilter); // main function, call this to enum all windows and set filtering info
BOOL Filter(HWND hWnd); // for use by EnumWindowsProc
void AddWindow(sWindowInfo *pWinInfo) { m_aWindows.Add(pWinInfo); } // add a sWindowInfo struct to m_aWindows
void AddExclusion(CString strExclude);
void ClearExclusions(void) { m_aExclusions.RemoveAll(); }

// info retreval functions
CString GetWindowTitle(int nIndex);
CString GetWindowTitle(HWND hWnd);
CString GetClassName(int nIndex);
CString GetClassName(HWND hWnd);
CString GetModuleName(int nIndex);
CString GetModuleName(HWND hWnd);
HWND GetHwnd(int nIndex);
sWindowInfo *GetWindowInfo(int nIndex);
int GetWindowCount(void) { return m_aWindows.GetSize(); }

protected:
//// FUNCTIONS ////
static BOOL CALLBACK EnumWindowsProc(HWND hWnd, LPARAM lParam);

//// VARIABLES ////
CArray<sWindowInfo*, sWindowInfo*> m_aWindows;
CArray<CString, CString> m_aExclusions;
int m_nFilter;
};

#endif // !defined(AFX_ENUMWINMODULES_H__86C7CD44_E90B_4844_97DB_FE2043DC6CBF__INCLUDED_)

enumwindows.cpp

// EnumWinModules.cpp: implementation of the CEnumWinModules class.
//
//////////////////////////////////////////////////////////////////////
// By Mike Ryan (mike@codexia.com)
// Copyright (c) 2000
// 4-30-2000
// Free usage granted in all applications including commercial.
// Do NOT distribute without permission from me. I can be reached
// at mike@codexia.com, http://www.codexia.com
// Please feel free to email me about this class.
//
// Special thanks to Jeff Kay (jeff@codexia.com) for helping with the
// PSAPI functions.
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "EnumWinModules.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CEnumWinModules::CEnumWinModules() : m_nFilter(0)
{
}

CEnumWinModules::~CEnumWinModules()
{
Clear();
}

void CEnumWinModules::Clear()
{
for (int i=0;i<m_aWindows.GetSize();i++)
{
delete m_aWindows[i];
}
m_aWindows.RemoveAll();
}


// Function name : EnumWindowsProc
// Description : This function will enumerate all running windows and store the results in the
// class member variable m_aWindows. The list will be filtered based on the params
// set by the Filter() function.
// Return type : BOOL CALLBACK
// Argument : HWND hWnd
// Argument : LPARAM lParam
//
// R E V I S I O N S:
// DATE PROGRAMMER CHANGES
//
BOOL CALLBACK CEnumWinModules::EnumWindowsProc(HWND hWnd, LPARAM lParam)
{
CEnumWinModules *ew = (CEnumWinModules*)lParam;
ASSERT(ew);

if (!ew->Filter(hWnd))
{
sWindowInfo *pWindowInfo = new sWindowInfo;
ASSERT(pWindowInfo);

// get window class name
CString strClass;
::GetClassName(hWnd, strClass.GetBuffer(_MAX_PATH), _MAX_PATH);
strClass.ReleaseBuffer();
pWindowInfo->strClassName = strClass;

// get pid
DWORD dwPID;
GetWindowThreadProcessId(hWnd, &dwPID);
pWindowInfo->dwPID = dwPID;

// get hwnd
pWindowInfo->hWnd = hWnd;

// add it
ew->AddWindow(pWindowInfo);
}

return true; // continue enumeration (otherwise this would be false)
}


// Function name : Filter
// Description : tests whether or not we should filter this window
// Return type : BOOL ; true on filter, false on not
// Argument : HWND hWnd
//
// R E V I S I O N S:
// DATE PROGRAMMER CHANGES
//
BOOL CEnumWinModules::Filter(HWND hWnd)
{
if (!m_nFilter) return false; // no reason to keep testing, they want all windows.

// test visibility filters
if (m_nFilter & FILTER_VISIBLEONLY)
{
// is this window invisible, if so filter it
if (!IsWindowVisible(hWnd)) return true;
}
else if (m_nFilter & FILTER_NONVISIBLEONLY)
{
// is this window visible, if so filter it
if (IsWindowVisible(hWnd)) return true;
}

// filter the parent/child windows
if (m_nFilter & FILTER_PARENTONLY)
{
// is it a child? if yes, filter
if (GetParent(hWnd)) return true;
}
else if (m_nFilter & FILTER_CHILDONLY)
{
// is it a parent? if yes, filter
if (!GetParent(hWnd)) return true;
}

// filter apps by class name
if (m_nFilter & FILTER_APPS)
{
CString temp;
::GetClassName(hWnd, temp.GetBuffer(_MAX_PATH), _MAX_PATH);
temp.ReleaseBuffer();

for (int i=0;i<m_aExclusions.GetSize();i++)
{
if (m_aExclusions.GetAt(i) == temp) return true;
}
}

return false; // don't filter if we get to this point
}



// Function name : CEnumWinModules::Process
// Description : The main function, call this to enum all the windows and set the class data members.
// Return type : int ; see .h for description
// Argument : int nFilter
//
// R E V I S I O N S:
// DATE PROGRAMMER CHANGES
//
int CEnumWinModules::Process(int nFilter)
{
// test filter for validity
// NOTE: these can be changed to return 0 instead of assert in a dynamic program
ASSERT(!((nFilter & FILTER_VISIBLEONLY) && (nFilter & FILTER_NONVISIBLEONLY)));
ASSERT(!((nFilter & FILTER_PARENTONLY) && (nFilter & FILTER_CHILDONLY)));

// set filter
m_nFilter = nFilter;

// clear all
Clear();

// process windows
EnumWindows(EnumWindowsProc, (LPARAM)this);

// enumerate processes
DWORD *pdwProcessIDs = new DWORD[250]; // should be a good number of processes. Change if you are using on a server
// that might be running more than 250 processes.
// change ti below in the EnumProcesses line as well. :)
ASSERT(pdwProcessIDs);
DWORD dwSize;

if (!EnumProcesses(pdwProcessIDs, 250*sizeof(DWORD), &dwSize))
{
delete [] pdwProcessIDs;
return 0;
}

// get the exe names
HANDLE hProcess;
HMODULE hModule;
DWORD dwSize2;
char szFilename[_MAX_PATH];
int nIndex = 0;
dwSize = dwSize / sizeof(DWORD);
for (int i=0;i<(int)dwSize;i++)
{
nIndex = -1;
for (int j=0;j<m_aWindows.GetSize();j++) // try to find a match of PID from a hWnd and PID from EnumProcesses
{
if (m_aWindows[j]->dwPID == pdwProcessIDs[i])
{
nIndex = j;
break;
}
}

if (nIndex > -1) // did we find a match
{
// open the process so we can get a handle
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, false, pdwProcessIDs[i]);
if (hProcess)
{
if (EnumProcessModules(hProcess, &hModule, sizeof(hModule), &dwSize2))
{
// get path name
if (!GetModuleFileNameEx(hProcess, hModule, szFilename, sizeof(szFilename)))
szFilename[0] = 0; // no name, so null it out

// add it to the list
m_aWindows[nIndex]->strModuleName = szFilename;
}
CloseHandle(hProcess); // be nice. :)
}
}
}

delete [] pdwProcessIDs;

return 1;
}

CString CEnumWinModules::GetClassName(int nIndex)
{
// test bounds
ASSERT(nIndex > -1);
ASSERT(nIndex < m_aWindows.GetSize());

return m_aWindows.GetAt(nIndex)->strClassName;
}

CString CEnumWinModules::GetClassName(HWND hWnd)
{
for (int i=0;i<m_aWindows.GetSize();i++)
{
if (m_aWindows[i]->hWnd == hWnd)
{
return GetClassName(i);
}
}
ASSERT(0); // this is bad, of course we could return an empty string.
return CString();
}

CString CEnumWinModules::GetWindowTitle(int nIndex)
{
ASSERT(nIndex > -1);
ASSERT(nIndex < m_aWindows.GetSize());

CString strTitle;
GetWindowText(m_aWindows[nIndex]->hWnd, strTitle.GetBuffer(_MAX_PATH), _MAX_PATH);
strTitle.ReleaseBuffer();
return strTitle;
}

CString CEnumWinModules::GetWindowTitle(HWND hWnd)
{
for (int i=0;i<m_aWindows.GetSize();i++)
{
if (m_aWindows[i]->hWnd == hWnd)
{
return GetWindowTitle(i);
}
}
ASSERT(0); // this is bad, of course we could return an empty string.
return CString();
}

CString CEnumWinModules::GetModuleName(int nIndex)
{
ASSERT(nIndex > -1);
ASSERT(nIndex < m_aWindows.GetSize());

return m_aWindows[nIndex]->strModuleName;
}

CString CEnumWinModules::GetModuleName(HWND hWnd)
{
for (int i=0;i<m_aWindows.GetSize();i++)
{
if (m_aWindows[i]->hWnd == hWnd)
{
return GetModuleName(i);
}
}
ASSERT(0); // this is bad, of course we could return an empty string.
return CString();
}

HWND CEnumWinModules::GetHwnd(int nIndex)
{
ASSERT(nIndex > -1);
ASSERT(nIndex < m_aWindows.GetSize());

return m_aWindows.GetAt(nIndex)->hWnd;
}

sWindowInfo *CEnumWinModules::GetWindowInfo(int nIndex)
{
ASSERT(nIndex > -1);
ASSERT(nIndex < m_aWindows.GetSize());

return m_aWindows.GetAt(nIndex);
}


// Function name : CEnumWinModules::AddExclusion
// Description : Exclude a window from the list based on class name
// Return type : void
// Argument : CString strExlude
//
// R E V I S I O N S:
// DATE PROGRAMMER CHANGES
//
void CEnumWinModules::AddExclusion(CString strExlude)
{
m_aExclusions.Add(strExlude);
}
jy 2000-06-16
  • 打赏
  • 举报
回复
OK, OOK.
我真TMD蠢不可及:直接去这里看看和下载:
Desc.: http://codeguru.earthweb.com/system/enummodules.shtml;
Proj.: http://codeguru.earthweb.com/system/enummodules_demo.zip
Src.: http://codeguru.earthweb.com/system/enummodules_src.zip
jy 2000-06-16
  • 打赏
  • 举报
回复
本想给你一个正确的下载地点,可惜找不到了。
不过,他的完整代码我手上是有的,如果需要,请来信确认。

另外,和你要求接近的参考地点推荐如下,假定你又耐心自己写的话:
说明页面:http://codeguru.earthweb.com/system/ListProcesses.shtml
演示代码:http://codeguru.earthweb.com/system/ListProcesses.zip

土豆 2000-06-15
  • 打赏
  • 举报
回复
枚举窗口我是知道怎么处理的,但我不知道你的所谓“会话”是指什么?
下面是一段DELPHI的代码,你应该很容易转换成C的吧.
这段程序采用第归算法,枚举所有的窗口,包括子窗口。
var
hand:HWND;
ClassName,WndName:PChar;
s_ClsName,s_WndName:String[255];
s_Space:String;
i:Integer;
begin
s_Space:='';
for i:=1 to Level do
s_Space:=s_Space+' ';

if hParent=0 then
Exit;
ClassName:=@s_ClsName[1];
WndName:=@s_WndName[1];
hand:=GetWindow(hParent,GW_CHILD);
while hand>0 do //枚举所有同级窗口,句柄为0,也就是C中的NULL.
begin
GetClassName(hand,ClassName,255);
GetWindowText(hand,WndName,255);
s_clsName:=StrPas(ClassName);
s_WndName:=StrPas(WndName);
if s_clsName='' then s_clsName:='No Window Name';
if s_WndName='' then s_WndName:='No Window Name';

mm_wnd.Lines.Add(s_Space+s_WndName+'('+s_ClsName+')');
//mm_wnd 是TMemo类型的一个对象。用于显示文本。
EnumWnd(hand,Level+1); //枚举子窗口。

hand:=GetWindow(hand,GW_HWNDNEXT);
end;
end;
darkstar 2000-06-15
  • 打赏
  • 举报
回复
没有人肯帮帮我吗?

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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