18,363
社区成员




HWND hWorker,hRebar,hComEx,hcom,hEdit;
HWND hwndParent=(HWND)FindWindow("IEFrame",0);//找到IE窗口
TCHAR szError[260];
hWorker=(HWND)FindWindowEx(hwndParent,0,"WorkerW",0);//找到工作区窗口(不是internet explorer server窗口)
hRebar=(HWND)FindWindowEx(hWorker,0,"ReBarWindow32",0);//找到Rebar窗口
hComEx=(HWND)FindWindowEx(hRebar,0,"Address Band Root",0);//找到Comboboxex
//hcom=(HWND)FindWindowEx(hComEx,0,"ComboBox",NULL);//找到combobox
hEdit=(HWND)FindWindowEx(hComEx,0,"Edit",NULL);//找到ComBoBox的edit控件句柄
::SendMessage(hEdit,WM_GETTEXT,260,(LPARAM)szError);
TCHAR bb[200];
//获取标题
::SendMessage(hIE,WM_GETTEXT,200,(LPARAM)bb);
AfxMessageBox(szError);
WCHAR wIEText[255] = {0},wIEURL[255] = {0};
CWnd *IEhwnd =FindWindow(_T( "IEFrame"),NULL);
HWND hwnd = IEhwnd->GetSafeHwnd();
if(hwnd)
{
::SendMessage(hwnd,WM_GETTEXT,255,(LPARAM)wIEText);
HWND hwnd2=::FindWindowEx(hwnd,NULL,_T("WorkerW"),NULL);
HWND hwnd3=::FindWindowEx(hwnd2,NULL,_T("ReBarWindow32"),NULL);
HWND hwnd4=::FindWindowEx(hwnd3,NULL,_T("ComboBoxEx32"),NULL);
//HWND hwnd5=::FindWindowEx(hwnd4,NULL,_T("ComboBox"),NULL);
//HWND hwnd6=::FindWindowEx(hwnd5,NULL,_T("Edit"),NULL);
HWND hwnd5=::FindWindowEx(hwnd3,NULL,_T("Address Band Root"),NULL);
HWND hwnd6=::FindWindowEx(hwnd5,NULL,_T("Edit"),NULL);
::SendMessage(hwnd6,WM_GETTEXT,255,(LPARAM)wIEURL);
// AfxMessageBox(sz);
}
// getURL.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "getURL.h"
//#import <shdocvw.dll>
#include <mshtml.h>
#include <oleacc.h>
#include <atlbase.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// The one and only application object
CWinApp theApp;
using namespace std;
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.
long ies;
::CoInitialize(NULL);
CComPtr< IShellWindows > spShellWin;
HRESULT hr = spShellWin.CoCreateInstance( CLSID_ShellWindows );
if(SUCCEEDED(hr))
{
spShellWin->get_Count(&ies); //获取当前IE数目
// if(!ies) return bret;
for(int i=1; i<ies; i++)
{
CComPtr< IDispatch > pDispIE;
HRESULT hr=spShellWin->Item( CComVariant( (long)i ), &pDispIE );
if ( FAILED ( hr ) ) continue;
CComQIPtr< IWebBrowser2 > pBrowser = pDispIE;
if ( !pBrowser ) continue;
CComPtr < IDispatch > pDispDoc;
hr = pBrowser->get_Document( &pDispDoc );
if ( FAILED ( hr ) ) continue;
CComQIPtr< IHTMLDocument2 > pDocument2 = pDispDoc;
if ( !pDocument2 ) continue;
CComBSTR bstrURL=NULL;
CString str1,str2;
pDocument2->get_URL( &bstrURL );//获得url
str1=bstrURL;
pDocument2->get_title(&bstrURL);
str2=bstrURL;
AfxMessageBox(str1+str2);
//HWND hWnd = ::GetForegroundWindow();
//HWND hChildWnd = ::GetTopWindow(hWnd);
}
}
}
::CoUninitialize();
return nRetCode;
}
这个代码是可以得到网址,但是怎么知道最顶层的是IE浏览器,并且是我要求的网址呢?