1 // Copyright (C) 2015 Kazuhiro Fujieda <fujieda@users.osdn.me>
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
16 using System.Diagnostics;
18 using System.Runtime.InteropServices;
19 using System.Windows.Forms;
23 internal static class Program
26 /// アプリケーションのメイン エントリ ポイントです。
29 private static void Main()
31 if (ProcessAlreadyExists())
33 Application.EnableVisualStyles();
34 Application.SetCompatibleTextRenderingDefault(false);
35 Application.Run(new FormMain());
38 public static bool ProcessAlreadyExists()
40 var cur = Process.GetCurrentProcess();
41 var all = Process.GetProcessesByName(cur.ProcessName);
42 var proc = all.FirstOrDefault(p => p.Id != cur.Id && p.MainModule.FileName == cur.MainModule.FileName);
45 var hWnd = proc.MainWindowHandle;
48 ShowWindowAsync(hWnd, 9); // SW_RESTORE
50 SetForegroundWindow(hWnd);
54 [DllImport("user32.dll")]
55 private static extern bool SetForegroundWindow(IntPtr hWnd);
57 [DllImport("user32.dll")]
58 private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
60 [DllImport("user32.dll")]
61 private static extern bool IsIconic(IntPtr hWnd);