- 
                Notifications
    You must be signed in to change notification settings 
- Fork 62
Open
Description
I can invoke Find by following the docs:
//************************************
// Method:    InitializeFindOptions
// FullName:  CWebBrowser::InitializeFindOptions
// Access:    public 
// Returns:   wil::com_ptr<ICoreWebView2FindOptions>
// Qualifier:
// Parameter: const std::wstring & findTerm
//************************************
wil::com_ptr<ICoreWebView2FindOptions> CWebBrowser::InitializeFindOptions(const std::wstring& findTerm)
{
	// Query for the ICoreWebView2Environment15 interface.
	auto webView2Environment15 = m_pImpl->m_webViewEnvironment.try_query<ICoreWebView2Environment15>();
	CHECK_FEATURE_RETURN_NULL(webView2Environment15);
	// Initialize Find options
	wil::com_ptr<ICoreWebView2FindOptions> find_options;
	CHECK_FAILURE(webView2Environment15->CreateFindOptions(&find_options));
	CHECK_FAILURE(find_options->put_FindTerm(findTerm.c_str()));
	return find_options;
}
//************************************
// Method:    ConfigureAndExecuteFind
// FullName:  CWebBrowser::ConfigureAndExecuteFind
// Access:    public 
// Returns:   bool
// Qualifier:
// Parameter: const std::wstring & findTerm
//************************************
bool CWebBrowser::ConfigureAndExecuteFind(const std::wstring& findTerm)
{
	auto find_options = InitializeFindOptions(findTerm);
	if (!find_options) {
		return false;
	}
	// Query for the ICoreWebView2_28 interface to access the Find feature.
	auto webView2Environment28 = m_pImpl->m_webView.try_query<ICoreWebView2_28>();
	CHECK_FEATURE_RETURN(webView2Environment28);
	// Get the Find interface.
	wil::com_ptr<ICoreWebView2Find> webView2Find;
	CHECK_FAILURE(webView2Environment28->get_Find(&webView2Find));
	// By default Find will use the default UI and highlight all matches. If you want different behavior
	// you can change the SuppressDefaultDialog and ShouldHighlightAllMatches properties here.
	// Start the Find operation with a callback for completion.
	CHECK_FAILURE(webView2Find->Start(
		find_options.get(),
		Callback<ICoreWebView2FindStartCompletedHandler>(
			[this](HRESULT result) -> HRESULT
			{
				if (SUCCEEDED(result))
				{
					// Optionally update UI elements here upon successful Find operation.
				}
				else
				{
					// Handle errors.
				}
				return S_OK;
			}).Get()));
	// End user interaction is handled via UI.
	return true;
}And then in my editor code I just call m_pWebBrowser->ConfigureAndExecuteFind(L"");.
But, in my context menu event handler I still need to use SendInput:
// ===============================================================
// Find (CTRL + F)
if (!m_itemFind || bFullInit)
{
	m_itemFind.reset();
	wil::com_ptr<IStream> iconStreamFind;
	CHECK_FAILURE(SHCreateStreamOnFileEx(
		theApp.GetCommonAppDataFolder() + L"WebView2\\find" + strIconSuffix, STGM_READ, FILE_ATTRIBUTE_NORMAL, FALSE,
		nullptr, &iconStreamFind));
	CString strMenuText;
	strMenuText.LoadString(IDS_STR_MENU_FIND);
	CHECK_FAILURE(webviewEnvironment->CreateContextMenuItem(
		strMenuText, iconStreamFind.get(),
		COREWEBVIEW2_CONTEXT_MENU_ITEM_KIND_COMMAND, &m_itemFind));
	CHECK_FAILURE(m_itemFind->add_CustomItemSelected(
		Callback<ICoreWebView2CustomItemSelectedEventHandler>(
			[](ICoreWebView2ContextMenuItem* sender, IUnknown* args)
			{
				// m_pWebBrowser->ConfigureAndExecuteFind(L"");
				// Create an array of generic keyboard INPUT structures
				std::vector<INPUT> vIP(4);
				for (int n = 0; n < 4; ++n)
				{
					vIP.at(n).type = INPUT_KEYBOARD;
					vIP.at(n).ki.wScan = 0;
					vIP.at(n).ki.time = 0;
					vIP.at(n).ki.dwFlags = 0; // 0 for key press
					vIP.at(n).ki.dwExtraInfo = 0;
				}
				vIP.at(0).ki.wVk = VK_CONTROL;
				vIP.at(1).ki.wVk = 'F';
				vIP.at(2).ki.wVk = 'F';
				vIP.at(2).ki.dwFlags = KEYEVENTF_KEYUP;
				vIP.at(3).ki.wVk = VK_CONTROL;
				vIP.at(3).ki.dwFlags = KEYEVENTF_KEYUP;
				SendInput(4, vIP.data(), sizeof(INPUT));
				return S_OK;
			})
		.Get(), nullptr));
}
CHECK_FAILURE(items->InsertValueAtIndex(itemsCount, m_itemFind.get()));
itemsCount++;
// ===============================================================Metadata
Metadata
Assignees
Labels
No labels