OSDN Git Service

多重起動時の動作を少し変更
[opentween/open-tween.git] / OpenTween / ApplicationEvents.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2007-2012 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
3 //           (c) 2008-2012 Moz (@syo68k)
4 //           (c) 2008-2012 takeshik (@takeshik) <http://www.takeshik.org/>
5 //           (c) 2010-2012 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
6 //           (c) 2010-2012 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
7 //           (c) 2012      Egtra (@egtra) <http://dev.activebasic.com/egtra/>
8 //           (c) 2012      kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
9 // All rights reserved.
10 // 
11 // This file is part of OpenTween.
12 // 
13 // This program is free software; you can redistribute it and/or modify it
14 // under the terms of the GNU General public License as published by the Free
15 // Software Foundation; either version 3 of the License, or (at your option)
16 // any later version.
17 // 
18 // This program is distributed in the hope that it will be useful, but
19 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General public License
21 // for more details.
22 // 
23 // You should have received a copy of the GNU General public License along
24 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
25 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
26 // Boston, MA 02110-1301, USA.
27
28 using System;
29 using System.IO;
30 using System.Diagnostics;
31 using System.Windows.Forms;
32 using System.Threading;
33 using System.Globalization;
34 using System.Reflection;
35
36 namespace OpenTween
37 {
38     internal class MyApplication
39     {
40         /// <summary>
41         /// アプリケーションのメイン エントリ ポイントです。
42         /// </summary>
43         [STAThread]
44         static int Main()
45         {
46             CheckSettingFilePath();
47             InitCulture();
48
49             string pt = Application.ExecutablePath.Replace("\\", "/") + "/" + Application.ProductName;
50             using (Mutex mt = new Mutex(false, pt))
51             {
52                 if (!mt.WaitOne(0, false))
53                 {
54                     var text = string.Format(MyCommon.ReplaceAppName(Properties.Resources.StartupText1), MyCommon.GetAssemblyName());
55                     MessageBox.Show(text, MyCommon.ReplaceAppName(Properties.Resources.StartupText2), MessageBoxButtons.OK, MessageBoxIcon.Information);
56
57                     ShowPreviousWindow();
58                     return 1;
59                 }
60
61                 Application.ThreadException += MyApplication_UnhandledException;
62
63                 Application.EnableVisualStyles();
64                 Application.SetCompatibleTextRenderingDefault(false);
65                 Application.Run(new TweenMain());
66
67                 mt.ReleaseMutex();
68
69                 return 0;
70             }
71         }
72
73         private static void ShowPreviousWindow()
74         {
75             // 実行中の同じアプリケーションのウィンドウ・ハンドルの取得
76             var prevProcess = Win32Api.GetPreviousProcess();
77             if (prevProcess == null)
78                 return;
79
80             if (prevProcess.MainWindowHandle != IntPtr.Zero)
81             {
82                 // 起動中のアプリケーションを最前面に表示
83                 Win32Api.WakeupWindow(prevProcess.MainWindowHandle);
84             }
85             else
86             {
87                 //プロセス特定は出来たが、ウィンドウハンドルが取得できなかった(アイコン化されている)
88                 //タスクトレイアイコンのクリックをエミュレート
89                 //注:アイコン特定はTooltipの文字列で行うため、多重起動時は先に見つけた物がアクティブになる
90                 Win32Api.ClickTasktrayIcon(Application.ProductName);
91             }
92         }
93
94         private static void MyApplication_UnhandledException(object sender, ThreadExceptionEventArgs e)
95         {
96             //GDI+のエラー原因を特定したい
97             if (e.Exception.Message != "A generic error occurred in GDI+." &&
98                e.Exception.Message != "GDI+ で汎用エラーが発生しました。")
99             {
100                 if (MyCommon.ExceptionOut(e.Exception))
101                 {
102                     Application.Exit();
103                 }
104             }
105         }
106
107         private static bool IsEqualCurrentCulture(string CultureName)
108         {
109             return Thread.CurrentThread.CurrentUICulture.Name.StartsWith(CultureName);
110         }
111
112         public static string CultureCode
113         {
114             get
115             {
116                 if (MyCommon.cultureStr == null)
117                 {
118                     var cfgCommon = SettingCommon.Load();
119                     MyCommon.cultureStr = cfgCommon.Language;
120                     if (MyCommon.cultureStr == "OS")
121                     {
122                         if (!IsEqualCurrentCulture("ja") &&
123                            !IsEqualCurrentCulture("en") &&
124                            !IsEqualCurrentCulture("zh-CN"))
125                         {
126                             MyCommon.cultureStr = "en";
127                         }
128                     }
129                 }
130                 return MyCommon.cultureStr;
131             }
132         }
133
134         public static void InitCulture(string code)
135         {
136             try
137             {
138                 Thread.CurrentThread.CurrentUICulture = new CultureInfo(code);
139             }
140             catch (Exception)
141             {
142             }
143         }
144         public static void InitCulture()
145         {
146             try
147             {
148                 if (CultureCode != "OS") Thread.CurrentThread.CurrentUICulture = new CultureInfo(CultureCode);
149             }
150             catch (Exception)
151             {
152             }
153         }
154
155         private static void CheckSettingFilePath()
156         {
157             if (File.Exists(Path.Combine(Application.StartupPath, "roaming")))
158             {
159                 MyCommon.settingPath = MySpecialPath.UserAppDataPath();
160             }
161             else
162             {
163                 MyCommon.settingPath = Application.StartupPath;
164             }
165         }
166     }
167 }