-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm2.cs
More file actions
200 lines (176 loc) · 6.56 KB
/
Form2.cs
File metadata and controls
200 lines (176 loc) · 6.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AlarmTo
{
public partial class BootForm : Form
{
private static BootForm _default;
private string thePID;
private string theTitle;
private bool isOneTimeInstance = false;
private MainForm mainAlarmForm;
private FormHelp theFormHelp;
public static BootForm Default
{
get
{
// 如果表單不存在或已被銷毀,就重新 new 一個
if (_default == null || _default.IsDisposed)
{
_default = new BootForm();
}
return _default;
}
}
public BootForm()
{
InitializeComponent();
this.Width = 0;
this.Height= 0;
_default = this;
}
private void Form2_Load(object sender, EventArgs e)
{
string[] argv = Environment.GetCommandLineArgs(); // argv[0] = 程式路徑
for (int i = 1; i < argv.Length; i++)
{
string token = argv[i].TrimStart('/', '-');
if (string.Equals(token, "Instance", StringComparison.OrdinalIgnoreCase))
{
isOneTimeInstance = true;
break;
}
}
string tooltipStr = "\nDouble-Click→Main\nRight-Click→Menu";
thePID = GetFormattedHexPid();
mainAlarmForm = new MainForm(this);
theFormHelp = new FormHelp();
mainAlarmForm.isOneTimeInstance = isOneTimeInstance;
mainAlarmForm.thePID = thePID;
if (isOneTimeInstance)
{
thePID = "Instance-" + thePID;
notifyIcon1.Text = "AlarmTo [" + thePID.ToString() + "]" + tooltipStr;
RefreshIcon(0, false);
mainAlarmForm.SelectIcon(0);
}
else
{
if (Properties.Settings.Default.AlarmTitle == "" || Properties.Settings.Default.AlarmTitle == null)
{
notifyIcon1.Text = "AlarmTo [" + thePID.ToString() + "]" + tooltipStr;
}
else
{
theTitle = Properties.Settings.Default.AlarmTitle;
notifyIcon1.Text = "AlarmTo [" + theTitle + "]" + tooltipStr;
}
}
GC.Collect();
GC.WaitForPendingFinalizers();
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
ShowMain();
}
private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
{
// 點擊左鍵時,手動顯示選單
// 必須知道滑鼠位置才能顯示
//ContextMenuStrip1.Show(Control.MousePosition);
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void showMenuItem_Click(object sender, EventArgs e)
{
ShowMain();
}
private void ShowMain()
{
mainAlarmForm.Show();
mainAlarmForm.Activate();
mainAlarmForm.LastWindowIsVisible = true;
}
public static string GetFormattedHexPid()
{
// 1. 獲取當前行程 ID (PID)
int currentProcessId = Process.GetCurrentProcess().Id;
// 2. 將 PID 轉換為大寫 Hex 字串 ("X")
// "X" 標準格式字串表示十六進制大寫。
string hexString = currentProcessId.ToString("X");
// 3. 補足 '0' 至至少 4 位長度
// PadLeft(4, '0') 確保字串長度至少為 4。
// 如果 hexString 已經是 5 位或更長 (例如 "1B34A"),則不會被填充。
return hexString.PadLeft(4, '0');
}
public static void RelaunchWithInstanceArg()
{
try
{
// 取得自己的執行檔路徑
string exePath = Application.ExecutablePath;
// 準備啟動參數
string arguments = "/Instance";
ProcessStartInfo psi = new ProcessStartInfo
{
FileName = exePath,
Arguments = arguments,
UseShellExecute = false
};
Process.Start(psi);
}
catch (Exception ex)
{
MessageBox.Show("Failed to relaunch: " + ex.Message);
}
}
public void RefreshIcon(int AlarmIconNum, bool AlarmIsOn)
{
switch (AlarmIconNum)
{
case 0:
if (AlarmIsOn) { this.notifyIcon1.Icon = Properties.Resources.Bell_oti_on; }
else { this.notifyIcon1.Icon = Properties.Resources.Bell_oti_off; }
break;
case 1:
if (AlarmIsOn) { this.notifyIcon1.Icon = Properties.Resources.Bell_01_on; }
else { this.notifyIcon1.Icon = Properties.Resources.Bell_01_off; }
break;
case 2:
if (AlarmIsOn) { this.notifyIcon1.Icon = Properties.Resources.Bell_02_on; }
else { this.notifyIcon1.Icon = Properties.Resources.Bell_02_off; }
break;
case 3:
if (AlarmIsOn) { this.notifyIcon1.Icon = Properties.Resources.Bell_03_on; }
else { this.notifyIcon1.Icon = Properties.Resources.Bell_03_off; }
break;
case 4:
if (AlarmIsOn) { this.notifyIcon1.Icon = Properties.Resources.Bell_04_on; }
else { this.notifyIcon1.Icon = Properties.Resources.Bell_04_off; }
break;
case 5:
if (AlarmIsOn) { this.notifyIcon1.Icon = Properties.Resources.Bell_05_on; }
else { this.notifyIcon1.Icon = Properties.Resources.Bell_05_off; }
break;
}
}
private void runInstMenuItem_Click(object sender, EventArgs e)
{
RelaunchWithInstanceArg();
}
private void helpMenuItem_Click(object sender, EventArgs e)
{
theFormHelp.Show();
}
}
}