hm.NETの特徴は
マクロ自体の実行は一瞬で終了し、マクロが終了した後もプログラムが常駐する
というタイプのモジュール作成に向いているということです。 このことを確認してみましょう。
using System; using System.Windows.Forms; namespace HmTestClass06 { class MonitorForm : Form { Timer timer; public MonitorForm() { this.FormClosing += form_FormClosing; CreateTimer(); } private void CreateTimer() { timer = new Timer() { Interval = 1000 }; timer.Tick += timer_Tick; timer.Enabled = true; timer.Start(); } private void DeleteTimer() { if (timer != null) { timer.Stop(); timer = null; } } private void timer_Tick(object sender, EventArgs e) { System.Diagnostics.Trace.WriteLine("定期実行"); } private void form_FormClosing(object sender, FormClosingEventArgs e) { StopMonitor(); } private void StopMonitor() { try { DeleteTimer(); } catch (Exception e) { System.Diagnostics.Trace.WriteLine(e.Message); } } } public class Class6 { private static Form form; public static IntPtr CreateForm() { if (form == null || !form.Visible) { form = new MonitorForm(); } form.Show(); return (IntPtr)1; } private static void DestroyForm() { if (form != null) { form.Close(); form = null; } } public static IntPtr OnDetachMethod(IntPtr reason) { DestroyForm(); return (IntPtr)0; } } }
debuginfo 1; debuginfo "macro start\n"; #HMNET = loaddll( hidemarudir + @"\hm.NET.dll" ); #r = dllfuncw( #HMNET, "SetDetachMethod", currentmacrodirectory + @"\HmTestClass06.dll", "HmTestClass06.Class6", "OnDetachMethod" ); #r = dllfuncw( #HMNET, "CallMethod", currentmacrodirectory + @"\HmTestClass06.dll", "HmTestClass06.Class6", "CreateForm" ); debuginfo "macro end\n";
マクロ自体は一瞬で実行を終了した後 定期的にデバッグメッセージが出力されることがわかります。
この、
C#のstatic関数を実行するだけ
秀丸マクロの実行自体は終わっても、C#のdllの処理は続いている
というhm.NETとC#の組み合わせの特徴は、複雑なモジュールを作成する上では必須のパターンとなります。