ryancheung / ImeSharp

C# wrapper for Windows IME APIs
MIT License
74 stars 12 forks source link

无法在Monogame上正常工作 #17

Closed point32 closed 5 months ago

point32 commented 5 months ago

项目模板:MonoGame Windows Desktop Application(短名称:mgwindowsdx)

更改Game1.cs的两个方法:

protected override void LoadContent()
    {
        // 使用 Procecss.GetCurrentProcess().MainWindowHandle 也不行
        var form = (Form)Window.GetType().GetField("Form", BindingFlags.NonPublic | BindingFlags.Instance)!.GetValue(Window)!;
        InputMethod.Initialize(form.Handle);
    }

    protected override void Update(GameTime gameTime)
    {
        if (Keyboard.GetState().IsKeyDown(Keys.Escape))
        {
            Exit();
        }

        if (Keyboard.GetState().IsKeyDown(Keys.Enter))
        {
            InputMethod.Enabled = true;
        }

        base.Update(gameTime);
    }

复现步骤:运行之后切换按下回车键 异常: Unhandled exception. SharpGen.Runtime.SharpGenException: HRESULT: [0x80004002], Module: [Unknown], ApiCode: [Unknown/Unknown], Message: 不支持此接口

at SharpGen.Runtime.Result.ThrowFailureException() at SharpGen.Runtime.ComObject.QueryInterface[T]() at ImeSharp.TextServicesContext.get_ThreadManager() at ImeSharp.TextServicesContext.RegisterTextStore(TextStore defaultTextStore) at ImeSharp.TextStore.get_Current() at ImeSharp.TextServicesContext.SetFocusOnDefaultTextStore() at ImeSharp.InputMethod.EnableOrDisableInputMethod(Boolean bEnabled) at ImeSharpOnMonogame.Game1.Update(GameTime gameTime) in D:\Notran\ImeSharpOnMonogame\ImeSharpOnMonogame\Game1.cs:line 57 at Microsoft.Xna.Framework.Game.DoUpdate(GameTime gameTime) at Microsoft.Xna.Framework.Game.Tick() at MonoGame.Framework.WinFormsGameWindow.TickOnIdle(Object sender, EventArgs e) at System.Windows.Forms.Application.ThreadContext.Interop.Mso.IMsoComponent.FDoIdle(msoidlef grfidlef) at System.Windows.Forms.Application.ComponentManager.Interop.Mso.IMsoComponentManager.FPushMessageLoop(UIntPtr dwComponentID, msoloop uReason, Void* pvLoopData) at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(msoloop reason, ApplicationContext context) at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(msoloop reason, ApplicationContext context) at System.Windows.Forms.Application.Run(Form mainForm) at MonoGame.Framework.WinFormsGameWindow.RunLoop() at Microsoft.Xna.Framework.Game.Run(GameRunBehavior runBehavior) at Program.

$(String[] args) in D:\Notran\ImeSharpOnMonogame\ImeSharpOnMonogame\Program.cs:line 6

我试过把 WindowsDX 换成 DesktopGL, 但是仍会抛出此异常

point32 commented 5 months ago
protected override void LoadContent()
    {
        var form = (Form)Window.GetType().GetField("Form", BindingFlags.NonPublic | BindingFlags.Instance)!.GetValue(Window)!;
        InputMethod.Initialize(form.Handle, tsfForceDisabled: true);
    }

当我把LoadContent中的Initialize调用增加上tsfForceDisabled参数并设置为true时,上述代码正常运行,且不再抛出该异常