본문 바로가기
코드

TShellWindows

by umaking 2008. 7. 14.

#include  
#include  

// IHTMLWindow2의 execScript을 이용한 자바호출 

void ExecScript(IHTMLDocument2* doc, String script) 
{ 
    if (!doc) 
        return; 

    IHTMLWindow2*  win; 
    HRESULT  hr = doc->get_parentWindow(&win); 
    if (SUCCEEDED(hr)) 
    { 
        try 
        { 
            VARIANT  v; 
            VariantInit(&v); 
            win->execScript( WideString(script), WideString("javascript"), &v ); 
            VariantClear(&v); 
        } 
        __finally 
        { 
            win->Release(); 
        } 
    } 
} 

// 자바스크립트 호출 테스트. 

void __fastcall TForm4::Button4Click(TObject *Sender) 
{ 
    TShellWindows* ShellWin = new TShellWindows(NULL); 
    IWebBrowser2* IE; 
    try 
    { 
        for(int i = 0; i < ShellWin->Count; i++) 
        { 
            if (ShellWin->Item(i) == NULL) 
                continue; 
            ShellWin->Item(i)->QueryInterface(IID_IWebBrowser2, (void **)&IE); 
            if (IE == NULL) 
                continue; 
            if (IE->LocationURL.SubString(1, 7) != "http://") 
                continue; 

            IHTMLDocument2* doc; 
            if (SUCCEEDED((IE->Document->QueryInterface(IID_IHTMLDocument2, (void**)&doc)))) 
            { 
                Form4->Memo1->Lines->Add(IE->LocationURL); 

                char  cmd[300]; 
                wsprintf(cmd, "alert('코드기어포럼 %s')", String(IE->LocationURL).c_str()); 
                ExecScript(doc, cmd); 
                doc->Release(); 
            } 
        } 
    } 
    __finally 
    { 
        delete ShellWin; 
    } 

}