IspObjectToken::DisplayUI

IspObjectToken::DisplayUI

我尝试在 C# 中使用 SAPI 5.4 中的 IspObjectToken::DisplayUI,但我不知道它的确切参数是什么。

void ISpObjectToken.DisplayUI(ref _RemotableHandle HWndParent, string pszTitle, string pszTypeOfUI,IntPtr pvExtraData, uint cbExtraData,object punkObject)

我知道我们应该为 pszTitle 设置 null,对于 pszTypeOfUI 我们应该使用受支持的 UI 之一,例如 MicTrainingAddRemoveWord ,但我对其余的一无所知。我在笔记本电脑上使用 windows 7。

编辑:这是我写的代码,显然它不起作用。

private void button1_Click(object sender, EventArgs e)
    {
        int pfSupported;
        string extradata = "test[=12=]";
        IntPtr pvExtraData = Marshal.StringToHGlobalUni(extradata);
        uint cbExtraData = (uint)extradata.Length * sizeof(Char);
        Speechlib._RemotableHandle rh = new Speechlib._RemotableHandle();
        rh = getRemotableHandle(this.Handle);
        ISpObjectToken isot = (ISpObjectToken)new SpObjectToken();

        isot.IsUISupported("AddRemoveWord", pvExtraData, cbExtraData, null, out pfSupported);
        if (pfSupported == 1)
        {
            isot.DisplayUI(ref rh, null, "AddRemoveWord", pvExtraData, cbExtraData, null);
        }
    }
    SpeechLib._RemotableHandle getRemotableHandle(IntPtr handle)
    {
        IntPtr address = Marshal.AllocHGlobal(IntPtr.Size);
        Marshal.WriteIntPtr(address, handle);
        return (SpeechLib._RemotableHandle)Marshal.PtrToStructure(address, typeof(SpeechLib._RemotableHandle));
    }

您始终可以传递空值和 0(视情况而定)以获得 UI 类型的默认行为。

如果您想要特定的行为,那么您传递的内容取决于 UI 的类型和底层引擎。

对于 Microsoft Desktop SR 引擎,pUnkObject 参数应始终为 NULL。此参数没有(有趣的)有效非空值。 如果 pszTypeOfUI 是 SPDUI_UserTraining or SPDUI_AddRemoveWord,则 pvExtraData 和 cbExtraData 可以是非 NULL。

当 pszTypeOfUI 为 SPDUI_AddRemoveWord 时,pvExtraData 可以是一个标准的以 null 结尾的字符串,其中包含要添加到词典中的单词。 cbExtraData 应该是以字节为单位的字符串大小,包括终止空值。

当 pszTypeOfUI 是 SPDUI_UserTraining, then pvExtraData should be a double-null-terminated string 包含一组训练句子。 cbExtraData 应该是整个字符串的大小(以字节为单位),包括两个终止空值。

我更详细地介绍了我的 blog

看来你的代码有点过于复杂了。通过正确的引用,包含 System.Speech 和 SpeechLib,我得到了以下工作并启动了 AddRemoveWord 对话框。

using SpeechLib;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
private void button1_Click(object sender, EventArgs e)
        {
            SpSharedRecoContext spSharedRecoCtx = new SpSharedRecoContext();
            ISpeechRecognizer ispSpeechReco = spSharedRecoCtx.Recognizer;
            if (ispSpeechReco.IsUISupported("AddRemoveWord", null))
            {
                ispSpeechReco.DisplayUI(this.Handle.ToInt32(), "Additional Training", "AddRemoveWord", "Example");
            }
        }

Eric 还介绍了如何使用 DisplayUI() 进行自定义语法训练here