您的位置:首页 > 其它

枚举当前环境中打开的所有IE

2009-03-31 20:18 225 查看
IE程序是属于Shell的一个应用程序,要枚举当前打开的所有IE程序窗口,可以通过ShellWindows集合来打开属于Shell的当前的窗口的集合.

首先添加程序需要的头文件和tlb库

//

#import <shdocvw.dll>

#import <mshtml.tlb>

#include <mshtml.h>

//

//定义IE程序处理函数指针

typedef void (*ProcessWebBrowser)( const SHDocVw::IWebBrowser2Ptr pBrowser );

/*

* 枚举当前打开的IE窗口,并输出网页的标题

*/

BOOL EnumInternetExplorer( ProcessWebBrowser pHander )

{

//IE属于shell的一个应用程序,再次需要枚举ShellWindows集合

SHDocVw::IShellWindowsPtr spSHWinds;

if( FAILED( spSHWinds.CreateInstance( __uuidof( SHDocVw::ShellWindows ) ) ) )

return FALSE;

//获取ShellWindows集合的数目

long nCount = spSHWinds->GetCount();

for ( long lIndex = 0; lIndex < nCount; lIndex++ )

{

//枚举每个项

IDispatchPtr spDisp;

_variant_t var( lIndex, VT_I4 );

spDisp = spSHWinds->Item( var );

if ( spDisp != NULL )

{

//Query网页接口

SHDocVw::IWebBrowser2Ptr spBrowser( spDisp );

if( ( spBrowser != NULL ) && ( pHander != NULL ) )

{

//调用处理函数

pHander( spBrowser );

}

}

}

return TRUE;

}

void ShowIETitle( const SHDocVw::IWebBrowser2Ptr pBrowser )

{

//获取网页的文档接口

MSHTML::IHTMLDocument2Ptr spDoc( pBrowser->GetDocument() );

if( spDoc != NULL )

{

CComBSTR bstr;

//获取标题

spDoc->get_title( &bstr );

//bstr即为网页标题

_bstr_t bstr_adapt = bstr;

TCHAR* szTitle = bstr_adapt;

AtlMessageBox( NULL, szTitle );

}

}

调用:

EnumInternetExplorer( ShowIETitle );
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: