OpenTweenのfork
修訂 | ca70b006fcb40c387ebbef5a9f6514ff7a508a0c (tree) |
---|---|
時間 | 2012-03-13 00:22:51 |
作者 | Egtra <yusuke.ichinohe@gmai...> |
Commiter | Kimura Youichi |
Port ApplicationEvents.vb to C#
@@ -1,160 +0,0 @@ | ||
1 | -' Tween - Client of Twitter | |
2 | -' Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com> | |
3 | -' (c) 2008-2011 Moz (@syo68k) | |
4 | -' (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/> | |
5 | -' (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/> | |
6 | -' (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow> | |
7 | -' All rights reserved. | |
8 | -' | |
9 | -' This file is part of Tween. | |
10 | -' | |
11 | -' This program is free software; you can redistribute it and/or modify it | |
12 | -' under the terms of the GNU General Public License as published by the Free | |
13 | -' Software Foundation; either version 3 of the License, or (at your option) | |
14 | -' any later version. | |
15 | -' | |
16 | -' This program is distributed in the hope that it will be useful, but | |
17 | -' WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
18 | -' or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
19 | -' for more details. | |
20 | -' | |
21 | -' You should have received a copy of the GNU General Public License along | |
22 | -' with this program. If not, see <http://www.gnu.org/licenses/>, or write to | |
23 | -' the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, | |
24 | -' Boston, MA 02110-1301, USA. | |
25 | - | |
26 | -Option Strict On | |
27 | - | |
28 | -Imports System.IO | |
29 | -Imports System.Diagnostics | |
30 | -Imports System.Threading.Thread | |
31 | - | |
32 | -Namespace My | |
33 | - | |
34 | - ' 次のイベントは MyApplication に対して利用できます: | |
35 | - ' | |
36 | - ' Startup: アプリケーションが開始されたとき、スタートアップ フォームが作成される前に発生します。 | |
37 | - ' Shutdown: アプリケーション フォームがすべて閉じられた後に発生します。このイベントは、通常の終了以外の方法でアプリケーションが終了されたときには発生しません。 | |
38 | - ' UnhandledException: ハンドルされていない例外がアプリケーションで発生したときに発生するイベントです。 | |
39 | - ' StartupNextInstance: 単一インスタンス アプリケーションが起動され、それが既にアクティブであるときに発生します。 | |
40 | - ' NetworkAvailabilityChanged: ネットワーク接続が接続されたとき、または切断されたときに発生します。 | |
41 | - Partial Friend Class MyApplication | |
42 | - Private Shared mt As System.Threading.Mutex | |
43 | - | |
44 | - Private Sub MyApplication_Shutdown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shutdown | |
45 | - Try | |
46 | - If mt IsNot Nothing Then | |
47 | - mt.ReleaseMutex() | |
48 | - mt.Close() | |
49 | - mt = Nothing | |
50 | - End If | |
51 | - Catch ex As Exception | |
52 | - | |
53 | - End Try | |
54 | - End Sub | |
55 | - | |
56 | - Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup | |
57 | - CheckSettingFilePath() | |
58 | - InitCulture() | |
59 | - | |
60 | - Dim pt As String = Application.Info.DirectoryPath.Replace("\", "/") + "/" + Application.Info.ProductName | |
61 | - mt = New System.Threading.Mutex(False, pt) | |
62 | - Try | |
63 | - If Not mt.WaitOne(0, False) Then | |
64 | - ' 実行中の同じアプリケーションのウィンドウ・ハンドルの取得 | |
65 | - Dim prevProcess As Process = GetPreviousProcess() | |
66 | - If prevProcess IsNot Nothing AndAlso _ | |
67 | - IntPtr.op_Inequality(prevProcess.MainWindowHandle, IntPtr.Zero) Then | |
68 | - ' 起動中のアプリケーションを最前面に表示 | |
69 | - WakeupWindow(prevProcess.MainWindowHandle) | |
70 | - Else | |
71 | - If prevProcess IsNot Nothing Then | |
72 | - 'プロセス特定は出来たが、ウィンドウハンドルが取得できなかった(アイコン化されている) | |
73 | - 'タスクトレイアイコンのクリックをエミュレート | |
74 | - '注:アイコン特定はTooltipの文字列で行うため、多重起動時は先に見つけた物がアクティブになる | |
75 | - Dim rslt As Boolean = ClickTasktrayIcon("Tween") | |
76 | - If Not rslt Then | |
77 | - ' 警告を表示(見つからない、またはその他の原因で失敗) | |
78 | - MessageBox.Show(My.Resources.StartupText1, My.Resources.StartupText2, MessageBoxButtons.OK, MessageBoxIcon.Information) | |
79 | - End If | |
80 | - Else | |
81 | - ' 警告を表示(プロセス見つからない場合) | |
82 | - MessageBox.Show(My.Resources.StartupText1, My.Resources.StartupText2, MessageBoxButtons.OK, MessageBoxIcon.Information) | |
83 | - End If | |
84 | - | |
85 | - End If | |
86 | - '起動キャンセル | |
87 | - e.Cancel = True | |
88 | - Try | |
89 | - mt.ReleaseMutex() | |
90 | - mt.Close() | |
91 | - mt = Nothing | |
92 | - Catch ex As Exception | |
93 | - | |
94 | - End Try | |
95 | - Exit Sub | |
96 | - End If | |
97 | - Catch ex As Exception | |
98 | - End Try | |
99 | - | |
100 | - End Sub | |
101 | - | |
102 | - Private Sub MyApplication_UnhandledException(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.UnhandledExceptionEventArgs) Handles Me.UnhandledException | |
103 | - 'GDI+のエラー原因を特定したい | |
104 | - If e.Exception.Message <> "A generic error occurred in GDI+." AndAlso _ | |
105 | - e.Exception.Message <> "GDI+ で汎用エラーが発生しました。" Then | |
106 | - e.ExitApplication = ExceptionOut(e.Exception) | |
107 | - Else | |
108 | - e.ExitApplication = False | |
109 | - End If | |
110 | - End Sub | |
111 | - | |
112 | - Private Function IsEqualCurrentCulture(ByVal CultureName As String) As Boolean | |
113 | - Return CurrentThread.CurrentUICulture.Name.StartsWith(CultureName) | |
114 | - End Function | |
115 | - | |
116 | - Public ReadOnly Property CultureCode() As String | |
117 | - Get | |
118 | - If cultureStr Is Nothing Then | |
119 | - Dim cfgCommon As SettingCommon = SettingCommon.Load() | |
120 | - cultureStr = cfgCommon.Language | |
121 | - If cultureStr = "OS" Then | |
122 | - If Not IsEqualCurrentCulture("ja") AndAlso _ | |
123 | - Not IsEqualCurrentCulture("en") AndAlso _ | |
124 | - Not IsEqualCurrentCulture("zh-CN") Then | |
125 | - cultureStr = "en" | |
126 | - End If | |
127 | - End If | |
128 | - End If | |
129 | - Return cultureStr | |
130 | - End Get | |
131 | - End Property | |
132 | - | |
133 | - Public Overloads Sub InitCulture(ByVal code As String) | |
134 | - Try | |
135 | - ChangeUICulture(code) | |
136 | - Catch ex As Exception | |
137 | - | |
138 | - End Try | |
139 | - End Sub | |
140 | - Public Overloads Sub InitCulture() | |
141 | - Try | |
142 | - If Me.CultureCode <> "OS" Then ChangeUICulture(Me.CultureCode) | |
143 | - Catch ex As Exception | |
144 | - | |
145 | - End Try | |
146 | - End Sub | |
147 | - | |
148 | - Private Sub CheckSettingFilePath() | |
149 | - If File.Exists(Path.Combine(Application.Info.DirectoryPath, "roaming")) Then | |
150 | - MyCommon.settingPath = MySpecialPath.UserAppDataPath() | |
151 | - Else | |
152 | - MyCommon.settingPath = Application.Info.DirectoryPath | |
153 | - End If | |
154 | - | |
155 | - End Sub | |
156 | - | |
157 | - End Class | |
158 | - | |
159 | -End Namespace | |
160 | - |
@@ -115,7 +115,6 @@ | ||
115 | 115 | <Import Include="System.Windows.Forms" /> |
116 | 116 | </ItemGroup> |
117 | 117 | <ItemGroup> |
118 | - <Compile Include="ApplicationEvents.vb" /> | |
119 | 118 | <Compile Include="My Project\AssemblyInfo.vb" /> |
120 | 119 | <Compile Include="My Project\Application.Designer.vb"> |
121 | 120 | <AutoGen>True</AutoGen> |
@@ -0,0 +1,198 @@ | ||
1 | +// Tween - 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 | +// All rights reserved. | |
9 | +// | |
10 | +// This file is part of Tween. | |
11 | +// | |
12 | +// This program is free software; you can redistribute it and/or modify it | |
13 | +// under the terms of the GNU General public License as published by the Free | |
14 | +// Software Foundation; either version 3 of the License, or (at your option) | |
15 | +// any later version. | |
16 | +// | |
17 | +// This program is distributed in the hope that it will be useful, but | |
18 | +// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
19 | +// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General public License | |
20 | +// for more details. | |
21 | +// | |
22 | +// You should have received a copy of the GNU General public License along | |
23 | +// with this program. If not, see <http://www.gnu.org/licenses/>, or write to | |
24 | +// the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor, | |
25 | +// Boston, MA 02110-1301, USA. | |
26 | + | |
27 | +using System; | |
28 | +using System.IO; | |
29 | +using System.Diagnostics; | |
30 | +using System.Threading.Thread; | |
31 | +using System.Windows.Forms; | |
32 | +using System.Threading; | |
33 | +using System.Globalization; | |
34 | +using System.Reflection; | |
35 | + | |
36 | +namespace Tween | |
37 | +{ | |
38 | + internal class MyApplication | |
39 | + { | |
40 | + static MyApplication() | |
41 | + { | |
42 | + MyApplication_Startup(); | |
43 | + Application.ApplicationExit += MyApplication_Shutdown; | |
44 | + Application.ThreadException += MyApplication_UnhandledException; | |
45 | + } | |
46 | + | |
47 | + private static System.Threading.Mutex mt; | |
48 | + | |
49 | + private static void MyApplication_Shutdown(object sender, EventArgs e) | |
50 | + { | |
51 | + try | |
52 | + { | |
53 | + if (mt != null) | |
54 | + { | |
55 | + mt.ReleaseMutex(); | |
56 | + mt.Close(); | |
57 | + mt = null; | |
58 | + } | |
59 | + } | |
60 | + catch (Exception) | |
61 | + { | |
62 | + } | |
63 | + } | |
64 | + | |
65 | + private static void MyApplication_Startup() | |
66 | + { | |
67 | + CheckSettingFilePath(); | |
68 | + InitCulture(); | |
69 | + | |
70 | + var directoryPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); | |
71 | + var product = (AssemblyProductAttribute)Attribute.GetCustomAttribute(Assembly.GetExecutingAssembly(), typeof(AssemblyProductAttribute)); | |
72 | + var pt = directoryPath.Replace("\\", "/") + "/" + product.Product; | |
73 | + mt = new System.Threading.Mutex(false, pt); | |
74 | + try | |
75 | + { | |
76 | + if (!mt.WaitOne(0, false)) | |
77 | + { | |
78 | + // 実行中の同じアプリケーションのウィンドウ・ハンドルの取得 | |
79 | + var prevProcess = Win32Api.GetPreviousProcess(); | |
80 | + if (prevProcess != null && prevProcess.MainWindowHandle == IntPtr.Zero) | |
81 | + { | |
82 | + // 起動中のアプリケーションを最前面に表示 | |
83 | + Win32Api.WakeupWindow(prevProcess.MainWindowHandle); | |
84 | + } | |
85 | + else | |
86 | + { | |
87 | + if (prevProcess != null) | |
88 | + { | |
89 | + //プロセス特定は出来たが、ウィンドウハンドルが取得できなかった(アイコン化されている) | |
90 | + //タスクトレイアイコンのクリックをエミュレート | |
91 | + //注:アイコン特定はTooltipの文字列で行うため、多重起動時は先に見つけた物がアクティブになる | |
92 | + var rslt = Win32Api.ClickTasktrayIcon("Tween"); | |
93 | + if (!rslt) | |
94 | + { | |
95 | + // 警告を表示(見つからない、またはその他の原因で失敗) | |
96 | + MessageBox.Show(Properties.Resources.StartupText1, Properties.Resources.StartupText2, MessageBoxButtons.OK, MessageBoxIcon.Information); | |
97 | + } | |
98 | + } | |
99 | + else | |
100 | + { | |
101 | + // 警告を表示(プロセス見つからない場合) | |
102 | + MessageBox.Show(Properties.Resources.StartupText1, Properties.Resources.StartupText2, MessageBoxButtons.OK, MessageBoxIcon.Information); | |
103 | + } | |
104 | + | |
105 | + } | |
106 | + //起動キャンセル | |
107 | + e.Cancel = true; | |
108 | + try | |
109 | + { | |
110 | + mt.ReleaseMutex(); | |
111 | + mt.Close(); | |
112 | + mt = null; | |
113 | + } | |
114 | + catch (Exception) | |
115 | + { | |
116 | + } | |
117 | + } | |
118 | + } | |
119 | + catch (Exception) | |
120 | + { | |
121 | + } | |
122 | + } | |
123 | + | |
124 | + private static void MyApplication_UnhandledException(object sender, ThreadExceptionEventArgs e) | |
125 | + { | |
126 | + //GDI+のエラー原因を特定したい | |
127 | + if (e.Exception.Message != "A generic error occurred in GDI+." && | |
128 | + e.Exception.Message != "GDI+ で汎用エラーが発生しました。") | |
129 | + { | |
130 | + if (MyCommon.ExceptionOut(e.Exception)) | |
131 | + { | |
132 | + Application.Exit(); | |
133 | + } | |
134 | + } | |
135 | + } | |
136 | + | |
137 | + private static bool IsEqualCurrentCulture(string CultureName) | |
138 | + { | |
139 | + return Thread.CurrentThread.CurrentUICulture.Name.StartsWith(CultureName); | |
140 | + } | |
141 | + | |
142 | + public static string CultureCode | |
143 | + { | |
144 | + get | |
145 | + { | |
146 | + if (MyCommon.cultureStr == null) | |
147 | + { | |
148 | + var cfgCommon = SettingCommon.Load(); | |
149 | + MyCommon.cultureStr = cfgCommon.Language; | |
150 | + if (MyCommon.cultureStr == "OS") | |
151 | + { | |
152 | + if (!IsEqualCurrentCulture("ja") && | |
153 | + !IsEqualCurrentCulture("en") && | |
154 | + !IsEqualCurrentCulture("zh-CN")) | |
155 | + { | |
156 | + MyCommon.cultureStr = "en"; | |
157 | + } | |
158 | + } | |
159 | + } | |
160 | + return MyCommon.cultureStr; | |
161 | + } | |
162 | + } | |
163 | + | |
164 | + public static void InitCulture(string code) | |
165 | + { | |
166 | + try | |
167 | + { | |
168 | + Thread.CurrentThread.CurrentUICulture = new CultureInfo(code); | |
169 | + } | |
170 | + catch (Exception) | |
171 | + { | |
172 | + } | |
173 | + } | |
174 | + public static void InitCulture() | |
175 | + { | |
176 | + try | |
177 | + { | |
178 | + if (CultureCode != "OS") Thread.CurrentThread.CurrentUICulture = new CultureInfo(CultureCode); | |
179 | + } | |
180 | + catch (Exception) | |
181 | + { | |
182 | + } | |
183 | + } | |
184 | + | |
185 | + private static void CheckSettingFilePath() | |
186 | + { | |
187 | + var directoryPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); | |
188 | + if (File.Exists(Path.Combine(directoryPath, "roaming"))) | |
189 | + { | |
190 | + MyCommon.settingPath = MySpecialPath.UserAppDataPath(); | |
191 | + } | |
192 | + else | |
193 | + { | |
194 | + MyCommon.settingPath = directoryPath; | |
195 | + } | |
196 | + } | |
197 | + } | |
198 | +} |
@@ -50,6 +50,9 @@ | ||
50 | 50 | <SubType>Component</SubType> |
51 | 51 | </Compile> |
52 | 52 | <Compile Include="ApiInformation.cs" /> |
53 | + <Compile Include="ApplicationEvents.cs"> | |
54 | + <SubType>Code</SubType> | |
55 | + </Compile> | |
53 | 56 | <Compile Include="ApplicationSettings.cs" /> |
54 | 57 | <Compile Include="AtIdSupplement.cs"> |
55 | 58 | <SubType>Form</SubType> |