1

Hi there I want a great HTML editor in WPF. So for this what I did is that I added a WebBrowser to WPF window and navigated it to the html page with the TinyMCE html editor in it. However when I run the app, the browser control shows the script warning: (please refer to this MSDN thread for images http://social.msdn.microsoft.com/Forums/en/wpf/thread/cbc3eae6-dbc4-4074-befc-902d990fbaae)

I tried the COM code posted by Simon Mourier on StackOverflow here (http://stackoverflow.com/questions/6138199/wpf-webbrowser-control-how-to-supress-script-errors)

Now my Code look like this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Runtime.InteropServices;
using System.Reflection;

namespace TinyMceWpf
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            webBrowser1.Navigated += new NavigatedEventHandler(webBrowser1_Navigated);
            webBrowser1.Navigate(new
Uri(@"C:\Users\MAHESH\Desktop\TechNode\WPF\MytTechDos\TinyMceWpf\TinyMceWpf\TinyMceWpf\edit.html"));
        }


        private void btnGetHtml_Click(object sender, RoutedEventArgs e)
        {
            string editHtml = this.webBrowser1.InvokeScript("getContent").ToString();
            MessageBox.Show(editHtml);
        }

    public static void SetSilent(WebBrowser browser, bool silent)
    {
        if (browser == null)
            throw new ArgumentNullException("browser");

        // get an IWebBrowser2 from the document
        IOleServiceProvider sp = browser.Document as IOleServiceProvider;
        if (sp != null)
        {
            Guid IID_IWebBrowserApp = new Guid("0002DF05-0000-0000-C000-000000000046");
            Guid IID_IWebBrowser2 = new Guid("D30C1661-CDAF-11d0-8A3E-00C04FC9E26E");

            object webBrowser;
            sp.QueryService(ref IID_IWebBrowserApp, ref IID_IWebBrowser2, out webBrowser);
            if (webBrowser != null)
            {
                webBrowser.GetType().InvokeMember("Silent", BindingFlags.Instance | BindingFlags.Public | BindingFlags.PutDispProperty, null, webBrowser, new object[] { silent });
            }
        }
    }


    [ComImport, Guid("6D5140C1-7436-11CE-8034-00AA006009FA"), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    private interface IOleServiceProvider
    {
        [PreserveSig]
        int QueryService([In] ref Guid guidService, [In] ref Guid riid, [MarshalAs(UnmanagedType.IDispatch)] out object ppvObject);
    }

    private void webBrowser1_Navigated(object sender, NavigationEventArgs e)
    {
        SetSilent(webBrowser1, true);
    }
}
}

But it is still not working till now. What should I do? Please help.

3
  • When you enabled the script the error went away. What specifically is the problem? Commented Apr 5, 2012 at 16:47
  • Yes when I enabled the script the error went away. But I dont want the script error to come at all when I run the app and the WebBrowser control is navigated to the html page that contains the tinyMce. Commented Apr 5, 2012 at 16:53
  • oop sorry for that. Actually I use to quickly go through the response and then implement and experiment with it. So in that I feel that I forgot to mark them as answer. But I am seriously puzzled how I did not marked none of them as answer. My mistake. I now marked them. Yes thanks to point to this. Commented Apr 5, 2012 at 17:04

1 Answer 1

3

Use this code in your SetSilent method.

FieldInfo webBrowserInfo = browser.GetType().GetField("_axIWebBrowser2", BindingFlags.Instance | BindingFlags.NonPublic);
object comWebBrowser = null;

if (webBrowserInfo != null)
   comWebBrowser = webBrowserInfo.GetValue(browser);

if (comWebBrowser != null)
   comWebBrowser.GetType().InvokeMember("Silent", BindingFlags.SetProperty, null, comWebBrowser, new object[] { silent });
Sign up to request clarification or add additional context in comments.

2 Comments

What is the silent object in the last line?
The last flag is bool silent. See also: stackoverflow.com/a/3190790/787893

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.