试玩C++ 操作页面控件
生活随笔
收集整理的這篇文章主要介紹了
试玩C++ 操作页面控件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最近數字和金山吵的熱火朝天的,群里有人說網友的投票可能有工具刷出來的,覺得應該很有意思,就想自己試一下,玩了半天終于可以操作頁面進行投票了,但這個投票做了IP限制,所以工具也無用武之地啊!典型的需求沒做好,反正也是自己玩,把過程記下來下給自己備忘一下:
?
?
1:? 2: #include "stdafx.h" 3: #include <windows.h> 4: #include <string> 5: #include "gtest/gtest.h" 6: #include "BrwHelperTest.h" 7: //#include <wstring> 8:? 9:? 10:? 11: #include "EnumFormVal.h" 12:? 13: #include <atlbase.h> 14:? 15: CComModule _Module; // 由于要使用 CComDispatchDriver ATL的智能指針, 16: // 所以聲明它是必須的 17:? 18: #include <mshtml.h> // 所有 IHTMLxxxx 的接口聲明 19: #include <atlcom.h> 20:? 21: #ifdef _DEBUG 22: #define new DEBUG_NEW 23: #undef THIS_FILE 24: static char THIS_FILE[] = __FILE__; 25: #endif 26:? 27: / 28: // The one and only application object 29:? 30: using namespace std; 31:? 32: void EnumIE( void ); //枚舉瀏覽器函數 33: void EnumFrame( IHTMLDocument2 * pIHTMLDocument2 ); //枚舉子框架函數 34: void EnumForm ( IHTMLDocument2 * pIHTMLDocument2 ); //枚舉表單函數 35:? 36: bool bTrue = false; 37:? 38: int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]) 39: { 40: 41: // ShellExecute(NULL,L"open", L"http://tech.qq.com/zt2010/360pkduba",L"",L"", SW_SHOW ); 42:? 43:? 44: ::CoInitialize(NULL); //初始化 COM 公寓 45:? 46: int count = 0; 47: for(;;) 48: { 49: EnumIE(); //枚舉瀏覽器 50: printf("tou piao : %d \n", count++); 51: printf("========================================================================\n"); 52: } 53: 54:? 55: ::CoUninitialize(); //釋放 COM 公寓 56:? 57: cout << _T("======完成======") << endl; 58:? 59:? 60: return 0; 61: } 62:? 63: void EnumIE( void ) 64: { 65: cout << _T("開始掃描系統中正在運行的瀏覽器實例") << endl; 66:? 67: CComPtr< IShellWindows > spShellWin; 68: HRESULT hr = spShellWin.CoCreateInstance( CLSID_ShellWindows ); 69: if ( FAILED ( hr ) ) 70: { 71: cout << _T("獲取 IShellWindows 接口錯誤") << endl; 72: return; 73: } 74:? 75: long nCount = 0; // 取得瀏覽器實例個數(Explorer 和 IExplorer) 76: spShellWin->get_Count( &nCount ); 77: if( 0 == nCount ) 78: { 79: cout << _T("沒有在運行著的瀏覽器") << endl; 80: return; 81: } 82:? 83: for(int i=0; i<nCount; i++) 84: { 85: CComPtr< IDispatch > spDispIE; 86: hr=spShellWin->Item(CComVariant( (long)i ), &spDispIE ); 87: if ( FAILED ( hr ) ) continue; 88:? 89: CComQIPtr< IWebBrowser2 > spBrowser = spDispIE; 90: if ( !spBrowser ) continue; 91:? 92: CComPtr < IDispatch > spDispDoc; 93: hr = spBrowser->get_Document( &spDispDoc ); 94: if ( FAILED ( hr ) ) continue; 95:? 96: CComQIPtr< IHTMLDocument2 > spDocument2 = spDispDoc; 97: if ( !spDocument2 ) continue; 98:? 99: // 程序運行到此,已經找到了 IHTMLDocument2 的接口指針 100:? 101: // 刪除下行語句的注釋,把瀏覽器的背景改變看看 102: // spDocument2->put_bgColor( CComVariant( "green" ) ); 103: 104: EnumForm( spDocument2 ); //枚舉所有的表單 105: if( bTrue ) 106: { 107: return; 108: } 109: } 110: } 111:? 112: void EnumFrame( IHTMLDocument2 * pIHTMLDocument2 ) 113: { 114: if ( !pIHTMLDocument2 ) return; 115:? 116: HRESULT hr; 117:? 118: CComPtr< IHTMLFramesCollection2 > spFramesCollection2; 119: pIHTMLDocument2->get_frames( &spFramesCollection2 ); //取得框架frame的集合 120:? 121: long nFrameCount=0; //取得子框架個數 122: hr = spFramesCollection2->get_length( &nFrameCount ); 123: if ( FAILED ( hr ) || 0 == nFrameCount ) return; 124:? 125: for(long i=0; i<nFrameCount; i++) 126: { 127: CComVariant vDispWin2; //取得子框架的自動化接口 128: hr = spFramesCollection2->item( &CComVariant(i), &vDispWin2 ); 129: if ( FAILED ( hr ) ) continue; 130:? 131: CComQIPtr< IHTMLWindow2 > spWin2 = vDispWin2.pdispVal; 132: if( !spWin2 ) continue; //取得子框架的 IHTMLWindow2 接口 133:? 134: CComPtr < IHTMLDocument2 > spDoc2; 135: spWin2->get_document( &spDoc2 ); //取得字框架的 IHTMLDocument2 接口 136:? 137: EnumForm( spDoc2 ); //遞歸枚舉當前子框架 IHTMLDocument2 上的表單form 138:? 139: if( bTrue ) 140: { 141: return; 142: } 143: } 144: } 145:? 146: void EnumForm( IHTMLDocument2 * pIHTMLDocument2 ) 147: { 148: if( !pIHTMLDocument2 ) return; 149:? 150: EnumFrame( pIHTMLDocument2 ); //遞歸枚舉當前 IHTMLDocument2 上的子框架fram 151:? 152: HRESULT hr; 153: CComBSTR bstrTitle; 154: pIHTMLDocument2->get_title( &bstrTitle ); //取得文檔標題 155:? 156: USES_CONVERSION; 157: cout << _T("開始枚舉“") << OLE2CT( bstrTitle ) << _T("”的表單") << endl; 158: CComQIPtr< IHTMLElementCollection > spElementCollection; 159: hr = pIHTMLDocument2->get_forms( &spElementCollection ); //取得表單集合 160: if ( FAILED( hr ) ) 161: { 162: wcout << L"獲取表單的集合 IHTMLElementCollection 錯誤" << endl; 163: return; 164: } 165:? 166: long nFormCount=0; //取得表單數目 167: hr = spElementCollection->get_length( &nFormCount ); 168: if ( FAILED( hr ) ) 169: { 170: wcout << L"獲取表單數目錯誤" << endl; 171: return; 172: } 173: 174: for(long i=0; i<nFormCount; i++) 175: { 176: IDispatch *pDisp = NULL; //取得第 i 項表單 177: hr = spElementCollection->item( CComVariant( i ), CComVariant(), &pDisp ); 178: if ( FAILED( hr ) ) continue; 179:? 180: CComQIPtr< IHTMLFormElement > spFormElement = pDisp; 181: pDisp->Release(); 182:? 183: long nElemCount=0; //取得表單中 域 的數目 184: hr = spFormElement->get_length( &nElemCount ); 185: if ( FAILED( hr ) ) continue; 186:? 187: int count = 0; 188: for(long j=0; j<nElemCount; j++) 189: { 190: CComDispatchDriver spInputElement; //取得第 j 項表單域 191: hr = spFormElement->item( CComVariant( j ), CComVariant(), &spInputElement ); 192: if ( FAILED( hr ) ) continue; 193:? 194: CComVariant vName,vVal,vType; //取得表單域的 名,值,類型 195: hr = spInputElement.GetPropertyByName( L"name", &vName ); 196: if( FAILED( hr ) ) continue; 197: hr = spInputElement.GetPropertyByName( L"value", &vVal ); 198: if( FAILED( hr ) ) continue; 199: hr = spInputElement.GetPropertyByName( L"type", &vType ); 200: if( FAILED( hr ) ) continue; 201:? 202: LPCTSTR lpName = vName.bstrVal? 203: OLE2CT( vName.bstrVal ) : L"NULL"; //未知域名 204: LPCTSTR lpVal = vVal.bstrVal? 205: OLE2CT( vVal.bstrVal ) :L"NULL"; //空值,未輸入 206: LPCTSTR lpType = vType.bstrVal? 207: OLE2CT( vType.bstrVal ) : L"NULL"; //未知類型 208:? 209: wcout << L"[" << lpType << L"] "; 210: wcout << lpName << L" = " << lpVal << endl; 211: wstring typeradio = L"radio"; 212: if ( typeradio.compare(lpType) == 0) 213: { 214: count++; 215: if(count == 2 || 216: count == 5 || 217: count == 7 || 218: count == 10) 219: { 220: spInputElement.PutPropertyByName(OLESTR("checked"), &CComVariant(true)); 221: } 222: } 223:? 224: typeradio = L"submit"; 225: if ( typeradio.compare(lpType) == 0) 226: { 227: CComQIPtr< IHTMLElement > spSingleElement; 228: hr = spInputElement->QueryInterface( IID_IHTMLElement , (void**)&spSingleElement); 229: if( FAILED( hr ) ) 230: continue; 231: hr = spSingleElement->click(); 232: bTrue = true; 233: return; 234:? 235: } 236: } 237: } 238: }?
轉載于:https://www.cnblogs.com/GnagWang/archive/2010/06/05/1752174.html
總結
以上是生活随笔為你收集整理的试玩C++ 操作页面控件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 浅谈项目开发现状(一)
- 下一篇: 位枚举(Bit Flags)