Pergunta

I am hosting a web browser control, initialized with IWebBrowser2.put_Silent(VARIANT_TRUE).

However, when calling IWebBrowser2.Navigatewith a malformed path, I still get a message box to the equivalent of

"file:///D://htmlstuff/page.html" was not found. Make sure the path or internet address is correct.

The issue here is the accidental double-slash specified by the user (D:\htmlstuff instead of D:\htmlstuff).

I can catch this particular problem earlier, before doing the Navigate, but I'm concerned that this message box still occurs, as I cannot verify every possible URL (or other possible cause that makes the webbrowser control ignore the Silent flag).

I would expect to get an error code returned silently.

Any ideas? Is there a "even more silent" option?

Windows 8.0, IE 10.0.9200.16750

Foi útil?

Solução 2

Try handling NavigateError event on the underlying WebBrowser ActiveX control. I have an example showing how handle the "underlying" WebBrowser events like that. It's for WPF, but it can be easily adapter for WinForms (using WebBrowser.ActiveXInstance).

Alternatively, I think you should be able to handle this kind of messages by implementing IDocHostShowUI::ShowMessage. I have another example showing how to implement IDocHostUIHandler on WebBrowser site object. The same approach can be used for IDocHostShowUI.

Disclaimer: I haven't verified either of these two potential solutions.

Outras dicas

This is a nasty behavior of the WebBrowser control, which I experience with VBA and UserForms. My workaround was to load the target URL into an iframe within a small hosting page. This way no alert pops up, and you can query if the target URL did indeed load or not.

Call the hosting page with <PathTo>/framehost.html#<TargetURL>

<!DOCTYPE html>
<head>
    <title>Frame Host</title>
    <script>

window.onload = function() {
    var frame = document.getElementById('MyFrame');
    frame.src = location.hash.substring(1);
    frame.onload = function() {
        try {
            var doc = frame.contentDocument;
            alert("Loaded " + frame.src);
        } catch(e) {
            alert("Failed to load " + frame.src)
        }
    }
}

    </script>
</head>
<body>
    <iframe id="MyFrame" src="about:blank"></iframe>
</body>
</html>
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top