Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong>Update:</strong> Ok, I just realized you don't have control over the web page, I can only think of injecting some javascript to the page, add an event handler to the WebBrowser's LoadCompleted event and inject the javascript there, this should take care of it and not require you to modify the original web page:</p> <pre><code>// Add a reference to Microsoft.mshtml using mshtml; // inside the window constructor after you set // the webBrowser's ObjectForScripting property this.webBrowser.LoadCompleted += this.webBrowser_LoadCompleted; void webBrowser_LoadCompleted(object sender, NavigationEventArgs e) { var doc = (HTMLDocument)webBrowser.Document; var head = doc.getElementsByTagName("head").Cast&lt;HTMLHeadElement&gt;().First(); var script = (IHTMLScriptElement)doc.createElement("script"); script.text = "javascript:window.onbeforeunload = function () { window.external.Close(); }"; head.appendChild((IHTMLDOMNode)script); } </code></pre> <hr> <p>It sounds like you own the webpage you are opening so here is what you can do:</p> <p>1.- Add a Popup control to your window and add a WebBrowser control to it, you'll use this to navigate to your page:</p> <pre><code>&lt;Button Content="Button" Click="button1_Click" /&gt; &lt;Popup x:Name="PopUp" Width="300" Height="300"&gt; &lt;WebBrowser x:Name="webBrowser"&gt; &lt;/WebBrowser&gt; &lt;/Popup&gt; </code></pre> <p>2.- Create a new class and add the [ComVisible(true)] attribute to it. It should include a reference of your Popup control object. Add a public method to your new class that hides the Popup control.</p> <pre><code>using System.Runtime.InteropServices; using System.Windows; using System.Windows.Controls.Primitives; [ComVisible(true)] public class ScriptHandler { private Popup popup; public ScriptHandler(Popup popup) { this.popup = popup; } public void Close() { this.popup.IsOpen = false; } } </code></pre> <p>3.- Set the WebBrowser control's ObjectForScripting property to an instance of your new class.</p> <pre><code>public partial class MainWindow : Window { private ScriptHandler scriptHandler; public MainWindow() { InitializeComponent(); this.scriptHandler = new ScriptHandler(this.PopUp); this.webBrowser.ObjectForScripting = this.scriptHandler; } // Shows the pop up and navigates to your page private void button1_Click(object sender, RoutedEventArgs e) { this.webBrowser.Navigate("http://localhost:56977/PopUp.htm"); this.PopUp.IsOpen = true; } } </code></pre> <p>4.- Since you registered the ScriptHandler object with the WebBrowser control all public methods in this class can be accessed from javascript, add a javascript method to your page (you may need to verify the 'undefined' validation I'm doing) that calls the Close method in your ComVisible class:</p> <pre><code>&lt;script type="text/javascript"&gt; function CloseMe() { if(window.external != null &amp;&amp; window.external.CloseMe != 'undefined') { window.external.Close(); } } &lt;/script&gt; </code></pre> <p>5.- Add a link that calls the javascript CloseMe method to your page:</p> <pre><code>&lt;a href='javascript:CloseMe()'&gt;Close Me&lt;/a&gt; </code></pre> <p>As you probably realized already this solution will work not only with a Popup control, you can also instantiate a new Window and close it, dispose of your WeBrowser control etc. or just Show/Hide a different type of control.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload