Skip to content

Commit 312b5bb

Browse files
committed
v1.1.0 - 2020-01-06
1 parent e28311c commit 312b5bb

11 files changed

+626
-118
lines changed

TopMostFriend/AboutWindow.cs

+4-7
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,13 @@ public sealed class AboutWindow : Form {
99
private const int BUTTON_HEIGHT = 23;
1010
private const int BUTTON_WIDTH = 70;
1111

12-
public const int WM_NCLBUTTONDOWN = 0xA1;
13-
public const int HT_CAPTION = 0x02;
14-
1512
public static void Display() {
1613
using (AboutWindow about = new AboutWindow())
1714
about.ShowDialog();
1815
}
1916

2017
public AboutWindow() {
21-
Text = $@"About Top Most Friend";
18+
Text = @"About Top Most Friend";
2219
Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath);
2320
BackgroundImage = Properties.Resources.about;
2421
StartPosition = FormStartPosition.CenterScreen;
@@ -81,7 +78,7 @@ public AboutWindow() {
8178
Controls.Add(creditButtonfff);
8279

8380
Controls.Add(new Label {
84-
Text = Application.ProductVersion,
81+
Text = @"v" + Application.ProductVersion.Substring(0, Application.ProductVersion.Length - 2), // cut off the last dingus
8582
TextAlign = ContentAlignment.MiddleLeft,
8683
AutoSize = true,
8784
Location = new Point(127, 97),
@@ -93,8 +90,8 @@ public AboutWindow() {
9390
protected override void OnMouseDown(MouseEventArgs e) {
9491
base.OnMouseDown(e);
9592

96-
Program.ReleaseCapture();
97-
Program.SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
93+
Win32.ReleaseCapture();
94+
Win32.SendMessage(Handle, Win32.WM_NCLBUTTONDOWN, Win32.HT_CAPTION, 0);
9895
}
9996
}
10097
}

TopMostFriend/HotKeyWindow.cs

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel;
4+
using System.Drawing;
5+
using System.Linq;
6+
using System.Runtime.InteropServices;
7+
using System.Windows.Forms;
8+
9+
namespace TopMostFriend {
10+
public sealed class HotKeyWindow : Form {
11+
public class HotKeyInfo {
12+
public string Name { get; }
13+
public int Atom { get; }
14+
public int Key { get; }
15+
public Action Action { get; }
16+
17+
public HotKeyInfo(string name, int atom, int key, Action action) {
18+
Name = name ?? throw new ArgumentNullException(nameof(name));
19+
Atom = atom;
20+
Key = key;
21+
Action = action ?? throw new ArgumentNullException(nameof(action));
22+
}
23+
}
24+
25+
private readonly List<HotKeyInfo> RegisteredHotKeys = new List<HotKeyInfo>();
26+
27+
public HotKeyWindow() {
28+
ShowInTaskbar = false;
29+
Text = string.Empty;
30+
FormBorderStyle = FormBorderStyle.None;
31+
StartPosition = FormStartPosition.Manual;
32+
Size = new Size(1, 1);
33+
Location = new Point(-9999, -9999);
34+
CreateHandle();
35+
Hide();
36+
}
37+
38+
protected override void OnFormClosing(FormClosingEventArgs e) {
39+
e.Cancel = e.CloseReason == CloseReason.UserClosing;
40+
}
41+
42+
protected override void WndProc(ref Message m) {
43+
base.WndProc(ref m);
44+
45+
if(m.Msg == Win32.WM_HOTKEY) {
46+
int keyCode = m.LParam.ToInt32();
47+
48+
lock (RegisteredHotKeys)
49+
RegisteredHotKeys.FirstOrDefault(x => x.Key == keyCode)?.Action.Invoke();
50+
}
51+
}
52+
53+
protected override void Dispose(bool disposing) {
54+
lock (RegisteredHotKeys) {
55+
HotKeyInfo[] hotKeys = RegisteredHotKeys.ToArray();
56+
57+
foreach(HotKeyInfo hotKey in hotKeys)
58+
Unregister(hotKey.Atom);
59+
}
60+
61+
base.Dispose(disposing);
62+
}
63+
64+
public int Register(string name, Win32ModKeys modifiers, Keys key, Action action) {
65+
if (action == null)
66+
throw new ArgumentNullException(nameof(action));
67+
if(string.IsNullOrEmpty(name))
68+
name = Guid.NewGuid().ToString();
69+
70+
int atom = Win32.GlobalAddAtom(name);
71+
int keyCode = ((ushort)key << 16) | (ushort)modifiers;
72+
73+
if (atom == 0)
74+
throw new Win32Exception(Marshal.GetLastWin32Error(), @"Atom creation failed.");
75+
76+
if (!Win32.RegisterHotKey(Handle, atom, modifiers, key)) {
77+
Win32.GlobalDeleteAtom((ushort)atom);
78+
throw new Win32Exception(Marshal.GetLastWin32Error(), @"Hotkey registration failed.");
79+
}
80+
81+
lock(RegisteredHotKeys)
82+
RegisteredHotKeys.Add(new HotKeyInfo(name, atom, keyCode, action));
83+
84+
return atom;
85+
}
86+
87+
public void Unregister(int id) {
88+
if (id < 1)
89+
return;
90+
91+
lock (RegisteredHotKeys) {
92+
if (!RegisteredHotKeys.Any(x => x.Atom == id))
93+
return;
94+
RegisteredHotKeys.RemoveAll(x => x.Atom == id);
95+
}
96+
97+
Win32.UnregisterHotKey(Handle, id);
98+
Win32.GlobalDeleteAtom((ushort)id);
99+
}
100+
101+
public void Unregister(string name) {
102+
int atom = 0;
103+
104+
lock (RegisteredHotKeys)
105+
atom = RegisteredHotKeys.FirstOrDefault(x => x.Name == name)?.Atom ?? 0;
106+
107+
Unregister(atom);
108+
}
109+
}
110+
}

0 commit comments

Comments
 (0)