これを解決するには多くの方法があります。つまり、プログラムを実行してフォーカスするスタートアップ コンソール アプリで実行できます。
[STAThread]
static void Main(string[] args)
{
System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
myProcess.StartInfo.FileName = "calc";
myProcess.Start();
IntPtr hWnd = myProcess.Handle;
SetFocus(new HandleRef(null, hWnd));
}
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern IntPtr SetFocus(HandleRef hWnd);
Windows サービス アプリケーションをホストし、タイマー チェックを使用してアプリが有効でフォーカスされているかどうかを確認するか、ホットキーを使用してフォーカスを戻すことができます: http://www.codeproject.com/KB/miscctrl/ashsimplehotkeys.aspx
編集済み
これはコンソール アプリケーションであり、アプリを有効に保ち、フォーカスを維持します (テスト済み)。vista で何かが変更され、サービスから見つめるとフォームが見えなくなるため、Windows サービスのウォークアラウンドを見つける必要があります :P
static Process myProcess;
[STAThread]
static void Main(string[] args)
{
for (int i = 0; i < 10000; i++)
{
//count how many procesess with this name are active if more than zero its still alive
Process[] proc = Process.GetProcessesByName("myprog");
if (proc.Length > 0)
{
//its alive check if it has focus
if (proc[0].MainWindowHandle != GetForegroundWindow())
{
SetFocus(proc[0].MainWindowHandle);
}
}
//no process start new one and focus on it
else
{
myProcess = new Process();
myProcess.StartInfo.FileName = "C:\\aa\\myprog.exe";
myProcess.Start();
SetFocus(myProcess.Handle);
}
Thread.Sleep(1000);
}
}
private static void SetFocus(IntPtr handle)
{
SwitchToThisWindow(handle, true);
}
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", SetLastError = true)]
public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);