[C++] 纯文本查看 复制代码
DWORD WINAPI WebThreadProc(PVOID pParam)//获取网站数据线程{ hWeb = ::CreateDialog(hInst, MAKEINTRESOURCE(IDD_WEB), NULL, (DLGPROC)WebProc); ShowWindow(hWeb, SW_SHOW); UpdateWindow(hWeb); HWND hWnd = hWeb; HRESULT res1 = CreateCoreWebView2EnvironmentWithOptions(nullptr, nullptr, nullptr, Callback<ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler>( [hWnd](HRESULT result1, ICoreWebView2Environment* env1) -> HRESULT {env1->CreateCoreWebView2Controller(hWnd, Callback<ICoreWebView2CreateCoreWebView2ControllerCompletedHandler>( [hWnd](HRESULT result2, ICoreWebView2Controller* controller2) -> HRESULT { if (controller2 != nullptr) { webviewController2 = controller2; webviewController2->get_CoreWebView2(&webviewWindow2); } ICoreWebView2Settings* Settings2; webviewWindow2->get_Settings(&Settings2); Settings2->put_IsScriptEnabled(TRUE); Settings2->put_AreDefaultScriptDialogsEnabled(TRUE); Settings2->put_IsWebMessageEnabled(TRUE); ICoreWebView2Settings4* Settings4; Settings4 = (ICoreWebView2Settings4*)Settings2; Settings4->put_IsGeneralAutofillEnabled(TRUE); Settings4->put_IsPasswordAutosaveEnabled(TRUE); RECT bounds2; GetClientRect(hWnd, &bounds2); bounds2.left = bounds2.right / 2 + 1; webviewController2->put_Bounds(bounds2); WCHAR wCookie[] = L"填入Cookie"; SetCookie(L".smzdm.com", wCookie); webviewWindow2->add_NavigationCompleted(Callback<ICoreWebView2NavigationCompletedEventHandler>( [hWnd](ICoreWebView2* webview2, ICoreWebView2NavigationCompletedEventArgs* args2) -> HRESULT { wil::unique_cotaskmem_string uri; webviewWindow2->get_Source(&uri); if (lstrcmp(uri.get(), L"https://www.smzdm.com/") == 0)//签到 { webviewWindow2->ExecuteScript(L"$('.J_punch').trigger('click');document.getElementsByClassName(\"J_punch\")[0].innerText;", Callback<ICoreWebView2ExecuteScriptCompletedHandler>( [hWnd](HRESULT error2, PCWSTR result2) -> HRESULT { if(lstrstr(result2,L"已")==0) { MessageBox(NULL, L"Cookie已过期,请更新!", L"错误", MB_OK); } return S_OK; }).Get()); }} return S_OK; }).Get(), &m_navCompletedToken2); webviewWindow2->Navigate(L"https://www.smzdm.com"); return S_OK; }).Get()); return S_OK; }).Get()); SetTimer(hWeb, 1, 33, NULL); MSG msg; while (GetMessage(&msg, nullptr, 0, 0)) { if (!IsDialogMessage(hWeb, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } } return 0;}void SetCookie(LPCWSTR uri,LPWSTR wCookie)//写入Cookie{ auto webview2_2 = webviewWindow2.try_query<ICoreWebView2_2>(); ICoreWebView2CookieManager* m_cookieManager; webview2_2->get_CookieManager(&m_cookieManager); if (m_cookieManager) { LPWSTR cStart = wCookie; while (cStart) { LPWSTR cValue = https://www.52pojie.cn/lstrstr(cStart, L"=");'\0'; LPWSTR cName = cStart; cValue += 1; cStart = lstrstr(cValue, L";"); if (cStart != NULL) { cStart[0] = L'\0'; cStart += 2; } ICoreWebView2Cookie* cookie; m_cookieManager->CreateCookie(cName, cValue, uri, L"/", &cookie); m_cookieManager->AddOrUpdateCookie(cookie); } else cStart = NULL; } }}