WinForms MenuStrip doesn't deactivate and close when clicking inside a web view
Description The WinForms MenuStrip doesn't deactivate and close when clicking inside a web view. This could be reproduced in the WinForms sample app while the WPF and Win32 sample apps works as expected.
Version SDK: 1.0.1587.40 Runtime: Release 110.0.1587.63 Framework: WinForms OS: Win11
Repro Steps In the WinForms sample app (WebView2WindowsFormsBrowser):
- Open a menu
- Click in the web view
Expected: Web view gets focus and menu deactivates and closes. Actual: Web view seems to get focus fine but the menu never deactivates and stays open.
Screenshots

Additional context Under hit transparent mode (--enable-features=msWebView2BrowserHitTransparent) this does work fine but it's not really an option for us since we need to be able to handle key events in the hosted web app before they reach the host.
I couldn't really tell if this is related or similar to #397 or any of the ones mentioned within.
I also had the same trouble.
Got the same issue.
@ForeverInfinity @0x526f6d656f
If you only expected webview gets focus and menu deactivates and closes, you can add BrowserArgument --enable-features=msWebView2BrowserHitTransparent when create the webview.
@ow-- Based on the information provided, it is possible that the behavior you are experiencing could be attributed to a design flaw in the control. In order to provide you with a more accurate explanation, we will conduct further investigation and get back to you with our findings as soon as possible.
@ForeverInfinity @0x526f6d656f If you only expected webview gets focus and menu deactivates and closes, you can add BrowserArgument
--enable-features=msWebView2BrowserHitTransparentwhen create the webview.@ow-- Based on the information provided, it is possible that the behavior you are experiencing could be attributed to a design flaw in the control. In order to provide you with a more accurate explanation, we will conduct further investigation and get back to you with our findings as soon as possible.
Works fine. Thanks for answering
@ForeverInfinity @0x526f6d656f If you only expected webview gets focus and menu deactivates and closes, you can add BrowserArgument
--enable-features=msWebView2BrowserHitTransparentwhen create the webview.@ow-- Based on the information provided, it is possible that the behavior you are experiencing could be attributed to a design flaw in the control. In order to provide you with a more accurate explanation, we will conduct further investigation and get back to you with our findings as soon as possible.
In our case we also need to handle key events from the web app, so it's not an option for us I'm afraid.
@xu-ms Any update on this?
We've run into this problem as well. Menu's don't close, and keyboard buttons pressed to manipulate the menus (Esc, arrow keys, alt, etc) don't work. If we use --enable-features=msWebView2BrowserHitTransparent then the keys work for the menus but not for the hosted content (can't use arrow keys to select from lists or move in textboxes etc).
And we've also faced this problem.
Is it possible to create an interface like IKeyboardHandler? And called him before a keyboard event is sent to the renderer. Method bool OnPreKeyEvent. Return true, if method process key events.
I am struggling with the same problem; menu (MenuStrip class in Winforms) does not get deactivated/closed when the webwiew2 receives the focus. "--enable-features=msWebView2BrowserHitTransparent" is not a solution, this disables the arrow keys in the html controls (textbox, etc..) in the webview2 control. Looks like the problem is that the menu control does not get notified by the webview2 control of receiving the focus. Any news when this will get fixed?
Hello,
Thank you for creating this issue so that I could find I was not alone. I found the following issue helpful to work around this problem:
https://github.com/MicrosoftEdge/WebView2Feedback/issues/425#issuecomment-1805369827
In the constructor of my form I have added a mouse down event to the top level menu items:
bool _menuMaybeNeedsToBeClosed = false;
public MyForm()
{
InitializeComponent();
foreach(ToolStripItem item in menuStrip1.Items)
{
item.MouseDown += MenuStrip1_MouseDown;
}
}
private void MenuStrip1_MouseDown(object? sender, EventArgs e)
{
_menuMaybeNeedsToBeClosed = true;
}
Then I have added the following override to WndPrc method
protected override void WndProc(ref Message m)
{
if(_menuMaybeNeedsToBeClosed)
{
if (m.Msg == 0x210 /* WM_PARENTNOTIFY */ && m.WParam == 0x201 /* WM_LBUTTONDOWN */)
{
_menuMaybeNeedsToBeClosed = false;
try
{
int x = ((int)(m.LParam & 0x0000FFFF));
int y = ((int)((m.LParam & 0xFFFF0000) >> 16));
var isClickInWebView = false;
if (x >= webView21.Left && x <= webView21.Left + webView21.Width)
{
if (y >= webView21.Top && y <= webView21.Top + webView21.Height)
{
isClickInWebView = true;
}
}
if (isClickInWebView)
{
foreach (ToolStripMenuItem item in menuStrip1.Items)
{
item.DropDown.Close();
}
}
}
catch (Exception ex)
{
Console.WriteLine("Error processing WM_PARENTNOTIFY WM_LBUTTONDOWN");
}
}
}
base.WndProc(ref m);
}