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

前端之家收集整理的这篇文章主要介绍了有什么办法可以将MS Office平滑打字整合到C#应用程序中吗?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我看来,MS Office平滑打字是Office套件中非常创新的功能,我想知道这个功能是否适用于.NET Framework中的程序员,特别是C#语言.

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

谢谢.

解决方法

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

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

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

  1. // import the functions (which are part of Win32 API - not .NET)
  2. [DllImport("user32.dll")] static extern bool SetCaretPos(int x,int y);
  3. [DllImport("user32.dll")] static extern Point GetCaretPos(out Point point);
  4.  
  5. public Form1()
  6. {
  7. InitializeComponent();
  8.  
  9. // target position to animate towards
  10. Point targetCaretPos; GetCaretPos(out targetCaretPos);
  11.  
  12. // richTextBox1 is some RichTextBox that I dragged on the form in the Designer
  13. richTextBox1.TextChanged += (s,e) =>
  14. {
  15. // we need to capture the new position and restore to the old one
  16. Point temp;
  17. GetCaretPos(out temp);
  18. SetCaretPos(targetCaretPos.X,targetCaretPos.Y);
  19. targetCaretPos = temp;
  20. };
  21.  
  22. // Spawn a new thread that animates toward the new target position.
  23. Thread t = new Thread(() =>
  24. {
  25. Point current = targetCaretPos; // current is the actual position within the current animation
  26. while (true)
  27. {
  28. if (current != targetCaretPos)
  29. {
  30. // The "30" is just some number to have a boundary when not animating
  31. // (e.g. when pressing enter). You can experiment with your own distances..
  32. if (Math.Abs(current.X - targetCaretPos.X) + Math.Abs(current.Y - targetCaretPos.Y) > 30)
  33. current = targetCaretPos; // target too far. Just move there immediately
  34. else
  35. {
  36. current.X += Math.Sign(targetCaretPos.X - current.X);
  37. current.Y += Math.Sign(targetCaretPos.Y - current.Y);
  38. }
  39.  
  40. // you need to invoke SetCaretPos on the thread which created the control!
  41. richTextBox1.Invoke((Action)(() => SetCaretPos(current.X,current.Y)));
  42. }
  43. // 7 is just some number I liked. The more,the slower.
  44. Thread.Sleep(7);
  45. }
  46. });
  47. t.IsBackground = true; // The animation thread won't prevent the application from exiting.
  48. t.Start();
  49. }

猜你在找的C#相关文章