-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm1.cs
More file actions
101 lines (87 loc) · 3.08 KB
/
Form1.cs
File metadata and controls
101 lines (87 loc) · 3.08 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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace DetectCapsLock
{
public partial class Form1 : Form, IMessageFilter
{
Icon red = Icon.FromHandle(Properties.Resources.red.GetHicon());
Icon green = Icon.FromHandle(Properties.Resources.green.GetHicon());
#region drag without title - varaibles
//source: https://stackoverflow.com/questions/23966253/moving-form-without-title-bar
public const int WM_NCLBUTTONDOWN = 0xA1;
public const int HT_CAPTION = 0x2;
public const int WM_LBUTTONDOWN = 0x0201;
[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();
private HashSet<Control> controlsToMove = new HashSet<Control>();
#endregion
NotifyIcon trayIcon;
public Form1()
{
InitializeComponent();
}
private void TrayIcon_Click(object sender, EventArgs e)
{
this.Visible = !this.Visible;
}
private void Form1_Load(object sender, EventArgs e)
{
trayIcon = new NotifyIcon();
trayIcon.Icon = red;
trayIcon.Visible = true; // Shows the notify icon in the system tray
trayIcon.ContextMenuStrip = contextMenuStrip1;
var exitItem = contextMenuStrip1.Items.Add("Exit");
exitItem.Click += Exit;
trayIcon.Click += TrayIcon_Click;
Application.AddMessageFilter(this);
controlsToMove.Add(this);
#region Set Update
Timer UpdateTimer;
UpdateTimer = new Timer();
UpdateTimer.Tick += new EventHandler(update);
UpdateTimer.Interval = 10; // in miliseconds
UpdateTimer.Start();
#endregion
}
void Exit(object sender, EventArgs e)
{
// Hide tray icon, otherwise it will remain shown until user mouses over it
trayIcon.Visible = false;
Application.Exit();
}
#region drag without title
public bool PreFilterMessage(ref Message m)
{
if (m.Msg == WM_LBUTTONDOWN &&
controlsToMove.Contains(Control.FromHandle(m.HWnd)))
{
ReleaseCapture();
SendMessage(this.Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
return true;
}
return false;
}
#endregion
void update(object sender, EventArgs e)
{
this.TopMost = true;
if (Control.IsKeyLocked(Keys.CapsLock))
{
//caps lock on
this.BackColor = Color.Green;
trayIcon.Icon = green;
}
else
{
//caps lock off
this.BackColor = Color.Red;
trayIcon.Icon = red;
}
}
}
}