OSDN Git Service

ヘッダのCondとExpのマウスカーソルを変える
[kancollesniffer/KancolleSniffer.git] / KancolleSniffer / Win32API.cs
1 // Copyright (C) 2014 Kazuhiro Fujieda <fujieda@users.osdn.me>\r
2 // \r
3 // Licensed under the Apache License, Version 2.0 (the "License");\r
4 // you may not use this file except in compliance with the License.\r
5 // You may obtain a copy of the License at\r
6 //\r
7 //    http://www.apache.org/licenses/LICENSE-2.0\r
8 //\r
9 // Unless required by applicable law or agreed to in writing, software\r
10 // distributed under the License is distributed on an "AS IS" BASIS,\r
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
12 // See the License for the specific language governing permissions and\r
13 // limitations under the License.\r
14 \r
15 using System;\r
16 using System.ComponentModel;\r
17 using System.Diagnostics;\r
18 using System.Runtime.InteropServices;\r
19 \r
20 namespace KancolleSniffer\r
21 {\r
22     public class Win32API\r
23     {\r
24         [DllImport("user32.dll")]\r
25         private static extern bool SetForegroundWindow(IntPtr hWnd);\r
26 \r
27         [DllImport("user32.dll")]\r
28         private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);\r
29 \r
30         [DllImport("user32.dll")]\r
31         private static extern bool IsIconic(IntPtr hWnd);\r
32 \r
33         public static bool ProcessAlreadyExists()\r
34         {\r
35             try\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             }\r
52             /*\r
53              * マルウェア対策ソフトが原因でMainModule.FileNameが失敗することがあり、\r
54              * その場合はWin32Exceptionが発生する。\r
55             */\r
56             catch (Win32Exception)\r
57             {\r
58             }\r
59             return false;\r
60         }\r
61 \r
62         [DllImport("user32.dll")]\r
63         private static extern Int32 FlashWindowEx(ref FLASHWINFO pwfi);\r
64 \r
65         public static void FlashWindow(IntPtr handle)\r
66         {\r
67             var info = new FLASHWINFO();\r
68             info.cbSize = Convert.ToUInt32(Marshal.SizeOf(info));\r
69             info.hwnd = handle;\r
70             info.dwFlags = 3; // FLASHW_ALL\r
71             info.uCount = 3;\r
72             info.dwTimeout = 0;\r
73             FlashWindowEx(ref info);\r
74         }\r
75 \r
76         [StructLayout(LayoutKind.Sequential)]\r
77         private struct FLASHWINFO\r
78         {\r
79             public UInt32 cbSize;\r
80             public IntPtr hwnd;\r
81             public UInt32 dwFlags;\r
82             public UInt32 uCount;\r
83             public UInt32 dwTimeout;\r
84         }\r
85     }\r
86 }