在我看來,MS Office Smooth Typing是Office Suite中的一項非常創新的功能,我想知道.NET Framework中的程序員是否可以使用此功能,特別是C#語言。有什麼辦法可以將MS Office平滑打字集成到C#應用程序中?
如果是這樣,你能請張貼在你的答案一個鏈接到文件,也可能在使用示例?
謝謝。
在我看來,MS Office Smooth Typing是Office Suite中的一項非常創新的功能,我想知道.NET Framework中的程序員是否可以使用此功能,特別是C#語言。有什麼辦法可以將MS Office平滑打字集成到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();
}
那。是。驚人。非常感謝。 – 2013-05-30 12:41:25
非常驚人的代碼!就像@ newStackExchangeInstance所說的那樣,但是當你按Enter鍵輸入一行時,會出現一個問題,光標和你走得更遠的時間相同,你認爲你可以修復這個問題,這樣光標就會返回默認速度?我認爲這將解決問題,如果是的話,我給你的獎金。 – Zignd 2013-05-30 14:23:39
謝謝。如果這是你看到的唯一問題,那麼它很好:)。我會說,還有一些不太好的東西,比如如果你打字速度太快,字母出現在插入符號「到達」之前,等等。無論如何,我更新了代碼,所以它不會動畫脫字符當距離太遠時,它應該「跳」到下一行,而不是在那裏緩慢移動。玩得開心的代碼;) – Imi 2013-05-30 16:46:17
使用SetCaretPos與您自己的動畫定時功能。創建一個新線程,根據之前的位置和新的所需位置插入插入符的位置。
請解釋一下這個「平滑輸入」功能是。你是指脫字符的動畫效果? – Dai 2013-03-11 03:10:55
@Dai我指的是打字動畫,這使得光標在打字過程中滑動。當你在MS Office Word 2010中鍵入內容並切換到MS Office Word 2013時,可以很容易地注意到。 – Zignd 2013-03-11 13:40:50