Skip to content

Commit 09c3bd8

Browse files
authored
Update FindOnPage.md
1 parent 9d9ef6d commit 09c3bd8

1 file changed

Lines changed: 106 additions & 24 deletions

File tree

FindOnPage.md

Lines changed: 106 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,114 @@ completed or not, whether the match count has changed, and whether the match ind
1313
#### Description
1414
To initiate a find operation within a WebView2 control, developers can utilize the `StartFindOnPage` method.
1515
This method allows specifying the find term and configuring other find parameters using the `ICoreWebView2FindConfiguration` interface.
16-
When StartFind is called while a find session is currently active, it will navigate the active match index forwards or backwards depending
17-
on the direction specified within the find configuration.
1816

1917
#### Create/Specify a Find Configuration
2018
```cpp
21-
bool AppWindow::ConfigureAndExecuteFind(
22-
const std::wstring& searchTerm,
23-
bool caseSensitive,
24-
bool highlightAllMatches,
25-
COREWEBVIEW2_FIND_DIRECTION direction)
26-
{
27-
// Query for the ICoreWebView2Environment5 interface.
28-
auto webView2Environment5 = m_webViewEnvironment.try_query<ICoreWebView2Environment5>();
29-
CHECK_FEATURE_RETURN(webView2Environment5);
30-
31-
// Create the find configuration.
32-
wil::com_ptr<ICoreWebView2FindConfiguration> findConfiguration;
33-
CHECK_FAILURE(webView2Environment5->CreateFindConfiguration(&findConfiguration));
34-
35-
// Apply the find operation configurations.
36-
CHECK_FAILURE(findConfiguration->put_FindTerm(searchTerm.c_str()));
37-
CHECK_FAILURE(findConfiguration->put_IsCaseSensitive(caseSensitive));
38-
CHECK_FAILURE(findConfiguration->put_ShouldHighlightAllMatches(highlightAllMatches));
39-
CHECK_FAILURE(findConfiguration->put_FindDirection(direction));
40-
41-
// Proceed to execute the find operation with the configured settings.
42-
return ExecuteFindOperation(findConfiguration.get());
19+
//! [StartFindOnPage]
20+
bool AppWindow::ConfigureAndExecuteFind(const std::wstring& searchTerm)
21+
{
22+
// Query for the ICoreWebView2Environment5 interface.
23+
auto webView2Environment5 = m_webViewEnvironment.try_query<ICoreWebView2Environment5>();
24+
CHECK_FEATURE_RETURN(webView2Environment5);
25+
26+
// Initialize find configuration/settings
27+
wil::com_ptr<ICoreWebView2StagingFindConfiguration> findConfiguration;
28+
CHECK_FAILURE(webView2Environment5->CreateFindConfiguration(&findConfiguration));
29+
CHECK_FAILURE(findConfiguration->put_FindTerm(searchTerm.c_str()));
30+
CHECK_FAILURE(findConfiguration->put_IsCaseSensitive(false));
31+
CHECK_FAILURE(findConfiguration->put_ShouldMatchWord(false));
32+
CHECK_FAILURE(findConfiguration->put_FindDirection(COREWEBVIEW2_FIND_DIRECTION_FORWARD));
33+
34+
// Query for the ICoreWebView217 interface to access the Find feature.
35+
auto webView217 = m_webView.try_query<ICoreWebView217>();
36+
CHECK_FEATURE_RETURN(webView217);
37+
38+
// Get the Find interface.
39+
wil::com_ptr<ICoreWebView2Find> webView2find;
40+
CHECK_FAILURE(webView217->get_Find(&webView2find));
41+
42+
// Apply custom UI settings and highlight configurations.
43+
CHECK_FAILURE(webView2find->put_UseCustomUI(false)); // Assuming you want to use the default UI, adjust as necessary.
44+
CHECK_FAILURE(webView2find->put_ShouldHighlightAllMatches(true)); // This should match the passed parameter if dynamic.
45+
46+
47+
48+
// Start the find operation
49+
CHECK_FAILURE(webView2find->StartFind(
50+
findConfiguration.get(),
51+
Callback<ICoreWebView2StagingFindOperationCompletedHandler>(
52+
[this](HRESULT result, LONG ActiveIdx, LONG MatchesCount) -> HRESULT
53+
{
54+
if (SUCCEEDED(result))
55+
{
56+
// Handle successful find operation
57+
// For example, you could update UI elements here
58+
}
59+
else
60+
{
61+
// Handle errors
62+
}
63+
return S_OK;
64+
})
65+
.Get()));
66+
CHECK_FAILURE(webView2find->FindNext());
67+
CHECK_FAILURE(webView2find->FindNext());
68+
CHECK_FAILURE(webView2find->FindPrevious());
69+
70+
return true;
71+
}
72+
//! [StartFindOnPage]
73+
74+
//! [StopFind]
75+
bool AppWindow::StopFind()
76+
{
77+
auto webView2staging11 = m_webView.try_query<ICoreWebView2Staging11>();
78+
CHECK_FEATURE_RETURN(webView2staging11);
79+
wil::com_ptr<ICoreWebView2StagingFind> webView2stagingfind;
80+
CHECK_FAILURE(webView2staging11->get_Find(&webView2stagingfind));
81+
82+
return true;
83+
}
84+
//! [StopFind]
85+
86+
//! [GetMatchCount]
87+
bool AppWindow::GetMatchCount()
88+
{
89+
auto webView2staging11 = m_webView.try_query<ICoreWebView2Staging11>();
90+
CHECK_FEATURE_RETURN(webView2staging11);
91+
wil::com_ptr<ICoreWebView2StagingFind> webView2stagingfind;
92+
CHECK_FAILURE(webView2staging11->get_Find(&webView2stagingfind));
93+
LONG matchCount;
94+
CHECK_FAILURE(webView2stagingfind->get_MatchesCount(&matchCount));
95+
96+
// Update UI or handle matchCount as you wish
97+
// For example, you could show a message box
98+
std::wstring matchCountStr = L"Match Count: " + std::to_wstring(matchCount);
99+
MessageBox(m_mainWindow, matchCountStr.c_str(), L"Find Operation", MB_OK);
100+
101+
return true;
102+
}
103+
//! [GetMatchCount]
104+
105+
//! [GetActiveMatchIndex]
106+
bool AppWindow::GetActiveMatchIndex()
107+
{
108+
auto webView2staging11 = m_webView.try_query<ICoreWebView2Staging11>();
109+
CHECK_FEATURE_RETURN(webView2staging11);
110+
wil::com_ptr<ICoreWebView2StagingFind> webView2stagingfind;
111+
CHECK_FAILURE(webView2staging11->get_Find(&webView2stagingfind));
112+
LONG activeMatchIndex;
113+
CHECK_FAILURE(webView2stagingfind->get_ActiveMatchIndex(&activeMatchIndex));
114+
115+
// Update UI or handle activeMatchIndex as you wish
116+
// For example, you could show a message box
117+
std::wstring activeMatchIndexStr = L"Active Match Index: " + std::to_wstring(activeMatchIndex);
118+
MessageBox(m_mainWindow, activeMatchIndexStr.c_str(), L"Find Operation", MB_OK);
119+
120+
return true;
121+
}
122+
//! [GetActiveMatchIndex]
123+
#endif
43124
}
44125
```
45126
```csharp
@@ -303,6 +384,7 @@ bool AppWindow::FindNext()
303384
#### Description
304385
To navigate to the previous match found during a find operation within a WebView2 control, developers can use the `FindPrevious` method.
305386

387+
306388
```cpp
307389
//! [FindPrevious]
308390
bool AppWindow::FindPrevious()

0 commit comments

Comments
 (0)