MicrosoftEdge / MicrosoftEdge/WebView2Feedback

Working with context menu

Open
#5,129 2 comments 0 reactions 1 assignee View on GitHub

@chetanpandey1266 is already working on this.

Since Mar 12, 2025.

Dominant language
PowerShell
Stars
526
Forks
67
PR merge metrics
No merged PRs in 30d

Description

I am confused.

Take this handler snippet:

// ===============================================================
// Refresh AJT 23.2.0 JIRA MSA 210
if (!m_itemRefresh || bFullInit)
{
	m_itemRefresh.reset();
	wil::com_ptr<IStream> iconStream;
	CHECK_FAILURE(SHCreateStreamOnFileEx(
		theApp.GetCommonAppDataFolder() + L"WebView2\\refresh" + strIconSuffix, STGM_READ, FILE_ATTRIBUTE_NORMAL, FALSE,
		nullptr, &iconStream));

	CString strMenuText;
	strMenuText.LoadString(IDS_STR_MENU_REFRESH);
	CHECK_FAILURE(webviewEnvironment->CreateContextMenuItem(
		strMenuText, iconStream.get(),
		COREWEBVIEW2_CONTEXT_MENU_ITEM_KIND_COMMAND, &m_itemRefresh));

	CHECK_FAILURE(m_itemRefresh->add_CustomItemSelected(
		Callback<ICoreWebView2CustomItemSelectedEventHandler>(
			[appWindow = this, target](ICoreWebView2ContextMenuItem* sender, IUnknown* args)
			{
				appWindow->GetParentWindow()->SendMessage(WM_COMMAND, ID_VIEW_REFRESH, NULL);

				return S_OK;
			})
		.Get(), nullptr));
}
CHECK_FAILURE(items->InsertValueAtIndex(0, m_itemRefresh.get()));
// ===============================================================

Now, if my context is:

CDialog > WebView2 > Right-click > Refresh

Works. GetParentWindow() is the pointer to CDialog.

Now, if my context is:

CDialog > TabCtrl > WebView2 Child Container > WebView2 > Right-click > Refresh

It doesn't work. I realised this was because GetParentWindow() is going to be WebView2 Child Container. So I tried two things:

1/ GetParentWindow()->GetParent()>GetParent()->PostMessage ...
The main dialog code is intercepted. But the moment the AfxMessageBox call happens in main dialog the app appears to freeze. I can see no popup. The app feels it is running this but I can't respond to the popup message box.

2/ When my WV2 is created, and before setting up the handlers, I pass it my WebView2 Child Container pointer. Then in the event handler it uses that pointer. Then, in WebView2 Child container I catch that, and post (or send) up the chain to CDialog. The result is the same, the moment a AfxMessageBox is encounter, app is freezing.

I have spent all afternoon and evening trying to get around this. The basic scenario is fine.

My latter scenario is a dialog, which has a TabCtrl, which has 3 tabs, each with a WebView2 Child Container, housing a WebView2.

I appreciate help. Thanks.

Win32.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.