// Copyright (C) 2015 Kazuhiro Fujieda // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. using System; using System.Diagnostics; using System.Linq; using System.Runtime.InteropServices; using System.Windows.Forms; namespace BurageSnap { internal static class Program { /// /// アプリケーションのメイン エントリ ポイントです。 /// [STAThread] private static void Main() { if (ProcessAlreadyExists()) return; Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new FormMain()); } public static bool ProcessAlreadyExists() { var cur = Process.GetCurrentProcess(); var all = Process.GetProcessesByName(cur.ProcessName); var proc = all.FirstOrDefault(p => p.Id != cur.Id && p.MainModule.FileName == cur.MainModule.FileName); if (proc == null) return false; var hWnd = proc.MainWindowHandle; if (IsIconic(hWnd)) ShowWindowAsync(hWnd, 9); // SW_RESTORE else SetForegroundWindow(hWnd); return true; } [DllImport("user32.dll")] private static extern bool SetForegroundWindow(IntPtr hWnd); [DllImport("user32.dll")] private static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow); [DllImport("user32.dll")] private static extern bool IsIconic(IntPtr hWnd); } }