有没有什么办法可以整合微软Office平滑输入一个C#应用程序?微软、平滑、有没有什么、应用程序

2023-09-03 00:08:49 作者:[祭冬]

在我看来微软Office平滑打字是在Office套件一个非常创新的功能,我想知道这个功能可用于在.NET Framework编程人员,特别是在C#语言。

In my opinion the MS Office Smooth Typing is a very innovating feature in the Office Suite, and I'd like to know if this feature is available for programmers in the .NET Framework, specifically in the C# language.

如果是这样,请你在你的答案一个链接发布到文件并可能也是一个使用例子

If so, could you please post in your answer a link to the documentation and might also a usage example?

感谢。

推荐答案

我没有自己的办公室,所以我不能看功能,但我需要反复折腾的RichTextBoxes插入符前一阵子,决定这是不值得的努力。基本上,你是你自己。从.NET没有辅助功能,但一切由后盾Win32的控制处理。你将有一个很难击败的引擎盖下已经发生的事情。而且很可能结束了拦截窗口消息和许多丑陋的code。

I don't own Office, so I can't look at the feature, but I needed to fiddle around with the caret in RichTextBoxes a while ago and decided that it wasn't worth the effort. Basically you are on your own. No helper functions from .NET, but everything is handled by the backing Win32 control. You will have a hard time defeating what already happens under the hood. And probably ending up intercepting window messages and lots of ugly code.

所以我的基本建议是:不要做。至少对于像文本框或RichTextBox的基本形式控制。您可能有更多的运气试图进行远程访问在.NET中运行Office,但是这是一个完全不同的CAN蠕虫。

So my basic advice is: Don't do it. At least for basic form controls like the TextBox or RichTextBox. You may have more luck trying to remote access an running office from within .NET, but that is a totally different can of worms.

如果你真的坚持要去的SetCaretPos - 航线,这里有一些code到让你和一个基本的版本上运行,你可以改善后:

If you really insist on going the SetCaretPos - route, here is some code to get you up and running with a basic version where you can improve upon:

// import the functions (which are part of Win32 API - not .NET)
[DllImport("user32.dll")] static extern bool SetCaretPos(int x, int y);
[DllImport("user32.dll")] static extern Point GetCaretPos(out Point point);

public Form1()
{
    InitializeComponent();

    // target position to animate towards
    Point targetCaretPos; GetCaretPos(out targetCaretPos);

    // richTextBox1 is some RichTextBox that I dragged on the form in the Designer
    richTextBox1.TextChanged += (s, e) =>
        {
            // we need to capture the new position and restore to the old one
            Point temp;
            GetCaretPos(out temp);
            SetCaretPos(targetCaretPos.X, targetCaretPos.Y);
            targetCaretPos = temp;
        };

    // Spawn a new thread that animates toward the new target position.
    Thread t = new Thread(() => 
    {
        Point current = targetCaretPos; // current is the actual position within the current animation
        while (true)
        {
            if (current != targetCaretPos)
            {
                // The "30" is just some number to have a boundary when not animating
                // (e.g. when pressing enter). You can experiment with your own distances..
                if (Math.Abs(current.X - targetCaretPos.X) + Math.Abs(current.Y - targetCaretPos.Y) > 30)
                    current = targetCaretPos; // target too far. Just move there immediately
                else
                {
                    current.X += Math.Sign(targetCaretPos.X - current.X);
                    current.Y += Math.Sign(targetCaretPos.Y - current.Y);
                }

                // you need to invoke SetCaretPos on the thread which created the control!
                richTextBox1.Invoke((Action)(() => SetCaretPos(current.X, current.Y)));
            }
            // 7 is just some number I liked. The more, the slower.
            Thread.Sleep(7);
        }
    });
    t.IsBackground = true; // The animation thread won't prevent the application from exiting.
    t.Start();
}