Skip to content

Commit 1bd546f

Browse files
committed
Fixed Button issue, enable darkmode, fixed multiple instances
1 parent 3436380 commit 1bd546f

2 files changed

Lines changed: 57 additions & 36 deletions

File tree

Internet Check/Form1.Designer.cs

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Internet Check/Form1.cs

Lines changed: 56 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System.IO;
55
using System.Net.NetworkInformation;
66
using System.Threading;
7-
using Microsoft.Win32;
7+
using System.Threading;
88
using System.Linq;
99
using System.Runtime.InteropServices;
1010
using System.Drawing;
@@ -20,7 +20,6 @@ public Form1()
2020
{
2121
Run();
2222
Application.Exit();
23-
//MessageBox.Show("Exit");
2423
}
2524
else
2625
{
@@ -33,11 +32,12 @@ private void formStart ()
3332
InitializeComponent();
3433
this.textBoxInterval.Text = Properties.Settings.Default.SettingInterval.ToString();
3534
notifyIcon1.Visible = true;
35+
this.button1.Text = "Start";
3636

3737
//this.textBoxInterval.TabStop = false; //to disable the highlight in textBoxInterval which sometimes occure
3838
textBoxInterval.SelectionStart = 0;
3939
textBoxInterval.SelectionLength = textBoxInterval.Text.Length;
40-
bool Darkmode = false;
40+
bool Darkmode = true;
4141
if (Darkmode)
4242
{
4343
DarkmodeForm();
@@ -104,21 +104,21 @@ private void ClickEvent()
104104
Thread.Sleep(2700);
105105
Thread.CurrentThread.IsBackground = true;
106106
this.panelError.BeginInvoke((MethodInvoker)delegate () { this.panelError.Visible = false; ; });
107-
108107
}).Start();
109108
}
110109
}
111110
private void tTimer()
112111
{
113-
DateTime jetzt = DateTime.Now;
112+
DateTime now = DateTime.Now;
114113
if (countclick % 2 == 1)
115114
{
115+
this.button1.Text = "Stop";
116116
this.textBoxInterval.Enabled = false;
117117
this.buttonClear.Enabled = false;
118-
jetzt = DateTime.Now;
118+
now = DateTime.Now;
119119
var startTimeSpan = TimeSpan.Zero;
120120
var periodTimeSpan = TimeSpan.FromSeconds(Properties.Settings.Default.SettingInterval);
121-
File.AppendAllText("Internetabbrüche.txt", "########### Program started at " + jetzt.ToString() + " ###########" + Environment.NewLine);
121+
File.AppendAllText("Internetabbrüche.txt", "########### Program started at " + now.ToString() + " ###########" + Environment.NewLine);
122122
this.labelRunning.Text = "Running . . .";
123123
timer = new System.Threading.Timer((d) =>
124124
{
@@ -127,8 +127,9 @@ private void tTimer()
127127
}
128128
else
129129
{
130+
this.button1.Text = "Start";
130131
timer.Dispose();
131-
File.AppendAllText("Internetabbrüche.txt", "########### Program stopped at " + jetzt.ToString() + " ###########" + Environment.NewLine + Environment.NewLine);
132+
File.AppendAllText("Internetabbrüche.txt", "########### Program stopped at " + now.ToString() + " ###########" + Environment.NewLine + Environment.NewLine);
132133
this.textBoxInterval.Enabled = true;
133134
this.buttonClear.Enabled = true;
134135
this.labelRunning.Text = "Waiting . . .";
@@ -144,9 +145,9 @@ private void Checker()
144145
}
145146
else
146147
{
148+
//Activate this if every ping should be written into the file
147149
//File.AppendAllText("Internetabbrüche.txt", jetzt.ToString() + " " + "Internet ist da" + Environment.NewLine);
148150
}
149-
150151
}
151152
public bool ping()
152153
{
@@ -191,14 +192,11 @@ private void buttonOpen_Click(object sender, EventArgs e)
191192
if (File.Exists("Internetabbrüche.txt"))
192193
{
193194
Process.Start("Internetabbrüche.txt");
194-
195195
} else
196196
{
197197
File.CreateText("Internetabbrüche.txt");
198198
Process.Start("Internetabbrüche.txt");
199-
200-
}
201-
199+
}
202200
}
203201

204202
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
@@ -215,7 +213,6 @@ private void Form1_FormClosing(object sender, FormClosingEventArgs e)
215213
catch
216214
{
217215
}
218-
219216
}
220217
}
221218

@@ -237,45 +234,68 @@ private void Form1_Resize(object sender, EventArgs e)
237234
*/
238235

239236
}
240-
/*
241-
private void textBoxInterval_KeyDown(object sender, KeyEventArgs e)
242-
{
243-
if (e.KeyValue == (char)Keys.Enter)
244-
{
245-
//ClickEvent();
246-
}
247-
else
248-
{
249-
250-
}
251-
}
252-
*/
253-
254-
237+
//Author unknown
255238
[DllImport("user32.dll")]
256239
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
257240

258-
//private const int SwShowmaximized = 10; //funktioniert nur wenn in Taskbar
259-
private const int ShowWindowFuntion = 3;
241+
private const int ShowWindowFuntion = 9;
260242
private void Run()
261243
{
262244
Process[] processlist = Process.GetProcesses();
263245

264246
foreach (Process process in processlist.Where(process => process.ProcessName == "Internet Check"))
265247
{
266-
//MessageBox.Show("Gefunden");
267-
this.Close();
268248
ShowWindow(Process.GetProcessById(process.Id).MainWindowHandle, ShowWindowFuntion); //https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow
249+
WindowHelper.BringProcessToFront(process);
250+
this.Close();
269251
}
270252
}
271253

272254
private void DarkmodeForm()
273-
{
274-
255+
{
275256
this.BackColor = Color.FromArgb(56, 55, 55);
257+
this.button1.ForeColor = Color.FromArgb(233, 233, 233);
258+
this.buttonOpen.ForeColor = Color.FromArgb(233, 233, 233);
259+
this.buttonClear.ForeColor = Color.FromArgb(233, 233, 233);
260+
this.labelErrormessage.ForeColor = Color.FromArgb(233, 233, 233);
261+
}
262+
263+
public static class WindowHelper
264+
{
265+
//https://stackoverflow.com/questions/2636721/bring-another-processes-window-to-foreground-when-it-has-showintaskbar-false
266+
public static void BringProcessToFront(Process process)
267+
{
268+
IntPtr handle = process.MainWindowHandle;
269+
if (IsIconic(handle))
270+
{
271+
ShowWindow(handle, SW_RESTORE);
272+
}
273+
274+
SetForegroundWindow(handle);
275+
}
276+
277+
const int SW_RESTORE = 9;
276278

279+
[System.Runtime.InteropServices.DllImport("User32.dll")]
280+
private static extern bool SetForegroundWindow(IntPtr handle);
281+
[System.Runtime.InteropServices.DllImport("User32.dll")]
282+
private static extern bool ShowWindow(IntPtr handle, int nCmdShow);
283+
[System.Runtime.InteropServices.DllImport("User32.dll")]
284+
private static extern bool IsIconic(IntPtr handle);
277285
}
278-
286+
/*
287+
private void textBoxInterval_TextChanged(object sender, EventArgs e)
288+
{
289+
if (e.KeyValue == (char)Keys.Enter)
290+
{
291+
ClickEvent();
292+
}
293+
else
294+
{
279295
296+
}
297+
}
298+
*/
280299
}
281300
}
301+

0 commit comments

Comments
 (0)