線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:3049
推到 Plurk!
推到 Facebook!

ole連接和控制IE,兼談自動登錄chinabcb

 
jackkcg
站務副站長


發表:891
回覆:1050
積分:848
註冊:2002-03-23

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-09-26 16:15:06 IP:61.221.xxx.xxx 未訂閱
ole連接和控制IE,兼談自動登錄chinabcb http://www.chinabcb.com/bbs/viewtopic.php?t=8277&highlight=    以下爲本人的一些源代碼,發給大家,僅供參考了  全部程式均在bcb4.0(sp2)下編譯通過,用了theme控制項組  需在在頭文件中定義如下:  Variant __fastcall GetIeObject();  Variant __fastcall GetIeTrueObject();  Variant browser; //瀏覽器  HWND IeHWnd;  IHTMLDocument2* GetDocInterface(HWND hWnd); 代碼:    //auleaf的ole連接IE的實例  //如下連接爲sink IE控制項的事件的方法  //http://msdn.microsoft.com/library/default.asp?url=/workshop/browser/mshtml/tutorials/sink.asp  //Handling HTML Element Events  //---------------------------------------------------------------------------  #include  #include #include #include //#include #include #include #include #include "wininet.h"> #pragma hdrstop #include "callclient.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma link "ksthemebuttons" #pragma link "ksthemeengine" #pragma link "ksthemeforms" #pragma link "te_controls" #pragma link "SHDocVw_OCX" #pragma link "ksthemeedits" #pragma link "ksthemelabels" #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- //更詳細的資料請參考如下網址: //http://msdn.microsoft.com/library/default.asp?url=/workshop/browser/webbrowser/reference/objects/internetexplorer.asp //用ole來連接和控制IE的技術 void __fastcall TForm1::Button1Click(TObject *Sender) { try { /* CoInitialize(NULL); browser=Variant::CreateObject("InternetExplorer.Application"); browser.OlePropertySet("Width","800"); browser.OlePropertySet("Height","600"); browser.OlePropertySet("Visible","true"); browser.OleFunction("GoHome"); browser = Unassigned; */ browser = NULL; browser = GetIeTrueObject(); // browser.OlePropertySet("Width","800"); // browser.OlePropertySet("Height","600"); browser.OlePropertySet("Visible","true"); // Sleep(1000); // browser.OlePropertySet("FullScreen","true"); // browser.OleFunction("GoHome"); browser.OleProcedure("Navigate","http://www.chinabcb.com/bbs/login.php"); Sleep(5000); //你可以判斷它是不是忙來解決延時的問題 // IeHWnd = browser.OlePropertyGet("HWND"); //得到控制碼 Variant Doc = browser.OlePropertyGet("Document"); //得到當前html文檔物件 // Doc.OlePropertyGet("body").OleFunction("insertAdjacentHTML","AfterBegin","大家好!"); Doc.OlePropertyGet("body").OleFunction("insertAdjacentHTML","AfterBegin","大家好!auleaf"); Variant Tag_all = Doc.OlePropertyGet("all"); //得到腳本的所有物件,具體參考javascript語法 Variant Tag_para1 = Tag_all.OlePropertyGet("username"); Tag_para1.OlePropertySet("innerText",TeThemeEdit1->Text); //你的用戶名,請寫在此處 Variant Tag_para2 = Tag_all.OlePropertyGet("password"); Tag_para2.OlePropertySet("innerText",TeThemeEdit2->Text); //你的密碼,請寫在此處 Variant Tag_para3 = Tag_all.OlePropertyGet("login"); //登錄 Tag_para3.OleFunction("click"); /* Variant Tag_all = Doc.OlePropertyGet("all"); // Variant Tag_para2 = Tag_all.OleFunction("item","servicdid"); Variant Tag_para2 = Tag_all.OlePropertyGet("servicdid"); Tag_para2.OlePropertySet("innerText","101"); // Variant Tag_all1 = Doc.OlePropertyGet("all"); Variant Tag_para3 = Tag_all.OleFunction("item","passwordid"); Tag_para3.OlePropertySet("innerText","111"); // document.all.submitlogin.click(); Variant Tag_para4 = Tag_all.OlePropertyGet("submitlogin"); Tag_para4.OleFunction("click"); //利用腳本提交 //有些網頁的登錄是經過處理,用腳本來做的,你可以用獲得腳本的方法去做就可以了,像www.csdn.net Variant script = Doc.OlePropertyGet("Script"); Variant r = script.OleFunction("InquiryLoginSubmit"); //調用的Script函數名稱 Sleep(2000); // if(Doc.OlePropertyGet("Busy")<1)//忙碌中 Variant script1 = Doc.OlePropertyGet("Script"); Variant r1 = script.OleFunction("LoginSubmit"); //調用的Script函數名稱 Variant r2 = script.OleFunction("GuestInfo"); //調用的Script函數名稱 ShowMessage(VarToStr(r2)); */ } catch(...) { ShowMessage("IE Loading fail"); browser = Unassigned; } } //--------------------------------------------------------------------------- Variant __fastcall TForm1::GetIeTrueObject() { Variant Obj1=NULL; AnsiString AppName="InternetExplorer.Application" ; //也可以用guid HWND hPrevApp = NULL; hPrevApp = ::FindWindow("IEFrame", NULL);//可能失敗,需找到真正的IE ,可以 // if(!hPrevApp) // hPrevApp= FindWindow("CabinetWClass", NULL); if(!hPrevApp) //如果無IE存在 { Obj1 = Variant::CreateObject(AppName) ;//IE沒啓動就啓動它返回一自動化物件 // Obj1 = CreateOleObject(AppName) ;//IE沒啓動就啓動它返回一自動化物件 } else { HWND h1 = NULL; h1 = ::FindWindowEx(hPrevApp, 0, "Shell DocObject View", NULL); if (!h1) { Obj1 = Variant::CreateObject(AppName) ;//IE沒啓動就啓動它返回一自動化物件 } else { HWND h2 = NULL; h2 = ::FindWindowEx(h1, 0, "Internet Explorer_Server", NULL); //這才是真正的IE所在 if (h2) { Obj1 = Variant::GetActiveObject(AppName); //否則返回正在運行的實例自動化物件 } else { Obj1 = Variant::CreateObject(AppName) ;//IE沒啓動就啓動它返回一自動化物件 } } } // Obj1.OlePropertySet("FullScreen","true");//全屏 return Obj1; } Variant __fastcall TForm1::GetIeObject() { Variant Obj1; AnsiString AppName="InternetExplorer.Application" ; // HWND hPrevApp = ::FindWindow("IEFrame", NULL);//可能失敗,需找到真正的IE // if(!hPrevApp) { Obj1 = Variant::CreateObject(AppName) ;//IE沒啓動就啓動它返回一自動化物件 // Obj1 = CreateOleObject(AppName) ;//IE沒啓動就啓動它返回一自動化物件 } /* else { Obj1 = Variant::GetActiveObject(AppName); //否則返回正在運行的實例自動化物件 }*/ //Obj1.OlePropertySet("Visible",true); return Obj1; } //--------------------------------------------------------------------------- void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action) { browser = Unassigned; } //--------------------------------------------------------------------------- void __fastcall TForm1::TeThemeButton1Click(TObject *Sender) { HRESULT hret; hret=CoInitialize(NULL); assert(SUCCEEDED(hret)); CLSID clsid; hret=CLSIDFromProgID(L"InternetExplorer.Application",&clsid); assert(SUCCEEDED(hret)); IUnknown *pUnknown=0; hret=CoCreateInstance(clsid,NULL,CLSCTX_ALL,IID_IUnknown,reinterpret_cast(&pUnknown)); assert(SUCCEEDED(hret)); ::IWebBrowser2 *pWeb=0; // Query for IWebBrowser2 Interface hret=pUnknown->QueryInterface(::IID_IWebBrowser2,reinterpret_cast(&pWeb)); assert(SUCCEEDED(hret)); pUnknown->Release(); // Release the interface // Display IE full screen for 10 seconds pWeb->put_Visible(true); // Hidden usage of Variant class!!! pWeb->GoHome(); // Sleep(1000); // pWeb->Navigate2((tagVARIANT*)"http://192.168.100.7:8080/cati/inquirylogin.asp",NULL,NULL,NULL,NULL); // pWeb->put_FullScreen(true); Sleep(3000); // pWeb->put_Visible(false); // Hidden usage of Variant class!!! BSTR pp; pWeb->get_LocationURL(&pp); // ShowMessage((AnsiString)pp); IHTMLDocument2 *IHTMLDocument21; IDispatch *ppDisp1; pWeb->get_Document(&ppDisp1); if (!ppDisp1)return; HRESULT hr0 = ppDisp1->QueryInterface(IID_IHTMLDocument2,reinterpret_cast(&IHTMLDocument21)); if (FAILED(hr0)) { return; } CComPtr m_pBody; HRESULT hr1 = IHTMLDocument21->get_body(&m_pBody); if (FAILED(hr1)) { return; } // 獲得 HTML 文本 BSTR bstrHTMLText; HRESULT hr2 = m_pBody->get_outerHTML(&bstrHTMLText); // HRESULT hr2 = m_pBody->get_outerText(&bstrHTMLText); if (FAILED(hr2)) { return; } ShowMessage((AnsiString)bstrHTMLText); pWeb->Release(); CoUninitialize(); // CComPtrspWnd2; // CComPtrspServiceProv; // CComPtrspDoc; } //--------------------------------------------------------------------------- void __fastcall TForm1::TeThemeButton2Click(TObject *Sender) { Close(); } //--------------------------------------------------------------------------- IHTMLDocument2* TForm1::GetDocInterface(HWND hWnd) { // 我們需要顯示地裝載OLEACC.DLL,這樣我們才知道有沒有安裝MSAA HINSTANCE hInst = ::LoadLibrary( _T("OLEACC.DLL") ); IHTMLDocument2* pDoc2=NULL; if ( hInst != NULL ){ if ( hWnd != NULL ){ CComPtr spDoc=NULL; LRESULT lRes; /*由於WM_HTML_GETOBJECT非Windows標準消息,所以需要RegisterWindowMessage*/ UINT nMsg = ::RegisterWindowMessage( _T("WM_HTML_GETOBJECT") ); ::SendMessageTimeout( hWnd, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes ); /*取得ObjectFromLresult函數的位址*/ LPFNOBJECTFROMLRESULT pfObjectFromLresult = (LPFNOBJECTFROMLRESULT)::GetProcAddress( hInst, _T("ObjectFromLresult") ); if ( pfObjectFromLresult != NULL ){ HRESULT hr; hr=pfObjectFromLresult(lRes,IID_IHTMLDocument,0,(void**)&spDoc); if ( SUCCEEDED(hr) ){ CComPtr spDisp; IHTMLWindow2 *spWin; spDoc->get_Script( &spDisp ); // spWin = spDisp; // spWin->get_document( &pDoc2 ); } } } ::FreeLibrary(hInst); } else{//如果沒有安裝MSAA } return pDoc2; } //--------------------------------------------------------------------------- void __fastcall TForm1::TeThemeButton3Click(TObject *Sender) { // if (browser) // browser.OleProcedure("Navigate","http://192.168.100.7:8080/cati/weihu/mainlogin.html"); try { // OleInitialize(NULL); // WebBrowser1->Navigate((WideString)"http://127.0.0.1:8080/cati/index.asp"); } catch(...) { ShowMessage("error"); } } //--------------------------------------------------------------------------- void __fastcall TForm1::TeThemeButton4Click(TObject *Sender) { Variant IEbrowser; HWND hWnd; if( VarIsEmpty( IEbrowser ) ) { IEbrowser = CreateOleObject("InternetExplorer.Application"); } if(!(hWnd = FindWindow( "IEFrame", NULL ))) { IEbrowser = CreateOleObject( "InternetExplorer.Application" ); hWnd = FindWindow( "IEFrame", NULL ); } if(hWnd) { IEbrowser.OlePropertySet("Visible",true); ::SetForegroundWindow( hWnd ); IEbrowser.OleProcedure("Navigate","http://www.chinabcb.com"); } } //--------------------------------------------------------------------------- void __fastcall TForm1::TeThemeButton5Click(TObject *Sender) { /* AnsiString html = ""; AnsiString Url ="http://www.yahoo.com"; HINTERNET hSession; hSession = InternetOpen( "MyApp", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0 ); if( hSession ) { HINTERNET hService; hService = InternetOpenUrl( hSession, Url.c_str(), NULL, 0, 0, 0 ); if(hService) { while(1) { char lpBuffer[1024 1]; //一度に読み込むバイト數 DWORD dwBytesRead; //読み込んだbyte數 InternetReadFile(hService, lpBuffer, 1024, &dwBytesRead); if(dwBytesRead == 0) break; //読み込んだbyte數が0になったらループを抜ける lpBuffer[dwBytesRead] = 0; //読み込んだデータの最後に0を書き込み文字列化 html = lpBuffer; //htmlに追加 } } InternetCloseHandle(hService); } InternetCloseHandle(hSession); */ } //--------------------------------------------------------------------------- ********************************************************* 哈哈&兵燹 最會的2大絕招 這個不會與那個也不會 哈哈哈 粉好 Delphi K.Top的K.Top分兩個字解釋Top代表尖端的意思,希望本討論區能提供Delphi的尖端新知 K.表Knowlege 知識,就是本站的標語:Open our mind to make knowledge together! 希望能大家敞開心胸,將知識寶庫結合一起
------
**********************************************************
哈哈&兵燹
最會的2大絕招 這個不會與那個也不會 哈哈哈 粉好

Delphi K.Top的K.Top分兩個字解釋Top代表尖端的意思,希望本討論區能提供Delphi的尖端新知
K.表Knowlege 知識,就是本站的標語:Open our mind
dg822
一般會員


發表:14
回覆:38
積分:10
註冊:2004-12-16

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-12-29 17:59:42 IP:219.132.xxx.xxx 未訂閱
我想问一个问题:当打开不止一个IE,程式哪知道要连接哪个?
系統時間:2024-05-17 2:41:40
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!