private void InitBrowser() { Control = new WebBrowser(); Control.HorizontalAlignment = HorizontalAlignment.Stretch; Control.LoadCompleted += (s, e) => { Zoom(_zoomFactor); _htmlDocument = (HTMLDocument)Control.Document; _cachedHeight = _htmlDocument.body.offsetHeight; _htmlDocument.documentElement.setAttribute("scrollTop", _positionPercentage * _cachedHeight / 100); foreach (IHTMLElement link in _htmlDocument.links) { HTMLAnchorElement anchor = link as HTMLAnchorElement; if (anchor == null || anchor.protocol != "file:") { continue; } HTMLAnchorEvents_Event handler = anchor as HTMLAnchorEvents_Event; if (handler == null) { continue; } string file = anchor.pathname.TrimStart('/').Replace('/', Path.DirectorySeparatorChar); if (!File.Exists(file)) { anchor.title = "The file does not exist"; return; } handler.onclick += () => { ProjectHelpers.OpenFileInPreviewTab(file); return(true); }; } }; // Open external links in default browser Control.Navigating += (s, e) => { if (e.Uri == null) { return; } e.Cancel = true; if (e.Uri.IsAbsoluteUri && e.Uri.Scheme.StartsWith("http")) { Process.Start(e.Uri.ToString()); } }; }
private void MainBrowser_LoadCompleted(object sender, NavigationEventArgs e) { HTMLDocument doc = (mshtml.HTMLDocument) this.MainBrowser.Document; foreach (IHTMLElement link in doc.links) { HTMLAnchorElement anchor = link as HTMLAnchorElement; if (anchor != null) { HTMLAnchorEvents_Event handler = anchor as HTMLAnchorEvents_Event; if (handler != null) { handler.onclick += new HTMLAnchorEvents_onclickEventHandler(delegate() { Console.WriteLine("You clicks the link: " + anchor.href); Process.Start(anchor.href); // MainBrowser.NavigateToString(mail.Message); return(true); }); } } } }