2013-03-10 5 views
10

私の意見では、MS Office Smooth TypingはOfficeスイートの非常に革新的な機能です。この機能が.NET Frameworkのプログラマ、特にC#言語で利用できるかどうかを知りたいと思います。C#アプリケーションにMS Office Smooth Typingを統合する方法はありますか?

もしそうなら、あなたはあなたの答えにドキュメントにリンクを投稿してください可能性もかもしれません使用例

ありがとうございました。

+2

何この「滑らかなタイピング」機能について教えてくださいです。キャレットのアニメーション効果を指していますか? – Dai

+2

@Dai私は入力中にカーソルをスライドさせるタイピングアニメーションを指しています。 MS Office Word 2010で何かを入力してからMS Office Word 2013に切り替えると、簡単に確認できます。 – Zignd

答えて

11

私はOfficeを所有していないので、その機能を見ることはできませんが、少し前に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(); 
} 
+0

そうです。素晴らしい。どうもありがとうございます。 –

+0

非常に驚くべきコード! @newStackExchangeInstanceが述べたように、新しい行に入力するためにEnterキーを押したときに発生する問題があります。カーソルはそれ以上の時間がかかるので、カーソルを戻すように修正できますか?デフォルトの速度ですか?私はこれが問題を解決するだろうと思っていれば、私はあなたに恩恵を与えます。 – Zignd

+0

ありがとうございます。それがあなたが見る唯一の問題ならば、その罰金:)。あなたがあまりにも速く入力すると、キャレットが "到着"する前に文字が表示されているように、それ以外のものはありません。とにかく、キャレットをアニメーション化しないようにコードを更新しました距離が遠すぎると、ゆっくりと移動するのではなく、次のラインに「ジャンプ」する必要があります。コードを使って楽しんでください;) – Imi

3

独自のアニメーションタイミング機能でSetCaretPosを使用します。前の場所と新しい目的の場所に基づいてキャレットの位置を補間する新しいスレッドを作成します。

関連する問題