cool hit counter [Handbook] WebBrowser response page in the blank open a new window and window.close close this form_Intefrankly

[Handbook] WebBrowser response page in the blank open a new window and window.close close this form


Notes. This article applies to .net 2.0+ winform projects

Objective.

  • Click on the pagetarget="_blank" When linking, Pop-up new form
  • Close the form when there is a window.close() operation on the page

The above form is the Form where the WebBrowser is located, and basically, the purpose of the above is just to make the form behave like a normal browser.

I. Pop-up new form

WebBrowser (hereinafter called wb) comes with a NewWindow event, so just register it directly at

private void wb_NewWindow(object sender, CancelEventArgs e)
{
     e.Cancel = true;//It makes no difference in my environment whether this is added or not, and not adding it won't cause an external browser to open
     new FmWebBrowser(wb. StatusText). Show();  //FmWebBrowser is my form that hosts wb, the constructor of this class accepts a url, and after Show will cause wb to access that url. Also, the StatusText of wb when that event enters is almost certainly the href of the link that was clicked, extreme cases will be encountered later
}

Second, respond to window.close in the page to close this form

Since wb does not have a ready-made event such as Close, so this is to toss a little, is to add this event to it, the core of the problem to be solved, is to let wb know that the page executed window.close(), solve this, the rest is to notify this out just.

  1. Let wb know that the page executes window.close() and raises a specific event The method is copied from the internet, the principle is that when the page executes window.close() wb receives a specific win32 message, so it can override wb's WndProc method to handle this message, which requires inheriting from wb to write a subclass, the subclass is as follows.
using System;
using System.Security.Permissions;
using System.Windows.Forms;

namespace AhDung.WinForm.Controls
{
    /// <summary>
    ///  Enhanced Browser
    /// </summary>
    public class WebBrowserEx : WebBrowser
    {
        /// <summary>
        ///  properWebBrowser post-closing
        /// </summary>
        public event EventHandler WindowClosed;

        protected void OnWindowClosed(EventArgs e)
        {
            if (WindowClosed != null) { WindowClosed(this, e); }
        }

        [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
        protected override void WndProc(ref Message m)
        {
            if (m.Msg == 0x210/*WM_PARENTNOTIFY*/)
            {
                int wp = m.WParam.ToInt32();

                int X = wp & 0xFFFF;
                if (X == 0x2/*WM_DESTROY*/)// If you receive this message, lead toWindowClosed events
                {
                    OnWindowClosed(EventArgs.Empty);
                }
            }

            base.WndProc(ref m);
        }
    }
}
  1. The rest is easy, the host form responds to the WindowClosed event of WebBrowserEx and just closes itself. Of course the first step is to replace the previous WebBrowser with the above WebBrowserEx, code snippet.
private WebBrowserEx wbex = new WebBrowserEx();
...

public FmMain()
{
    wbex.WindowClosed += wbex_WindowClosed;
    ...
}

void wbex_WindowClosed(object sender, System.EventArgs e)
{
    this.Close();
}

- Man Bi -


Recommended>>
1、The original has a variety of spell three cheat auxiliary general version
2、When a heart device meets a hacker how can you avoid being attacked
3、unsupervised learning
4、Microsofts Denali project has a streamlined SSD controller
5、Samsungs 146inch TV wall is awesome LGs rollable screen TV is even more anticlimactic

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号