@@ -16,6 +16,113 @@ This method allows specifying the find term and configuring other find parameter
1616
1717#### Create/Specify a Find Configuration
1818``` cpp
19+
20+ // ! [ConfigureAndExecuteFind]
21+ bool AppWindow::ConfigureAndExecuteFind (const std::wstring& searchTerm)
22+ {
23+ // Query for the ICoreWebView2Environment5 interface.
24+ auto webView2Environment5 = m_webViewEnvironment.try_query<ICoreWebView2Environment5 >();
25+ CHECK_FEATURE_RETURN(webView2Environment5);
26+
27+ // Initialize find configuration/settings
28+ wil::com_ptr<ICoreWebView2FindConfiguration> findConfiguration;
29+ CHECK_FAILURE(webView2Environment5->CreateFindConfiguration(&findConfiguration));
30+ CHECK_FAILURE(findConfiguration->put_FindTerm(searchTerm.c_str()));
31+ CHECK_FAILURE(findConfiguration->put_IsCaseSensitive(false));
32+ CHECK_FAILURE(findConfiguration->put_ShouldMatchWord(false));
33+ CHECK_FAILURE(findConfiguration->put_FindDirection(COREWEBVIEW2_FIND_DIRECTION_FORWARD));
34+
35+ // Query for the ICoreWebView217 interface to access the Find feature.
36+ auto webView217 = m_webView.try_query<ICoreWebView217>();
37+ CHECK_FEATURE_RETURN(webView217);
38+
39+ // Get the Find interface.
40+ wil::com_ptr<ICoreWebView2Find> webView2find;
41+ CHECK_FAILURE(webView217->get_Find(&webView2find));
42+
43+ // Determine if custom UI will be usedsettings and highlight configurations.
44+
45+ // Assuming you want to use the default UI, adjust as necessary.
46+ CHECK_FAILURE(webView2find->put_UseCustomUI(false));
47+ CHECK_FAILURE(webView2find->put_ShouldHighlightAllMatches(true));
48+
49+ // Start the find operation
50+ CHECK_FAILURE(webView2find->StartFind(
51+ findConfiguration.get(),
52+ Callback<ICoreWebView2FindOperationCompletedHandler>(
53+ [this](HRESULT result, LONG ActiveIdx, LONG MatchesCount) -> HRESULT
54+ {
55+ if (SUCCEEDED(result))
56+ {
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+ CHECK_FAILURE(webView2find->StopFind());
70+
71+ return true;
72+ }
73+ //! [ ConfigureAndExecuteFind]
74+
75+ //! [ StopFind]
76+ bool AppWindow::StopFind()
77+ {
78+ auto webView2staging11 = m_webView.try_query<ICoreWebView2Staging11 >();
79+ CHECK_FEATURE_RETURN(webView2staging11);
80+ wil::com_ptr<ICoreWebView2StagingFind > webView2stagingfind;
81+ CHECK_FAILURE(webView2staging11->get_Find(&webView2stagingfind));
82+
83+ return true;
84+ }
85+ //! [ StopFind]
86+
87+ //! [ GetMatchCount]
88+ bool AppWindow::GetMatchCount()
89+ {
90+ auto webView2staging11 = m_webView.try_query<ICoreWebView2Staging11 >();
91+ CHECK_FEATURE_RETURN(webView2staging11);
92+ wil::com_ptr<ICoreWebView2StagingFind > webView2stagingfind;
93+ CHECK_FAILURE(webView2staging11->get_Find(&webView2stagingfind));
94+ LONG matchCount;
95+ CHECK_FAILURE(webView2stagingfind->get_MatchesCount(&matchCount));
96+
97+ // Update UI or handle matchCount as you wish
98+ // For example, you could show a message box
99+ std::wstring matchCountStr = L"Match Count: " + std::to_wstring(matchCount);
100+ MessageBox(m_mainWindow, matchCountStr.c_str(), L"Find Operation", MB_OK);
101+
102+ return true;
103+ }
104+ //! [ GetMatchCount]
105+
106+ //! [ GetActiveMatchIndex]
107+ bool AppWindow::GetActiveMatchIndex()
108+ {
109+ auto webView2staging11 = m_webView.try_query<ICoreWebView2Staging11 >();
110+ CHECK_FEATURE_RETURN(webView2staging11);
111+ wil::com_ptr<ICoreWebView2StagingFind > webView2stagingfind;
112+ CHECK_FAILURE(webView2staging11->get_Find(&webView2stagingfind));
113+ LONG activeMatchIndex;
114+ CHECK_FAILURE(webView2stagingfind->get_ActiveMatchIndex(&activeMatchIndex));
115+
116+ // Update UI or handle activeMatchIndex as you wish
117+ // For example, you could show a message box
118+ std::wstring activeMatchIndexStr = L"Active Match Index: " + std::to_wstring(activeMatchIndex);
119+ MessageBox(m_mainWindow, activeMatchIndexStr.c_str(), L"Find Operation", MB_OK);
120+
121+ return true;
122+ }
123+ //! [ GetActiveMatchIndex]
124+ #endif
125+
19126 bool AppWindow::ConfigureAndExecuteFind(
20127 const std::wstring& searchTerm,
21128 bool caseSensitive,
@@ -39,8 +146,9 @@ This method allows specifying the find term and configuring other find parameter
39146 // Proceed to execute the find operation with the configured settings.
40147 return ExecuteFindOperation(findConfiguration.get());
41148 }
42- ```
43- ```csharp
149+ ```
150+
151+ ```csharp
44152 public async Task<bool> ConfigureAndExecuteFindAsync(
45153 string searchTerm,
46154 bool caseSensitive,
@@ -56,10 +164,10 @@ This method allows specifying the find term and configuring other find parameter
56164 };
57165 return await ExecuteFindOperationAsync(findConfiguration);
58166 }
59- ```
60- ### Start a Find Operation
167+ ```
168+ ### Start a Find Operation
61169
62- ```cpp
170+ ``` cpp
63171 // ! [StartFindOnPage]
64172 bool AppWindow::ExecuteFindOperation (ICoreWebView2FindConfiguration* configuration)
65173 {
@@ -98,8 +206,9 @@ This method allows specifying the find term and configuring other find parameter
98206 }
99207
100208 //! [ StartFindOnPage]
101- ```
102- ```csharp
209+ ```
210+
211+ ```csharp
103212 //! [StartFindOnPage]
104213 public async Task<bool> ExecuteFindOperationAsync(CoreWebView2FindConfiguration configuration)
105214 {
@@ -114,11 +223,11 @@ This method allows specifying the find term and configuring other find parameter
114223 }
115224
116225 //! [StartFindOnPage]
117- ```
118- ### Stop an existing find operation
119- #### Description
120- To stop an ongoing find operation within a WebView2 control, developers can use the `StopFind` method of the `ICoreWebView2Find` interface.
121- ```cpp
226+ ```
227+ ### Stop an existing find operation
228+ #### Description
229+ To stop an ongoing find operation within a WebView2 control, developers can use the ` StopFind ` method of the ` ICoreWebView2Find ` interface.
230+ ``` cpp
122231 // ! [StopFind]
123232 bool AppWindow::StopFind ()
124233 {
@@ -130,24 +239,25 @@ This method allows specifying the find term and configuring other find parameter
130239 return true;
131240 }
132241 //! [ StopFind]
133- ```
134- ```csharp
242+ ```
243+
244+ ```csharp
135245 //! [StopFind]
136246 public void StopFindOperation()
137247 {
138248 var webViewFind = webView.CoreWebView2.FindController; // Assuming webView is your WebView2 control
139249 webViewFind.StopFind();
140250 }
141251 //! [StopFind]
142- ```
252+ ```
143253
144254 ### Retrieve the Number of Matches
145255
146256 #### Description
147257 To retrieve the total number of matches found during a find operation within a WebView2 control, developers can utilize the `GetMatchCount` method.
148258
149259
150- ```cpp
260+ ``` cpp
151261 // ! [GetMatchCount]
152262 bool AppWindow::GetMatchCount ()
153263 {
@@ -166,8 +276,9 @@ This method allows specifying the find term and configuring other find parameter
166276 return true;
167277 }
168278 //! [ GetMatchCount]
169- ```
170- ```csharp
279+ ```
280+
281+ ```csharp
171282 //! [GetMatchCount]
172283 bool AppWindow::GetMatchCount()
173284 public async Task<int> GetMatchCountAsync()
@@ -178,7 +289,7 @@ This method allows specifying the find term and configuring other find parameter
178289 return matchCount;
179290 }
180291 //! [GetMatchCount]
181- ```
292+ ```
182293 #### Handle Match Count Changes
183294
184295 ```cpp
0 commit comments