Win8 是否也为旧版应用程序添加了内置拼写检查器? [英] Did Win8 add built-in spell checker for legacy apps too?

查看:28
本文介绍了Win8 是否也为旧版应用程序添加了内置拼写检查器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 MSDN:

在 Windows 8 中,拼写检查是内置的以编辑控件.

In Windows 8, spell checking is built-in to edit controls.

好吧,我在我的设置中启用了该选项(突出显示和自动更正),但我在 Notepad.exe 或我自己的旧版 Win32 应用程序中没有看到这一点.

Well, I have the option enabled in my settings (both highlight and auto-correct) and I'm not seeing this in Notepad.exe or my own legacy Win32 app.

我需要做什么才能为我的应用程序启用(希望就这么简单)?我确实尝试遵循文章中的信息并阅读了很多参考资料,但它确实不清楚并且似乎旨在创建自定义提供程序/解决方案,但我对任何内置"行为感到满意.

What do I need to do to enable (hopefully it's that simple) this for my application? I did try to follow the info in the article and read a lot of the reference, but it really isn't clear and seems geared towards creating custom providers/solutions, but I'd be happy with any "built-in" behaviors.

推荐答案

仅内置于 Rich Edit 控件,EM_SETLANGOPTIONS,IMF_SPELLCHECKING 选项.您需要使用更高版本的 Rich Edit,即 MsftEdit.dll 中的版本,而不是您默认获得的更常见的 v2.0 版本.

It is built-in only for Rich Edit controls, EM_SETLANGOPTIONS, IMF_SPELLCHECKING option. You need to use the later version of Rich Edit, the one in MsftEdit.dll instead of the more common v2.0 version that you get by default.

我在 Winforms 控件中尝试过,效果很好.请注意,它不会使拼写检查具有交互性,没有任何类似对话框可以让您从一组建议的替代方案中进行选择.任何可以自动更正的东西,比如teh"到the"和spelll"到spell"都会立即应用,没有自动更正的词用红色下划线标出.Ctrl+Z 将自动更正的单词恢复为原始单词.

I tried it out in a Winforms control, worked well. Do note that it doesn't make spell-checking inter-active, nothing resembling a dialog that lets you pick from a set of suggested alternatives. Anything that can be auto-corrected, like "teh" to "the" and "spelll" to "spell" is immediately applied, words that don't have an auto-correction are underlined in red. Ctrl+Z turns an auto-corrected word back into its original.

从这个 C# 代码转换到其他未指定的语言应该不会有太多麻烦.存在一些样板 Winforms 管道,关键要点是使用 LoadLibrary 来初始化 v5 版本的控件,以便您可以使用 RichEdit50W 窗口类名称.并使用 SendMessage() 打开该选项:

You shouldn't have to much trouble working from this C# code to your otherwise unspecified language. There's some boilerplate Winforms plumbing present, key take-aways is to use LoadLibrary to get the v5 version of the control initialized so you can use the RichEdit50W window class name. And use SendMessage() to turn the option on:

using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;

class RichTextBoxEx : RichTextBox {

    protected override CreateParams CreateParams {
        get {
            if (moduleHandle == IntPtr.Zero) {
                moduleHandle = LoadLibrary("msftedit.dll");
                if (moduleHandle == IntPtr.Zero) throw new Win32Exception("Could not load Msftedit.dll");
            }
            CreateParams createParams = base.CreateParams;
            createParams.ClassName = "RichEdit50W";
            if (this.Multiline) {
                if (((this.ScrollBars & RichTextBoxScrollBars.Horizontal) != RichTextBoxScrollBars.None) && !base.WordWrap) {
                    createParams.Style |= 0x100000;
                    if ((this.ScrollBars & ((RichTextBoxScrollBars)0x10)) != RichTextBoxScrollBars.None) {
                        createParams.Style |= 0x2000;
                    }
                }
                if ((this.ScrollBars & RichTextBoxScrollBars.Vertical) != RichTextBoxScrollBars.None) {
                    createParams.Style |= 0x200000;
                    if ((this.ScrollBars & ((RichTextBoxScrollBars)0x10)) != RichTextBoxScrollBars.None) {
                        createParams.Style |= 0x2000;
                    }
                }
            }
            if ((BorderStyle.FixedSingle == base.BorderStyle) && ((createParams.Style & 0x800000) != 0)) {
                createParams.Style &= -8388609;
                createParams.ExStyle |= 0x200;
            }
            return createParams;
        }
    }

    protected override void OnHandleCreated(EventArgs e) {
        base.OnHandleCreated(e);
        if (Environment.OSVersion.Version.Major > 6 ||
            Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Minor >= 2) {
            int opts = (int)SendMessage(this.Handle, EM_GETLANGOPTIONS, IntPtr.Zero, IntPtr.Zero);
            opts |= IMF_SPELLCHECKING;
            SendMessage(this.Handle, EM_SETLANGOPTIONS, IntPtr.Zero, new IntPtr(opts));
        }
    }

    private static IntPtr moduleHandle;

    private const int IMF_SPELLCHECKING = 0x0800;
    private const int EM_SETLANGOPTIONS = 0x0400 + 120;
    private const int EM_GETLANGOPTIONS = 0x0400 + 121;

    [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
    private static extern IntPtr LoadLibrary(string lpFileName);
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    private static extern IntPtr SendMessage(IntPtr hWnd, int msg, IntPtr wp, IntPtr lp);
}

<小时>

更新,当您面向 .NET 4.7 或更高版本时,您现在需要的代码少得多.它已经处理了 CreateParams 覆盖以也使用 msftedit.dll:https://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/RichTextBox.cs,d2aebb12b70acde0

这篇关于Win8 是否也为旧版应用程序添加了内置拼写检查器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆