OSDN Git Service

バージョン1.0
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / Program.cs
1 // Copyright (C) 2013 Kazuhiro Fujieda <fujieda@users.sourceforge.jp>\r
2 // \r
3 // This program is part of KancolleSniffer.\r
4 //\r
5 // KancolleSniffer is free software: you can redistribute it and/or modify\r
6 // it under the terms of the GNU General Public License as published by\r
7 // the Free Software Foundation, either version 3 of the License, or\r
8 // (at your option) any later version.\r
9 //\r
10 // This program is distributed in the hope that it will be useful,\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 // GNU General Public License for more details.\r
14 //\r
15 // You should have received a copy of the GNU General Public License\r
16 // along with this program; if not, see <http://www.gnu.org/licenses/>.\r
17 \r
18 using System;\r
19 using System.Diagnostics;\r
20 using System.Runtime.InteropServices;\r
21 using System.Windows.Forms;\r
22 \r
23 namespace KancolleSniffer\r
24 {\r
25     internal static class Program\r
26     {\r
27         /// <summary>\r
28         /// アプリケーションのメイン エントリ ポイントです。\r
29         /// </summary>\r
30         [STAThread]\r
31         private static void Main()\r
32         {\r
33             if (Win32API.ProcessAlreadyExists())\r
34                 return;\r
35             Application.EnableVisualStyles();\r
36             Application.SetCompatibleTextRenderingDefault(false);\r
37             Application.Run(new MainForm());\r
38         }\r
39     }\r
40 \r
41     /// <summary>\r
42     /// Win32APIを実行するクラス\r
43     /// </summary>\r
44     public class Win32API\r
45     {\r
46         [DllImport("user32.dll")]\r
47         private static extern bool SetForegroundWindow(IntPtr hWnd);\r
48 \r
49         [DllImport("user32.dll")]\r
50         private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);\r
51 \r
52         [DllImport("user32.dll")]\r
53         private static extern bool IsIconic(IntPtr hWnd);\r
54 \r
55         /// <summary>\r
56         /// 同じアプリケーションがすでに起動しているか調べる。起動していたら最前面に表示する。\r
57         /// </summary>\r
58         /// <returns>起動していたらtrue</returns>\r
59         public static bool ProcessAlreadyExists()\r
60         {\r
61             var cur = Process.GetCurrentProcess();\r
62             var all = Process.GetProcessesByName(cur.ProcessName);\r
63             foreach (var p in all)\r
64             {\r
65                 if (cur.Id == p.Id)\r
66                     continue;\r
67                 if (p.MainModule.FileName != cur.MainModule.FileName)\r
68                     continue;\r
69                 if (IsIconic(p.MainWindowHandle))\r
70                     ShowWindowAsync(p.MainWindowHandle, 9); // SW_RESTORE\r
71                 else\r
72                     SetForegroundWindow(p.MainWindowHandle);\r
73                 return true;\r
74             }\r
75             return false;\r
76         }\r
77     }\r
78 }