Spellcheck in web browser control not working unde

2019-04-30 12:55发布

问题:

I am trying to have spellcheck work in a Winforms web browser control.

This is my current C# code:

try
{
    String appname = Process.GetCurrentProcess().ProcessName + ".exe";
    RegistryKey key = Registry.LocalMachine.OpenSubKey("Software\\Microsoft\\Internet Explorer\\Main\\FeatureControl\\FEATURE_BROWSER_EMULATION", RegistryKeyPermissionCheck.ReadWriteSubTree);

    object ieVal = key.GetValue(appname, null);
    MessageBox.Show(ieVal.ToString());

    if (ieVal == null || (int)ieVal != 11001)
    {
        key.SetValue(appname, 11001, RegistryValueKind.DWord);
    }

    key.Close();
}
catch
{
    MessageBox.Show("Registry stuff didn't work");
}

MessageBox.Show(webBrowser1.Version.ToString());

webBrowser1.DocumentText = "<html><head><body><div spellcheck=\"true\" style=\"width:100%; height:100%;\" contenteditable=\"true\"></div>"
            +"<script>alert(navigator.userAgent);</script>"
            +"</body></html>";

So first I set the proper registry key so that the browser emulates IE11

Then I add a div tag with spellcheck attribute set to true.

The version that the MessageBox.Show(webBrowser1.Version.ToString()) shows is:

11.0.9600.18525

The navigator.userAgent that the Javascript displays is:

Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko

So it seems like the web browser control is using IE11. But when I type the spell check doesn't work.

Note: When I run that html code with the real IE everything works properly.

Also, the navigator.userAgent displayed on the actual browser is:

Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; rv:11.0) like Gecko

Note2: When I run my application on Windows 10 machine the spellcheck works. But I need to make it work on Windows 7 machines.

回答1:

I had very similar problems using a WebBrowser control on a form and found 2 solutions, each with differing effects, which could both be used.

  1. Using spellcheck=true in the HTML:

    Adding the attribute spellcheck=true to the HTML or BODY or TEXTAREA tags, depending on where you want it implemented, will allow spelling checking on text input boxes (my tests were on Windows 10).

    Note that any EXISTING text in the text boxes was not spell checked - you had to type NEW text in. This caught me out when running test EXEs with a pre-filled text box, which never got the little red underlines on its spelling mistakes.

  2. Registry entry for FEATURE_SPELLCHECK

    This did something different. It allowed spell checking in TinyMCE to work in our embedded web browser control. On its own, it did not enable spell checking in text areas.

    See the link below for references to a registry key close to the one you were setting. However, it requires a new key and then a value that matches the name of the EXE you're running. In my case, this involved creating the FEATURE_SPELLCHECKING registry key, and then a DWORD with name TEST123.EXE and value 1.

    https://msdn.microsoft.com/en-us/library/ee330735%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396#spellchecking

    I found this through the page linked below, where someone reports it not working for Windows 7. Note that this person also tries it with a "local user" key, which does not work in my experience:

    https://social.msdn.microsoft.com/Forums/ie/en-US/515fa4b1-2b85-46e4-a041-7dc27c4539c4/how-to-enable-spell-checker-in-web-browser-control-for-ie10?forum=ieextensiondevelopment

  3. Use both of the above.

    We found that approach 2 above met most of our needs, as we were using TinyMCE. However, in other applications, both 1 and 2 can be used in conjunction to provide the most functionality.