From 1f4e14b2e973ee7de337fd4866d9a5ceff5cb6d1 Mon Sep 17 00:00:00 2001 From: Lucas Faria Mendes Date: Mon, 30 Mar 2026 10:38:18 -0300 Subject: chore: location --- .../Gestor.Application/Componentes/WebBrowser.cs | 197 +++++++++++++++++++++ 1 file changed, 197 insertions(+) create mode 100644 Codemerx/Gestor.Application/Componentes/WebBrowser.cs (limited to 'Codemerx/Gestor.Application/Componentes/WebBrowser.cs') diff --git a/Codemerx/Gestor.Application/Componentes/WebBrowser.cs b/Codemerx/Gestor.Application/Componentes/WebBrowser.cs new file mode 100644 index 0000000..35327bc --- /dev/null +++ b/Codemerx/Gestor.Application/Componentes/WebBrowser.cs @@ -0,0 +1,197 @@ +using Gestor.Application.Properties; +using HtmlAgilityPack; +using mshtml; +using System; +using System.CodeDom.Compiler; +using System.Collections.Generic; +using System.ComponentModel; +using System.Diagnostics; +using System.Net; +using System.Reflection; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Input; +using System.Windows.Interop; +using System.Windows.Markup; +using System.Windows.Navigation; +using System.Windows.Threading; + +namespace Gestor.Application.Componentes +{ + public class WebBrowser : UserControl, IComponentConnector + { + public HTMLDocument HtmlDocument; + + public System.Windows.Controls.WebBrowser Browser; + + public readonly static DependencyProperty IsReadOnlyProperty; + + internal Grid GridWebBrowser; + + private bool _contentLoaded; + + public bool IsReadOnly + { + get + { + return (bool)base.GetValue(Gestor.Application.Componentes.WebBrowser.IsReadOnlyProperty); + } + set + { + base.SetValue(Gestor.Application.Componentes.WebBrowser.IsReadOnlyProperty, value); + } + } + + static WebBrowser() + { + Gestor.Application.Componentes.WebBrowser.IsReadOnlyProperty = DependencyProperty.Register("IsReadOnly", typeof(bool), typeof(Gestor.Application.Componentes.WebBrowser), new PropertyMetadata(false)); + } + + public WebBrowser() + { + this.InitializeComponent(); + System.Windows.Threading.Dispatcher dispatcher = base.Dispatcher; + if (dispatcher == null) + { + return; + } + dispatcher.BeginInvoke(DispatcherPriority.Render, new Action(this.ContentLoad)); + } + + private void Browser_PreviewKeyDown(object sender, KeyEventArgs e) + { + if (this.IsReadOnly) + { + e.Handled = true; + } + } + + private void Completed(object sender, NavigationEventArgs e) + { + this.HtmlDocument = this.Browser.Document as HTMLDocument; + if (this.HtmlDocument == null) + { + return; + } + this.HtmlDocument.designMode = "On"; + } + + private void ContentLoad() + { + base.PreviewKeyDown += new KeyEventHandler(this.Browser_PreviewKeyDown); + } + + private static void EncodeNode(HtmlNode node) + { + if (node.get_HasChildNodes()) + { + foreach (HtmlNode childNode in node.get_ChildNodes()) + { + if (childNode.get_NodeType() != 3) + { + Gestor.Application.Componentes.WebBrowser.EncodeNode(childNode); + } + else + { + childNode.set_InnerHtml(WebUtility.HtmlEncode(childNode.get_InnerHtml())); + } + } + } + else if (node.get_NodeType() == 3) + { + node.set_InnerHtml(WebUtility.HtmlEncode(node.get_InnerHtml())); + } + } + + private void HideScriptErrors() + { + FieldInfo field = typeof(Gestor.Application.Componentes.WebBrowser).GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic); + if (field == null) + { + return; + } + object value = field.GetValue(this.Browser); + if (value != null) + { + value.GetType().InvokeMember("Silent", BindingFlags.SetProperty, null, value, new object[] { true }); + } + } + + public static string HtmlEncode(string html) + { + if (html == null) + { + return null; + } + HtmlAgilityPack.HtmlDocument htmlDocument = new HtmlAgilityPack.HtmlDocument(); + htmlDocument.LoadHtml(html); + htmlDocument.OptionWriteEmptyNodes = true; + Gestor.Application.Componentes.WebBrowser.EncodeNode(htmlDocument.get_DocumentNode()); + return htmlDocument.get_DocumentNode().get_InnerHtml(); + } + + [DebuggerNonUserCode] + [GeneratedCode("PresentationBuildTasks", "4.0.0.0")] + public void InitializeComponent() + { + if (this._contentLoaded) + { + return; + } + this._contentLoaded = true; + System.Windows.Application.LoadComponent(this, new Uri("/Gestor.Application;component/componentes/webbrowser.xaml", UriKind.Relative)); + } + + public void InstanciateNew(string htmlData = null) + { + if (this.Browser != null) + { + this.Browser.LoadCompleted -= new LoadCompletedEventHandler(this.Completed); + this.Browser.Dispose(); + this.GridWebBrowser.Children.Remove(this.Browser); + } + HTMLDocument htmlDocument = this.HtmlDocument; + if (htmlDocument != null) + { + htmlDocument.clear(); + } + else + { + } + this.Browser = new System.Windows.Controls.WebBrowser() + { + IsEnabled = true + }; + this.Browser.LoadCompleted += new LoadCompletedEventHandler(this.Completed); + this.GridWebBrowser.Children.Add(this.Browser); + this.HideScriptErrors(); + htmlData = Gestor.Application.Componentes.WebBrowser.HtmlEncode(htmlData); + this.Browser.NavigateToString((string.IsNullOrEmpty(htmlData) ? Gestor.Application.Properties.Resources.New : htmlData)); + this.HtmlDocument = this.Browser.Document as HTMLDocument; + if (this.HtmlDocument == null) + { + return; + } + this.HtmlDocument.charset = "ISO-8859-1"; + this.HtmlDocument.designMode = "On"; + } + + public void ReadOnly(bool isReadOnly) + { + this.IsReadOnly = isReadOnly; + } + + [DebuggerNonUserCode] + [EditorBrowsable(EditorBrowsableState.Never)] + [GeneratedCode("PresentationBuildTasks", "4.0.0.0")] + void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object target) + { + if (connectionId != 1) + { + this._contentLoaded = true; + return; + } + this.GridWebBrowser = (Grid)target; + } + } +} \ No newline at end of file -- cgit v1.2.3