@@ -13,53 +13,87 @@ To initiate a find operation within a WebView2 control, developers can utilize t
1313This method allows specifying the find term and configuring other find parameters using the ` ICoreWebView2FindConfiguration ` interface.
1414
1515#### Create/Specify a Find Configuration
16-
17-
18-
1916``` cpp
20- ICoreWebView2FindConfiguration CreateFindConfiguration ()
17+ bool AppWindow::ConfigureAndExecuteFind (
18+ const std::wstring& searchTerm,
19+ bool caseSensitive,
20+ bool highlightAllMatches,
21+ COREWEBVIEW2_FIND_DIRECTION direction)
2122{
22- ICoreWebView2Environment5 environment = webView2Control.GetEnvironment();
23- ICoreWebView2FindConfiguration configuration;
24- environment.CreateFindConfiguration(out configuration);
25- configuration.FindTerm = "example";
26- configuration.FindDirection = COREWEBVIEW2_FIND_DIRECTION.COREWEBVIEW2_FIND_DIRECTION_FORWARD;
27- configuration.IsCaseSensitive = false;
28- configuration.ShouldMatchWord = false;
29- return configuration;
23+ // Query for the ICoreWebView2StagingEnvironment5 interface.
24+ auto webView2Environment5 = m_webViewEnvironment.try_query<ICoreWebView2StagingEnvironment5 >();
25+ CHECK_FEATURE_RETURN(webView2Environment5);
26+
27+ // Create the find configuration.
28+ wil::com_ptr<ICoreWebView2StagingFindConfiguration> findConfiguration;
29+ CHECK_FAILURE(webView2Environment5->CreateFindConfiguration(&findConfiguration));
30+
31+ // Apply the find operation configurations.
32+ CHECK_FAILURE(findConfiguration->put_FindTerm(searchTerm.c_str()));
33+ CHECK_FAILURE(findConfiguration->put_IsCaseSensitive(caseSensitive));
34+ CHECK_FAILURE(findConfiguration->put_ShouldHighlightAllMatches(highlightAllMatches));
35+ CHECK_FAILURE(findConfiguration->put_FindDirection(direction));
36+
37+ // Proceed to execute the find operation with the configured settings.
38+ return ExecuteFindOperation(findConfiguration.get());
3039}
3140```
3241
3342### Start a Find Operation
3443
35-
3644```cpp
3745//! [StartFindOnPage]
38- void AppWindow::StartFindOnPage (const std::wstring& findTerm )
46+ bool AppWindow::ExecuteFindOperation(ICoreWebView2StagingFindConfiguration* configuration )
3947{
40- // Get the find interface
41- auto find = webView2Control.GetFind();
42-
43- // Create find configuration
44- auto configuration = CreateFindConfiguration();
45-
46- // Start the find operation
47- find.StartFind(configuration, OnFindOperationCompleted);
48+ // Query for the ICoreWebView2Staging17 interface to access the Find feature.
49+ auto webView2staging17 = m_webView.try_query<ICoreWebView2Staging17>();
50+ CHECK_FEATURE_RETURN(webView2staging17);
51+
52+ // Get the Find interface.
53+ wil::com_ptr<ICoreWebView2StagingFind> webView2stagingfind;
54+ CHECK_FAILURE(webView2staging17->get_Find(&webView2stagingfind));
55+
56+ // Apply custom UI settings and highlight configurations.
57+ CHECK_FAILURE(webView2stagingfind->put_UseCustomUI(false)); // Assuming you want to use the default UI, adjust as necessary.
58+ CHECK_FAILURE(webView2stagingfind->put_ShouldHighlightAllMatches(true)); // This should match the passed parameter if dynamic.
59+ CHECK_FAILURE(webView2stagingfind->PassHighlightSettings());
60+
61+ // Start the find operation with the configured findConfiguration.
62+ HRESULT result = webView2stagingfind->StartFind(
63+ configuration,
64+ Callback<ICoreWebView2StagingFindOperationCompletedHandler>(
65+ [this](HRESULT result, LONG ActiveIdx, LONG MatchesCount) -> HRESULT
66+ {
67+ if (SUCCEEDED(result))
68+ {
69+ // Handle successful find operation
70+ // For example, updating UI elements to reflect the find results
71+ }
72+ else
73+ {
74+ // Handle errors appropriately
75+ }
76+ return S_OK;
77+ }).Get());
78+
79+ return SUCCEEDED(result);
4880}
81+
4982//! [StartFindOnPage]
5083```
5184### Stop an existing find operation
5285#### Description
5386To stop an ongoing find operation within a WebView2 control, developers can use the ` StopFind ` method of the ` ICoreWebView2Find ` interface.
5487``` cpp
5588// ! [StopFind]
56- void AppWindow::StopFind()
89+ bool AppWindow::StopFind ()
5790{
58- // Get the find interface
59- auto find = webView2Control.GetFind();
60-
61- // Stop the find operation
62- find.StopFind();
91+ auto webView2staging17 = m_webView.try_query<ICoreWebView2Staging17>();
92+ CHECK_FEATURE_RETURN (webView2staging17);
93+ wil::com_ptr<ICoreWebView2StagingFind > webView2stagingfind;
94+ CHECK_FAILURE(webView2staging17->get_Find(&webView2stagingfind));
95+ CHECK_FAILURE(webView2stagingfind->StopFind());
96+ return true;
6397}
6498// ! [StopFind]
6599```
@@ -72,16 +106,21 @@ To retrieve the total number of matches found during a find operation within a W
72106
73107``` cpp
74108// ! [GetMatchCount]
75- void AppWindow::GetMatchCount ()
109+ bool AppWindow::GetMatchCount ()
76110{
77- // Get the find interface
78- auto find = webView2Control.GetFind( );
79-
80- // Get the match count
111+ auto webView2staging17 = m_webView.try_query<ICoreWebView2Staging17>();
112+ CHECK_FEATURE_RETURN (webView2staging17 );
113+ wil::com_ptr< ICoreWebView2StagingFind > webView2stagingfind;
114+ CHECK_FAILURE(webView2staging17->get_Find(&webView2stagingfind));
81115 LONG matchCount;
82- find.GetMatchesCount(&matchCount);
116+ CHECK_FAILURE(webView2stagingfind->get_MatchesCount(&matchCount));
117+
118+ // Update UI or handle matchCount as you wish
119+ // For example, you could show a message box
120+ std::wstring matchCountStr = L"Match Count: " + std::to_wstring(matchCount);
121+ MessageBox (m_mainWindow, matchCountStr.c_str(), L"Find Operation", MB_OK);
83122
84- // Update UI or handle matchCount
123+ return true;
85124}
86125// ! [GetMatchCount]
87126```
@@ -94,16 +133,22 @@ Developers can retrieve the index of the currently active match within a WebView
94133
95134``` cpp
96135// ! [GetActiveMatchIndex]
97- void AppWindow::GetActiveMatchIndex ()
136+ bool AppWindow::GetActiveMatchIndex ()
98137{
99- // Get the find interface
100- auto find = webView2Control.GetFind( );
101-
102- // Get the active match index
138+ auto webView2staging17 = m_webView.try_query<ICoreWebView2Staging17>();
139+ CHECK_FEATURE_RETURN (webView2staging17 );
140+ wil::com_ptr< ICoreWebView2StagingFind > webView2stagingfind;
141+ CHECK_FAILURE(webView2staging17->get_Find(&webView2stagingfind));
103142 LONG activeMatchIndex;
104- find.GetActiveMatchIndex(&activeMatchIndex);
143+ CHECK_FAILURE(webView2stagingfind->get_ActiveMatchIndex(&activeMatchIndex));
144+
145+ // Update UI or handle activeMatchIndex as you wish
146+ // For example, you could show a message box
147+ std::wstring activeMatchIndexStr =
148+ L"Active Match Index: " + std::to_wstring(activeMatchIndex);
149+ MessageBox (m_mainWindow, activeMatchIndexStr.c_str(), L"Find Operation", MB_OK);
105150
106- // Update UI or handle activeMatchIndex
151+ return true;
107152}
108153// ! [GetActiveMatchIndex]
109154```
@@ -116,13 +161,18 @@ To navigate to the next match found during a find operation within a WebView2 co
116161
117162``` cpp
118163// ! [FindNext]
119- void AppWindow::FindNext ()
164+ bool AppWindow::FindNext ()
120165{
121- // Get the find interface
122- auto find = webView2Control.GetFind();
166+ auto webView2staging17 = m_webView.try_query<ICoreWebView2Staging17>();
167+ CHECK_FEATURE_RETURN (webView2staging17);
168+ wil::com_ptr<ICoreWebView2StagingFind > webView2stagingfind;
169+ CHECK_FAILURE(webView2staging17->get_Find(&webView2stagingfind));
123170
124- // Find the next occurrence
125- find.FindNext();
171+ CHECK_FAILURE (webView2stagingfind->FindNext());
172+ CHECK_FAILURE(webView2stagingfind->remove_ActiveMatchIndexChanged(
173+ m_ActiveMatchIndexChangedEventToken));
174+
175+ return true;
126176}
127177// ! [FindNext]
128178```
@@ -135,20 +185,23 @@ To navigate to the previous match found during a find operation within a WebView
135185
136186``` cpp
137187// ! [FindPrevious]
138- void AppWindow::FindPrevious ()
188+ bool AppWindow::FindPrevious ()
139189{
140- // Get the find interface
141- auto find = webView2Control.GetFind();
190+ auto webView2staging17 = m_webView.try_query<ICoreWebView2Staging17>();
191+ CHECK_FEATURE_RETURN (webView2staging17);
192+ wil::com_ptr<ICoreWebView2StagingFind > webView2stagingfind;
193+ CHECK_FAILURE(webView2staging17->get_Find(&webView2stagingfind));
194+
195+ CHECK_FAILURE (webView2stagingfind->FindPrevious());
142196
143- // Find the previous occurrence
144- find.FindPrevious();
197+ return true;
145198}
146199// ! [FindPrevious]
147200```
148201
149- ```
150202## API Details
151203``` csharp
204+
152205/// Specifies the direction of find Parameters.
153206 [v1_enum ]
154207typedef enum COREWEBVIEW2_FIND_DIRECTION {
@@ -161,58 +214,46 @@ typedef enum COREWEBVIEW2_FIND_DIRECTION {
161214// Interface defining the find configuration.
162215 [uuid (4A6ED732 - DF08 - 4449 - 8949 - 3632A4DEBFCD ), object , pointer_default (unique )]
163216interface ICoreWebView2FindConfiguration : IUnknown {
217+
164218 // Gets or sets the find term used for the find operation.
165219 [propget ] HRESULT FindTerm ([out , retval ] LPWSTR* value);
166- [propput] HRESULT FindTerm([in] LPCWSTR value);
220+ [propput ] HRESULT FindTerm ([in ] LPCWSTR value );
221+
167222 // Gets or sets the direction of the find operation (forward or backward).
168223 [propget ] HRESULT FindDirection ([out , retval ] COREWEBVIEW2_FIND_DIRECTION* value);
169- [propput] HRESULT FindDirection([in] COREWEBVIEW2_FIND_DIRECTION value);
224+ [propput ] HRESULT FindDirection ([in ] COREWEBVIEW2_FIND_DIRECTION value );
225+
170226 // Gets or sets whether the find operation is case sensitive.
171227 [propget ] HRESULT IsCaseSensitive ([out , retval ] BOOL* value);
172- [propput] HRESULT IsCaseSensitive([in] BOOL value);
228+ [propput ] HRESULT IsCaseSensitive ([in ] BOOL value );
229+
173230 // Gets or sets whether to only match whole words during the find operation.
174231 [propget ] HRESULT ShouldMatchWord ([out , retval ] BOOL* value);
175- [propput] HRESULT ShouldMatchWord([in] BOOL value);
232+ [propput ] HRESULT ShouldMatchWord ([in ] BOOL value );
233+
176234 // Gets the state of whether all matches are highlighted.
177235 // \return TRUE if all matches are highlighted, FALSE otherwise.
178236 [propget ] HRESULT ShouldHighlightAllMatches ([out , retval ] BOOL* value);
179237 // Sets the state to either highlight all matches or not.
180- [propput] HRESULT ShouldHighlightAllMatches([in] BOOL value);
181- // Determines if the currently active match is highlighted.
182- // \return TRUE if the active match is highlighted, FALSE otherwise.
183- [propget] HRESULT ShouldHighlightActiveMatch([out, retval] BOOL* value);
184- // Sets whether to highlight the currently active match.
185- [propput] HRESULT ShouldHighlightActiveMatch([in] BOOL value);
238+ [propput ] HRESULT ShouldHighlightAllMatches ([in ] BOOL value );
239+
186240 // Checks if a custom user interface is desired by end developer.
187241 // If TRUE, the default Find bar is not displayed.
188- // \return TRUE if using a custom UI, FALSE if using the default.
189- [propget] HRESULT UseCustomUI ([out, retval] BOOL* value);
242+ // Returns TRUE if using a custom UI, FALSE if using the default.
243+ [propget ] HRESULT SuppressDefaultDialog ([out , retval ] BOOL* value);
190244 // Sets whether to use a custom UI for the Find operation.
191- [propput] HRESULT UseCustomUI([in] BOOL value);
245+ [propput ] HRESULT SuppressDefaultDialog ([in ] BOOL value );
246+
192247 // Gets the index of the currently active match.
193- // If there's no active find session but an attempt is made to change the active match index:
194- // The function might crash if the global_match_id isn't found in the map.
195- // It will clear any active match highlighting if there's a previously active frame.
196- // It will either select a new active match or return an error through the callback if the frame associated with the match isn't valid.
197- // \return The active match index.
248+ // If there's no active find session but an attempt is made to get the active match index:
249+ // The function will return -1.
250+ // Returns The active match index.
198251 [propget ] HRESULT ActiveMatchIndex ([out , retval ] LONG* value);
199- // Sets the index for the active match.
200- [propput] HRESULT ActiveMatchIndex([in] LONG value);
252+
201253 // Gets the total count of matches found in the current document based on the last find criteria.
202254 // \return The total count of matches.
203255 [propget ] HRESULT MatchesCount ([out , retval ] LONG* value);
204- /**
205- * Passes the current highlighting settings to the underlying Mojo.
206- *
207- * This function retrieves the current text highlighting settings set by the user
208- * or the default system and ensures that they are used during any subsequent text
209- * find or highlight operations. This includes settings related to highlighting
210- * all matches, the active match, and any custom UI preferences.
211- *
212- * Users should call this function after changing any highlight settings to ensure
213- * that they are applied properly in the system.
214- */
215- HRESULT PassHighlightSettings();
256+
216257}
217258
218259/// Handles the event that's fired when the match count changes.
@@ -226,8 +267,8 @@ interface ICoreWebView2FindMatchCountChangedEventHandler : IUnknown {
226267 [uuid (623EFBF9 - A19E - 43C4 - B309 - D578511D24A9 ), object , pointer_default (unique )]
227268interface ICoreWebView2FindActiveMatchIndexChangedEventHandler : IUnknown {
228269 /// Provides the event args when the active match index changes.
229- /// \param sender The sender of this event, representing the current instance of ICoreWebView2Find.
230- /// \param args The event args that contain the new active match index.
270+ /// Parameter is a sender The sender of this event, representing the current instance of ICoreWebView2Find.
271+ /// Parameter is a args The event args that contain the new active match index.
231272 HRESULT Invoke (
232273 [in ] ICoreWebView2* sender,
233274 [in ] ICoreWebView2FindActiveMatchIndexChangedEventArgs* args);
@@ -241,36 +282,42 @@ interface ICoreWebView2FindOperationCompletedHandler : IUnknown {
241282
242283// Interface providing methods and properties for finding and navigating through text in the web view.
243284 [uuid (7C49A8AA - 2A17 - 4846 - 8207 - 21D1520AABC0 ), object , pointer_default (unique )]
244- interface ICoreWebView2Find : IUnknown {
285+ interface ICoreWebView2Find : IUnknown {
286+
245287 // Initiates a find using the specified configuration.
246288 HRESULT StartFind ([in ] ICoreWebView2FindConfiguration* configuration,
247289 ICoreWebView2FindOperationCompletedHandler* handler);
290+
248291 // Navigates to the next match in the document.
249- HRESULT FindNext();
292+ HRESULT FindNext ();
293+
250294 // Navigates to the previous match in the document.
251- HRESULT FindPrevious();
295+ HRESULT FindPrevious ();
296+
252297 // Stops the current 'Find' operation and hides the Find bar.
253- HRESULT StopFind();
298+ HRESULT StopFind ();
299+
254300 // Registers an event handler for the FindCompleted event.
255301 // This event is raised when a find operation completes, either by finding all matches, navigating to a match, or by being stopped.
256- // \param eventHandler The event handler to be added.
257- // \return A token representing the added event handler. This token can be used to unregister the event handler.
302+ // Parameter is the event handler to be added.
303+ // Returns a token representing the added event handler. This token can be used to unregister the event handler.
258304 HRESULT add_MatchCountChanged (
259305 [in ] ICoreWebView2FindMatchCountChangedEventHandler* eventHandler,
260306 [out ] EventRegistrationToken* token);
261307 // Unregisters an event handler from the MatchCountChanged event.
262- // \param token The token of the event handler to be removed, obtained during the registration of the event handler.
308+ // Parameter is the token of the event handler to be removed, obtained during the registration of the event handler.
263309 HRESULT remove_MatchCountChanged ([in ] EventRegistrationToken token );
310+
264311 // Registers an event handler for the ActiveMatchIndexChanged event.
265312 // This event is raised when the index of the currently active match changes.
266313 // This can happen when the user navigates to a different match or when the active match is changed programmatically.
267- // \param eventHandler The event handler to be added.
268- // \return A token representing the added event handler. This token can be used to unregister the event handler.
314+ // Parameter is the event handler to be added.
315+ // Returns a token representing the added event handler. This token can be used to unregister the event handler.
269316 HRESULT add_ActiveMatchIndexChanged (
270317 [in ] ICoreWebView2FindActiveMatchIndexChangedEventHandler* eventHandler,
271318 [out ] EventRegistrationToken* token);
272319 // Unregisters an event handler from the ActiveMatchIndexChanged event.
273- // \param token The token of the event handler to be removed, obtained during the registration of the event handler.
320+ // parameter is the token of the event handler to be removed, obtained during the registration of the event handler.
274321 HRESULT remove_ActiveMatchIndexChanged ([in ] EventRegistrationToken token );
275322}
276323```
0 commit comments