WebBrowser中取对应的图片资源

在客户端中使用webbrower控件,控件中已显示网页,对应的图片资源应已下载完,下面从缓存中找到图片资源,两种方法都可

1、GetUrlCacheEntryInfo

http://msdn.microsoft.com/en-us/library/aa384185(v=vs.85).aspx

2、FindFirstUrlCacheEntry、FindNextUrlCacheEntry、FindCloseUrlCache

http://msdn.microsoft.com/zh-SG/library/aa384026

void CTesthaijiaDlg::OnDocumentComplete(LPDISPATCH pDisp, LPCTSTR szUrl)
{
    if (pDisp == m_pBrowserApp)
    {
        CString sCheckUrl(szUrl);
        if (sCheckUrl.CompareNoCase(sLoginUrl) == 0)
        {
            if (m_spHtmlDoc)
            {
                CDispatch disp = m_spHtmlDoc;
                disp.InvokePtr(L"getElementById", L"txtUserName").Put(L"value", L"abc");
                disp.InvokePtr(L"getElementById", L"txtPassword").Put(L"value", L"abc");

                  CDispatch dispImg = disp.InvokePtr(L"getElementById", L"ValidIMG");
                 CString s = dispImg.Get(L"src");
                if (1)
                {
                    LPINTERNET_CACHE_ENTRY_INFO lpCacheEntry = NULL;
                    DWORD dwEntrySize = 4096;
                    lpCacheEntry = (LPINTERNET_CACHE_ENTRY_INFO) new char[dwEntrySize];
                    lpCacheEntry->dwStructSize = dwEntrySize;
                    BOOL b = GetUrlCacheEntryInfo(s, lpCacheEntry, &dwEntrySize);
                    CString sPath = lpCacheEntry->lpszLocalFileName;

                    RECT rc1;
                    SystemParametersInfo(SPI_GETWORKAREA,0,(PVOID)&rc1,0);
                    SetWindowPos(NULL, 0, rc1.bottom - 4, 0, 0, SWP_NOSIZE);
                                        
                    CDlgCode dlg;
                    dlg.SetPath(sPath);
                    if (dlg.DoModal() == IDOK)
                    {
                        disp.InvokePtr(L"getElementById", L"txtIMGCode").Put(L"value", CComVariant(dlg.m_sCode));
                        disp.InvokePtr(L"getElementById", L"BtnLogin").Invoke(L"click");
                        SetWindowPos(NULL, 0, 0, 0, 0, SWP_NOSIZE);
                    }
                }
                if (0)
                {
                    LPINTERNET_CACHE_ENTRY_INFO lpCacheEntry = NULL;
                    HANDLE hCacheDir = NULL;
                    DWORD dwTrySize, dwEntrySize = 4096;
                    DWORD dwError = ERROR_INSUFFICIENT_BUFFER;
                    BOOL bSuccess, bDone = FALSE; //是否已经处理完,初始设为false
                    while(!bDone){
                        switch(dwError){
                        case ERROR_INSUFFICIENT_BUFFER:     
                            if(lpCacheEntry) 
                            {
                                delete [] lpCacheEntry;
                            }
                            lpCacheEntry = (LPINTERNET_CACHE_ENTRY_INFO) new char[dwEntrySize];
                            lpCacheEntry->dwStructSize = dwEntrySize;
                            dwTrySize = dwEntrySize;
                            if(!hCacheDir)
                            {//为空,说明第一次执行,需要找第一个
                                hCacheDir = FindFirstUrlCacheEntry(NULL,lpCacheEntry,&dwTrySize);
                                bSuccess = (hCacheDir != NULL);
                            }
                            else 
                            {
                                bSuccess = FindNextUrlCacheEntry(hCacheDir,lpCacheEntry,&dwTrySize);
                            }
                            if(!bSuccess){
                                dwError = GetLastError();
                                dwEntrySize = dwTrySize; //使用得到的新长度 
                            }
                            else 
                            {
                                dwError = ERROR_SUCCESS;
                            }
                            break; 
                            //we are done
                        case ERROR_NO_MORE_ITEMS:
                            bDone = TRUE;
                            break;
                            //we have got an entry
                        case ERROR_SUCCESS:
                            if(bSuccess && (NORMAL_CACHE_ENTRY & lpCacheEntry->CacheEntryType))
                            { //
                                CString sUrl1 = lpCacheEntry->lpszSourceUrlName;
                                if (sUrl1.Compare(s) == 0)
                                {
                                    //处理
                                }
                                //     CTime lastModified(lpCacheEntry->LastModifiedTime);
                                //     CTime lastAccessed(lpCacheEntry->LastAccessTime);
                                //a changed cookie - delete it!
                                //     if(lastModified >= m_time || lastAccessed >= m_time){
                                //const   CString   name   =   lpCacheEntry->lpszSourceUrlName;  
                                //const   CString   result   =   lastModified.Format("%I:%M:%S%p   %d   %b   %Y");  
                                //MessageBox   (NULL,   name   +   "   last   modified   "   +   result,   "Deleting cookie",   MB_OK);  
                                //DeleteUrlCacheEntry(lpCacheEntry->lpszSourceUrlName);
                            }
                            //get ready for next entry
                            dwTrySize = dwEntrySize;
                            if(!FindNextUrlCacheEntry(hCacheDir,lpCacheEntry,&dwTrySize))
                            {
                                dwError = GetLastError();
                                dwEntrySize = dwTrySize;   //使用得到的新长度
                            }
                            else 
                            {
                                dwError = ERROR_SUCCESS;
                            }
                            break;
                            //unknown error
                        default:
                            CString strError; strError.Format(_T("There is an error (%d) when trying deleting temporary internet files.\r\n"),   dwError);  
                            TRACE(strError);
                            bDone = TRUE;
                            break;   
                            if(bDone) 
                                break;
                            Sleep(10);  if(lpCacheEntry){ delete [] lpCacheEntry; lpCacheEntry = NULL;}
                            if(hCacheDir){ FindCloseUrlCache(hCacheDir); hCacheDir = NULL;}
                            break;
                        }
                    }
                }
            }
        }
    }

    CDHtmlDialog::OnDocumentComplete(pDisp, szUrl);
}

 

转载于:https://www.cnblogs.com/fatterbetter/p/4118453.html

你可能感兴趣的:(WebBrowser中取对应的图片资源)