-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatForm.cs
More file actions
266 lines (241 loc) · 9.38 KB
/
ChatForm.cs
File metadata and controls
266 lines (241 loc) · 9.38 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
using Discord;
using System;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace FloatChat;
public partial class ChatForm : Form {
delegate void WinEventDelegate(IntPtr hWinEventHook, uint eventType, IntPtr hWnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);
[DllImport("user32.dll")]
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll")]
private static extern IntPtr GetWindowThreadProcessId(IntPtr hWnd, out uint ProcessId);
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
private static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, WinEventDelegate lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags);
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("gdi32.dll")]
private static extern IntPtr AddFontMemResourceEx(IntPtr pbFont, uint cbFont, IntPtr pdv, [In] ref uint pcFonts);
private readonly int oldWindowLong;
private bool clickThrough = false;
private readonly WinEventDelegate winEventDelegate;
private string lastProcess;
private IntPtr lastIntPtr = IntPtr.Zero;
private IntPtr lastIntPtrBuffer = IntPtr.Zero;
private DiscordBot bot;
private Timer hideTimer = new();
private bool newMessage = false;
private double activeOpacity;
private double inactiveOpacity;
// private PrivateFontCollection privateFontCollection;
public ChatForm() {
InitializeComponent();
Process currentProcess = Process.GetCurrentProcess();
Process[] otherProcesses = [.. Process.GetProcessesByName(currentProcess.ProcessName).Where(p => p.Id != currentProcess.Id)];
if (otherProcesses.Length > 0) {
MessageBox.Show($"{Program.Name} is already running", Program.Name);
Environment.Exit(1);
}
notifyIcon.Visible = true;
oldWindowLong = GetWindowLong(Handle, -20);
winEventDelegate = new WinEventDelegate(WinEventProc);
IntPtr eventHook = SetWinEventHook(3, 3, IntPtr.Zero, winEventDelegate, 0, 0, 0);
StartBot();
}
private static string GetActiveWindowProcessName() {
IntPtr hWnd = GetForegroundWindow();
GetWindowThreadProcessId(hWnd, out uint pid);
Process p = Process.GetProcessById((int) pid);
return p.ProcessName;
}
private async void StartBot() {
bot = new DiscordBot(this);
try {
await bot.Run();
} catch (Exception e) {
AddMessage("Error: " + e.Message);
}
}
private void ChatForm_Load(object sender, EventArgs e) {
Opacity = (double) Program.ConfigHandler.Data["activeOpacity"];
/* privateFontCollection = new PrivateFontCollection();
byte[] fontData = Properties.Resources.BurbankBigRegular_Medium;
IntPtr data = Marshal.AllocCoTaskMem(fontData.Length);
Marshal.Copy(fontData, 0, data, fontData.Length);
privateFontCollection.AddMemoryFont(data, fontData.Length);
uint dummy = 0;
AddFontMemResourceEx(data, (uint) Properties.Resources.BurbankBigRegular_Medium.Length, IntPtr.Zero, ref dummy);
Marshal.FreeCoTaskMem(data); */
ApplySettings();
}
private void ApplySettings() {
activeOpacity = (double) Program.ConfigHandler.Data["activeOpacity"];
inactiveOpacity = (double) Program.ConfigHandler.Data["inactiveOpacity"];
Location = new Point((int) Program.ConfigHandler.Data["locationX"], (int) Program.ConfigHandler.Data["locationY"]);
Width = (int) Program.ConfigHandler.Data["sizeX"];
Height = (int) Program.ConfigHandler.Data["sizeY"];
int chatBoxFontSize = (int) Program.ConfigHandler.Data["chatBoxFontSize"];
string chatBoxFont = (string) Program.ConfigHandler.Data["chatBoxFont"];
if (string.IsNullOrEmpty(chatBoxFont)) {
// chatBox.Font = new Font(privateFontCollection.Families[0], chatBoxFontSize);
// chatBox.Font = new Font(privateFontCollection.Families[0], chatBoxFontSize);
} else {
chatBox.Font = new Font(chatBoxFont, chatBoxFontSize);
}
int inputBoxFontSize = (int) Program.ConfigHandler.Data["inputBoxFontSize"];
string inputBoxFont = (string) Program.ConfigHandler.Data["inputBoxFontStr"];
if (string.IsNullOrEmpty(inputBoxFont)) {
// inputBox.Font = new Font(privateFontCollection.Families[0], inputBoxFontSize);
} else {
inputBox.Font = new Font(inputBoxFont, inputBoxFontSize);
}
chatBox.Location = new Point(10, 10);
chatBox.Width = Width - 20;
chatBox.Height = Height - inputBox.Height - 10 - 10 - 10;
inputBox.Location = new Point(10, Height - 10 - inputBox.Height);
inputBox.Width = Width - 20;
chatBox.SelectionStart = chatBox.Text.Length;
chatBox.ScrollToCaret();
}
public void OnBotReady() {
if (InvokeRequired) {
Invoke(new Action(OnBotReady));
} else {
AddMessage("Connected to Discord\n");
inputBox.Enabled = true;
if (!clickThrough) {
inputBox.Focus();
}
}
}
public void AddMessage(string message) {
if (InvokeRequired) {
Invoke(new Action(() => AddMessage(message)));
} else {
string extra = "";
if (!string.IsNullOrEmpty(chatBox.Text)) {
extra = "\n";
}
chatBox.Text += extra + message;
chatBox.SelectionStart = chatBox.Text.Length;
chatBox.ScrollToCaret();
if (clickThrough) {
StartHideTimer((int) Program.ConfigHandler.Data["newMessageHideTimer"]);
Opacity = inactiveOpacity;
newMessage = true;
}
}
}
private void ChatBox_LinkClicked(object sender, LinkClickedEventArgs e) {
inputBox.Focus();
ProcessStartInfo startInfo = new(e.LinkText) { UseShellExecute = true };
Process.Start(startInfo);
}
private void InputBox_KeyDown(object sender, KeyEventArgs e) {
if (e.KeyCode == Keys.Enter) {
string text = inputBox.Text.Trim();
inputBox.Text = "";
if (!string.IsNullOrEmpty(text)) {
e.SuppressKeyPress = true;
string[] textSplit = text.Split(' ');
if (textSplit[0] == "/nick") {
if (textSplit.Length > 1) {
Program.ConfigHandler.Data["nick"] = textSplit[1].Trim();
Program.ConfigHandler.SaveConfig();
AddMessage("Your nick has been changed to \"" + textSplit[1].Trim() + "\"");
} else {
AddMessage("Syntax: /nick <nick>");
}
} else {
ITextChannel channel = (ITextChannel) bot.Client.GetChannel((ulong) Program.ConfigHandler.Data["channelId"]);
channel.SendMessageAsync((string) Program.ConfigHandler.Data["nick"] + ": " + text);
}
}
} else if (e.KeyCode == Keys.Escape) {
e.SuppressKeyPress = true;
SetForegroundWindow(lastIntPtr);
}
}
private void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hWnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime) {
string processName = GetActiveWindowProcessName();
string targetProcessName = (string) Program.ConfigHandler.Data["processName"];
if (processName == targetProcessName || targetProcessName == "*") {
if (lastProcess == Process.GetCurrentProcess().ProcessName || (bool) Program.ConfigHandler.Data["alwaysShowInProcess"]) {
ChatForm_Deactivate(null, null);
}
} else if (processName == Process.GetCurrentProcess().ProcessName) {
ChatForm_Activated(null, null);
} else {
if (!newMessage) {
Opacity = 0D;
}
}
if (processName != "explorer") {
lastProcess = processName;
lastIntPtr = lastIntPtrBuffer;
lastIntPtrBuffer = hWnd;
}
}
private void ChatForm_Activated(object sender, EventArgs e) {
inputBox.SelectionStart = 0;
inputBox.SelectionLength = inputBox.Text.Length;
StopHideTimer();
Opacity = activeOpacity;
_ = SetWindowLong(Handle, -20, oldWindowLong);
clickThrough = false;
}
private void ChatForm_Deactivate(object sender, EventArgs e) {
if (!hideTimer.Enabled) {
StartHideTimer();
}
Opacity = inactiveOpacity;
_ = SetWindowLong(Handle, -20, oldWindowLong | 0x80000 | 0x20);
clickThrough = true;
}
private void StopHideTimer() {
if (hideTimer.Enabled) {
hideTimer.Stop();
}
newMessage = false;
}
private void StartHideTimer(int hideTimerInterval = -2) {
if (hideTimerInterval == -2) {
hideTimerInterval = (int) Program.ConfigHandler.Data["hideTimer"];
}
if (hideTimerInterval >= 0) {
StopHideTimer();
hideTimer = new Timer();
hideTimer.Tick += new EventHandler(OnHideTimer);
hideTimer.Interval = hideTimerInterval * 1000;
hideTimer.Enabled = true;
}
}
private void OnHideTimer(object sender, EventArgs e) {
string processName = GetActiveWindowProcessName();
hideTimer.Enabled = false;
newMessage = false;
if ((processName != (string) Program.ConfigHandler.Data["processName"] && processName != "*") || (bool) Program.ConfigHandler.Data["alwaysShowInProcess"] == false) {
Opacity = 0D;
}
}
private void NotifyIcon_DoubleClick(object sender, EventArgs e) {
Activate();
}
private void ReloadSettingsToolStripMenuItem_Click(object sender, EventArgs e) {
Program.ConfigHandler.LoadConfig();
ApplySettings();
}
private void OpenSettingsToolStripMenuItem_Click(object sender, EventArgs e) {
ProcessStartInfo startInfo = new(Program.ConfigHandler.ConfigFilePath) { UseShellExecute = true };
Process.Start(startInfo);
}
private void ExitToolStripMenuItem_Click(object sender, EventArgs e) {
Close();
}
}