WebBrowser and javascript window.close()

后端 未结 5 1922
挽巷
挽巷 2020-12-19 02:31

If I host a WebBrowser in my application, and a javascript code in the web page shown on my WebBrowser calls window.close() and I click \"Yes\" on

5条回答
  •  星月不相逢
    2020-12-19 03:04

    In WPF you can catch WM_CLOSE message by attaching to WebBrowser's message loop.

    public MainWindow()
    {
        InitializeComponent();
        webBrowser.MessageHook += webBrowser_MessageHook;
    }
    
    IntPtr webBrowser_MessageHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
    {
        switch(msg)
        {
            case 0x0010:    // WM_CLOSE
                handled = true; // cancel event here
                // do additional stuff here    
                break;
        }
        return IntPtr.Zero;
    }
    

提交回复
热议问题