WebView2Feedback icon indicating copy to clipboard operation
WebView2Feedback copied to clipboard

WinForms MenuStrip doesn't deactivate and close when clicking inside a web view

Open ow-- opened this issue 2 years ago • 10 comments

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 winforms-menu

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.

ow-- avatar Mar 09 '23 15:03 ow--

I also had the same trouble.

ForeverInfinity avatar Mar 20 '23 13:03 ForeverInfinity

Got the same issue.

0x526f6d656f avatar Mar 30 '23 09:03 0x526f6d656f

@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.

xu-ms avatar Apr 03 '23 02:04 xu-ms

@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.

Works fine. Thanks for answering

ForeverInfinity avatar Apr 08 '23 01:04 ForeverInfinity

@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.

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.

0x526f6d656f avatar Apr 12 '23 05:04 0x526f6d656f

@xu-ms Any update on this?

0x526f6d656f avatar Jul 06 '23 08:07 0x526f6d656f

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).

wintal avatar Mar 11 '24 03:03 wintal

And we've also faced this problem.

fotodmitriru avatar Mar 11 '24 08:03 fotodmitriru

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.

fotodmitriru avatar Mar 11 '24 10:03 fotodmitriru

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?

Jeroen3000 avatar Jun 25 '24 08:06 Jeroen3000

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);
        }

ashelley avatar Feb 27 '25 21:02 ashelley