OSDN Git Service

エラーメッセージのword wrapを止める
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / Win32API.cs
1 // Copyright (C) 2014 Kazuhiro Fujieda <fujieda@users.osdn.me>\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 \r
22 namespace KancolleSniffer\r
23 {\r
24     public class Win32API\r
25     {\r
26         [DllImport("user32.dll")]\r
27         private static extern bool SetForegroundWindow(IntPtr hWnd);\r
28 \r
29         [DllImport("user32.dll")]\r
30         private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);\r
31 \r
32         [DllImport("user32.dll")]\r
33         private static extern bool IsIconic(IntPtr hWnd);\r
34 \r
35         public static bool ProcessAlreadyExists()\r
36         {\r
37             var cur = Process.GetCurrentProcess();\r
38             var all = Process.GetProcessesByName(cur.ProcessName);\r
39             foreach (var p in all)\r
40             {\r
41                 if (cur.Id == p.Id)\r
42                     continue;\r
43                 if (p.MainModule.FileName != cur.MainModule.FileName)\r
44                     continue;\r
45                 if (IsIconic(p.MainWindowHandle))\r
46                     ShowWindowAsync(p.MainWindowHandle, 9); // SW_RESTORE\r
47                 else\r
48                     SetForegroundWindow(p.MainWindowHandle);\r
49                 return true;\r
50             }\r
51             return false;\r
52         }\r
53 \r
54         [DllImport("user32.dll")]\r
55         private static extern Int32 FlashWindowEx(ref FLASHWINFO pwfi);\r
56 \r
57         public static void FlashWindow(IntPtr handle)\r
58         {\r
59             var info = new FLASHWINFO();\r
60             info.cbSize = Convert.ToUInt32(Marshal.SizeOf(info));\r
61             info.hwnd = handle;\r
62             info.dwFlags = 3; // FLASHW_ALL\r
63             info.uCount = 3;\r
64             info.dwTimeout = 0;\r
65             FlashWindowEx(ref info);\r
66         }\r
67 \r
68         [StructLayout(LayoutKind.Sequential)]\r
69         private struct FLASHWINFO\r
70         {\r
71             public UInt32 cbSize;\r
72             public IntPtr hwnd;\r
73             public UInt32 dwFlags;\r
74             public UInt32 uCount;\r
75             public UInt32 dwTimeout;\r
76         }\r
77     }\r
78 }