1 // Copyright (C) 2014 Kazuhiro Fujieda <fujieda@users.osdn.me>
\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
7 // http://www.apache.org/licenses/LICENSE-2.0
\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
16 using System.ComponentModel;
\r
17 using System.Diagnostics;
\r
18 using System.Runtime.InteropServices;
\r
20 namespace KancolleSniffer
\r
22 public class Win32API
\r
24 [DllImport("user32.dll")]
\r
25 private static extern bool SetForegroundWindow(IntPtr hWnd);
\r
27 [DllImport("user32.dll")]
\r
28 private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
\r
30 [DllImport("user32.dll")]
\r
31 private static extern bool IsIconic(IntPtr hWnd);
\r
33 public static bool ProcessAlreadyExists()
\r
37 var cur = Process.GetCurrentProcess();
\r
38 var all = Process.GetProcessesByName(cur.ProcessName);
\r
39 foreach (var p in all)
\r
43 if (p.MainModule.FileName != cur.MainModule.FileName)
\r
45 if (IsIconic(p.MainWindowHandle))
\r
46 ShowWindowAsync(p.MainWindowHandle, 9); // SW_RESTORE
\r
48 SetForegroundWindow(p.MainWindowHandle);
\r
53 * マルウェア対策ソフトが原因でMainModule.FileNameが失敗することがあり、
\r
54 * その場合はWin32Exceptionが発生する。
\r
56 catch (Win32Exception)
\r
62 [DllImport("user32.dll")]
\r
63 private static extern Int32 FlashWindowEx(ref FLASHWINFO pwfi);
\r
65 public static void FlashWindow(IntPtr handle)
\r
67 var info = new FLASHWINFO();
\r
68 info.cbSize = Convert.ToUInt32(Marshal.SizeOf(info));
\r
70 info.dwFlags = 3; // FLASHW_ALL
\r
73 FlashWindowEx(ref info);
\r
76 [StructLayout(LayoutKind.Sequential)]
\r
77 private struct FLASHWINFO
\r
79 public UInt32 cbSize;
\r
81 public UInt32 dwFlags;
\r
82 public UInt32 uCount;
\r
83 public UInt32 dwTimeout;
\r