-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
94 lines (79 loc) · 2.98 KB
/
Program.cs
File metadata and controls
94 lines (79 loc) · 2.98 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
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Reflection;
using System.IO;
namespace WideAwake
{
public class Program : Form
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern uint SetThreadExecutionState(uint esFlags);
[DllImport("user32.dll")]
static extern void mouse_event(uint dwFlags, int dx, int dy, uint dwData, uint dwExtraInfo);
const uint ES_CONTINUOUS = 0x80000000;
const uint ES_SYSTEM_REQUIRED = 0x00000001;
const uint ES_DISPLAY_REQUIRED = 0x00000002;
const uint MOUSEEVENTF_MOVE = 0x0001;
private NotifyIcon trayIcon = null!;
private System.Windows.Forms.Timer activityTimer = null!;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Program());
}
public Program()
{
SetThreadExecutionState(ES_CONTINUOUS | ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED);
activityTimer = new System.Windows.Forms.Timer();
activityTimer.Interval = 60000;
activityTimer.Tick += (s, e) => {
mouse_event(MOUSEEVENTF_MOVE, 1, 0, 0, 0);
mouse_event(MOUSEEVENTF_MOVE, -1, 0, 0, 0);
};
activityTimer.Start();
InitializeTray();
this.WindowState = FormWindowState.Minimized;
this.ShowInTaskbar = false;
}
private void InitializeTray()
{
trayIcon = new NotifyIcon();
trayIcon.Text = "WideAwake (Active)";
trayIcon.Visible = true;
var assembly = Assembly.GetExecutingAssembly();
string resourceName = "WideAwake.app.ico";
try
{
using (Stream? stream = assembly.GetManifestResourceStream(resourceName))
{
if (stream != null)
trayIcon.Icon = new Icon(stream);
else
trayIcon.Icon = SystemIcons.Application;
}
}
catch
{
trayIcon.Icon = SystemIcons.Application;
}
var contextMenu = new ContextMenuStrip();
contextMenu.Items.Add("Exit", null, (s, e) => Application.Exit());
trayIcon.ContextMenuStrip = contextMenu;
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
activityTimer.Stop();
SetThreadExecutionState(ES_CONTINUOUS);
trayIcon.Dispose();
base.OnFormClosing(e);
}
protected override void SetVisibleCore(bool value)
{
base.SetVisibleCore(false);
}
}
}