2013-10-09 56 views
1

如果它是C++中的Web瀏覽器,我需要獲取活動窗口地址欄的內容。我已經想出瞭如何抓住標題,並可以抓住記事本的內容,但我陷入了瀏覽器。如何獲取C++中瀏覽器地址欄的內容?

我的目標是讓這項工作適用於IE,Chrome和Firefox。如果這需要不同的方法,我會讓程序嘗試每一個,直到返回數據。

這是我到目前爲止有:

HWND foreground = GetForegroundWindow(); 
    HWND hwndEdit = FindWindowEx(foreground, NULL, "EDIT", NULL); 

    const int bufferSize = 5024; 
    char textBuffer[bufferSize] = ""; 
    SendMessage(hwndEdit, WM_GETTEXT, (WPARAM)bufferSize, (LPARAM)textBuffer); 

    cout << textBuffer; 
+2

我很抱歉讓你們失望,但是這一切都依賴於瀏覽器。 Internet Explorer很容易,因爲它的確是編輯框(當然這是可以改變的)。然而,FireFox有自己的顯示方式,當我在一個(未完成的)這樣的項目,我們無法通過簡單地找到一個窗口得到它。然而,FireFox有一套特殊的功能來獲取地址欄的內容(不記得那個)。我希望你能得到很好的答案,我也可以在將來使用。祝你好運! – Grzegorz

+0

@Grzegorz我有一種感覺,情況就是這樣,我只是需要點什麼來指出我正確的方向,所以我可以找到每個普通瀏覽器的方法。 –

+0

我現在應該指出,它不適用於我列出的任何瀏覽器。 –

回答

1

所以看起來我有這方面的工作了IE瀏覽器,在Firefox和Chrome仍在工作。

這裏是IE

HWND foreground = GetForegroundWindow(); 
    HWND hwndEdit = FindWindowEx(foreground, NULL, "EDIT", NULL); 

    HWND handle = FindWindowEx(foreground, NULL, "WorkerW", "Navigation Bar"); 
    if (NULL != handle) 
    { 
    handle = FindWindowEx(handle, NULL, "ReBarWindow32", NULL); 
    if (NULL != handle) 
    { 
    handle = FindWindowEx(handle, NULL, "Address Band Root", NULL); 
    }} 
    HWND hwndEdit = FindWindowEx(handle, NULL, "Edit", NULL); 

    const int bufferSize = 5024; 
    Char textBuffer[bufferSize] = ""; 
    SendMessage(hwndEdit, WM_GETTEXT, (WPARAM)bufferSize, (LPARAM)textBuffer); 

    string addressbartext = textBuffer; 
    if(addressbartext == "AutoCompleteProxy") 
    {addressbartext = "";} 
    else 
    {addressbartext = textBuffer; 
    } 

    cout << addressbartext; 
2

另一種工作方法,例如IE工作:

#include <stdio.h> 
#include <wchar.h> 
#include <Windows.h> 
#include <Exdisp.h> 

//This imports the shell extionsions 
//we disable warnings of multiple defines 
#pragma warning(disable: 4192) 
#pragma warning(disable: 4146) 
#import <mshtml.tlb> 
#import <shdocvw.dll> 

void PrintBrowserInfo(IWebBrowser2 *pBrowser) { 
    //These functions return Unicode strings. 
    BSTR bstr; 
    //Get the window title 
    pBrowser->get_LocationName(&bstr); 
    wprintf(L"Title: %s\n", bstr); 
    SysFreeString(bstr); 
    //Detect if this is Windows Explorer (My Computer) or Internet Explorer (the internet) 
    IDispatchPtr spDisp; 
    char *type = "Windows Explorer"; 
    if (pBrowser->get_Document(&spDisp) == S_OK && spDisp != NULL) { 
     MSHTML::IHTMLDocument2Ptr spHtmlDocument(spDisp); 
     if (spHtmlDocument != NULL) { 
      MSHTML::IHTMLElementPtr spHtmlElement; 
      spHtmlDocument->get_body(&spHtmlElement); 
      if (spHtmlElement != NULL) { 
       type = "Internet Explorer"; 
      } 
     } 
     spDisp.Release(); 
    } 
    printf(" Type: %s\n", type); 
    //Get the URL of the folder or web page 
    pBrowser->get_LocationURL(&bstr); 
    wprintf(L" URL: %s\n\n", bstr); 
    SysFreeString(bstr); 
} 

int main() { 
    CoInitialize(NULL); //We need COM 
    SHDocVw::IShellWindowsPtr spSHWinds; 
    IDispatchPtr spDisp; 
    //Find all explorer (Windows and Internet) and list them 
    if (spSHWinds.CreateInstance(__uuidof(SHDocVw::ShellWindows)) == S_OK) { 
     long nCount = spSHWinds->GetCount(); 
     for (long i = 0; i < nCount; i++) { 
      _variant_t va(i, VT_I4); 
      spDisp = spSHWinds->Item(va); 
      SHDocVw::IWebBrowser2Ptr spBrowser(spDisp); 
      if (spBrowser != NULL) { 
       //spBrowser->AddRef(); 
       PrintBrowserInfo((IWebBrowser2 *)spBrowser.GetInterfacePtr()); 
       spBrowser.Release(); 
      } 
     } 
    } else { 
     puts("Shell windows failed to initialise"); 
    } 
    system("PAUSE"); 
    return 0; 
} 
+0

經過測試的工作方法。 – kirill

相關問題