Gnuplotを操る

http://d.hatena.ne.jp/KrdLab/20060528/1148815875
でもやったけど,今回はC#オンリーで書いてみようかと.
自分の復習を兼ねてます.


Spy++ で調べてみると,コマンド入力ウィンドウのウィンドウクラスは "wgnuplot_text" です.

基本的な方針としては,

  1. プロセス起動
  2. プロセスのメインスレッドに関連づけられている親ウィンドウを取得
  3. 子ウィンドウの中から "wgnuplot_text" を持つウィンドウを検索
  4. ウィンドウハンドルを取得
  5. PostMessage を用いて,コマンドを送信


なので,

  1. Process
  2. Process.MainWindowHandle
  3. P/Invoke で FindWindowEx
  4. P/Invoke で PostMessage


となります.
おそらく,一番オーソドックスな方法ではないかと.(2009/01/06 追記:より簡潔な方法は,コメント欄を参照してください)

http://msdn.microsoft.com/library/ja/default.asp?url=/library/ja/jpwinui/html/_win32_findwindowex.asp
http://msdn.microsoft.com/library/ja/default.asp?url=/library/ja/jpwinui/html/_win32_postmessage.asp

P/Invokeの宣言がまとめてあるサイト → http://www.pinvoke.net/

P/Invokeのサンプル → http://msdn2.microsoft.com/ja-jp/library/fzhhdwae(VS.80).aspx

成果物

(2013/02/05 追記: こちらに移りました → https://github.com/krdlab/wgnuplot4cs)

(興味のある方は,是非改造してください.そして,それを私にください.f^_^; <おい!)
(2008/03/19 配置場所を変えました.)

使い方

  1. 自分のプロジェクトに Wgnuplot.dll を参照させる
  2. サンプルみたいに呼び出す
  3. 自分が作成した実行ファイルと同じフォルダに wgnuplot.exe を配置する
  4. 実行!

サンプル

namespace KrdLab.Test
{
    class Program
    {
        static void Main(string[] args)
        {
            using (Wgnuplot plot = new Wgnuplot())
            {
                plot.Send("splot x**2 + y**2");
                System.Console.Read();
            }
        }
    }
}