OSDN Git Service

Version 1.10
[buragesnap/BurageSnap.git] / BurageSnap / Program.cs
1 // Copyright (C) 2015 Kazuhiro Fujieda <fujieda@users.osdn.me>
2 //
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
6 //
7 //    http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14
15 using System;
16 using System.Diagnostics;
17 using System.Linq;
18 using System.Runtime.InteropServices;
19 using System.Windows.Forms;
20
21 namespace BurageSnap
22 {
23     internal static class Program
24     {
25         /// <summary>
26         /// アプリケーションのメイン エントリ ポイントです。
27         /// </summary>
28         [STAThread]
29         private static void Main()
30         {
31             if (ProcessAlreadyExists())
32                 return;
33             Application.EnableVisualStyles();
34             Application.SetCompatibleTextRenderingDefault(false);
35             Application.Run(new FormMain());
36         }
37
38         public static bool ProcessAlreadyExists()
39         {
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);
43             if (proc == null)
44                 return false;
45             var hWnd = proc.MainWindowHandle;
46
47             if (IsIconic(hWnd))
48                 ShowWindowAsync(hWnd, 9); // SW_RESTORE
49             else
50                 SetForegroundWindow(hWnd);
51             return true;
52         }
53
54         [DllImport("user32.dll")]
55         private static extern bool SetForegroundWindow(IntPtr hWnd);
56
57         [DllImport("user32.dll")]
58         private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
59
60         [DllImport("user32.dll")]
61         private static extern bool IsIconic(IntPtr hWnd);
62     }
63 }