微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

有什么办法可以将MS Office平滑打字整合到C#应用程序中吗?

在我看来,MS Office平滑打字是Office套件中非常创新的功能,我想知道这个功能是否适用于.NET Framework中的程序员,特别是C#语言.

如果是这样,你可以在你的答案中发布文档的链接,也可能是一个使用示例?

谢谢.

解决方法

我没有办公室,所以我不能看这个功能,但是我需要在RichTextBoxes前面提到一些插入符号,并且认为这不值得.基本上你是自己的.没有.NET的帮助函数,但是一切都由后台Win32控件处理.你将很难打败罩下已经发生的事情.并且可能最终截断窗口消息和许多丑陋的代码.

所以我的基本建议是:不要这样做至少对于基本的窗体控件,如TextBox或RichTextBox.您可能会有更多的运气尝试从.NET远程访问运行的办公室,但这是一个完全不同的蠕虫病毒.

如果你真的坚持要去SetCaretPos路线,这里有一些代码可以让你运行一个基本的版本,你可以改进:

// 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();
}

原文地址:https://www.jb51.cc/csharp/93741.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐