-
Notifications
You must be signed in to change notification settings - Fork 0
/
subMsgs.cs
executable file
·825 lines (731 loc) · 20.9 KB
/
subMsgs.cs
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Security.Cryptography;
namespace subMsg
{
/*
* Create a class which is a Child of Form
*
*/
public partial class Form1 : Form
{
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vk);
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
enum KeyModifier
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
WinKey = 8
}
private Button quitButton;
private Label label2;
private Button loadMessagesButton;
private Button clearWindowButton;
private Button pauseButton;
/*
* Messages and configuration file contents as lists
*
*/
string[] messages;
string[] config;
/*
* Dictionaries for configuration file contents, return values and colors
*
*/
Dictionary<string, string> config_dict = new Dictionary<string, string>();
Dictionary<int, string> rvalues_dict = new Dictionary<int, string>();
Dictionary<string, int> msg_display_types = new Dictionary<string, int>();
/*
* Timer related variables
*
*/
int init_timer_sleep = 2000;
int timer_sleep = 2000;
int timer_change_rate = 100;
int shown = 0;
// int messages_loaded = 0;
// int debug = 0;
/*
* Global variables
*
*/
int config_correct = 0;
int delay_time = 2000;
int blink_time = 1000;
int is_bold = 0;
int msg_display_speed = 0;
float font_size = 30;
string font_type = "Arial";
string font_color = "Red";
Random xpos = new Random();
Random ypos = new Random();
/*
* Screen resolution
*
*/
int screen_width = SystemInformation.VirtualScreen.Width;
int screen_height = SystemInformation.VirtualScreen.Height;
int num_config_items_read = 0;
/*
* The label which will hold the text to be displayed, and flashed
*
*/
Label label1 = new Label();
int rvalue = 0;
/*
* Initialize two timers -
* timer1 - delay between flashes
* timer2 - flashing time
*
*/
System.Windows.Forms.Timer timer1 = new System.Windows.Forms.Timer();
System.Windows.Forms.Timer timer2 = new System.Windows.Forms.Timer();
public int pause_msgs;
public int Pause_Msgs
{
get { return pause_msgs; }
set
{
pause_msgs = value;
}
}
/*
* This function is run whenever a message is sent to a window
*
*/
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == 0x0312)
{
/*
* Note that the three lines below are not needed if you only want to register one hotkey.
* The below lines are useful in case you want to register multiple keys, which you can use a switch with the id as argument, or if you want to know which key/modifier was pressed for some particular reason
*
*/
Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF); // The key of the hotkey that was pressed.
KeyModifier modifier = (KeyModifier)((int)m.LParam & 0xFFFF); // The modifier of the hotkey that was pressed.
int id = m.WParam.ToInt32(); // The id of the hotkey that was pressed.
// MessageBox.Show(key.ToString());
/*
* Below sleep is important for the hotkeys to function
*
*/
if (key.ToString() == "E")
{
if (pause_msgs == 0)
{
pauseButton.Text = "Resume";
Pause_Msgs += 1;
}
else
{
pauseButton.Text = "Pause";
Pause_Msgs -= 1;
}
}
if (key.ToString() == "D")
{
if (this.shown == 0)
{
quitButton.Show();
loadMessagesButton.Show();
clearWindowButton.Show();
pauseButton.Show();
this.shown = 1;
}
else
{
quitButton.Hide();
loadMessagesButton.Hide();
clearWindowButton.Hide();
pauseButton.Hide();
this.shown = 0;
}
}
}
}
public Form1()
{
/*
* Return values mapped to errors/messages
*
*/
rvalues_dict[3] = "Unable to set Label parameters and Global variables";
rvalues_dict[1] = "Unable to set Global variables";
rvalues_dict[2] = "Unable to set Label parameters";
rvalues_dict[20] = "Invalid Configuration File";
rvalues_dict[30] = "Invalid Messages File";
/*
* How messages are displayed?
*
*/
msg_display_types["normal"] = 0;
msg_display_types["slowing"] = 1;
msg_display_types["faster"] = 2;
msg_display_types["sync_with_music"] = 3;
/*
* Read and process configuration file
*
*/
rvalue = readAndProcessConfig();
if (rvalue != 0)
{
MessageBox.Show(rvalues_dict[rvalue]);
this.Close();
Application.Exit();
}
/*
* Initialize component
*
*/
InitializeComponent();
/*
* Form background, border and transparency
*
*/
this.BackColor = Color.White;
this.TransparencyKey = Color.White;
this.FormBorderStyle = FormBorderStyle.None;
this.Bounds = Screen.PrimaryScreen.Bounds;
this.TopMost = true;
this.Name = "subMsgs";
this.Text = "subMsgs";
this.Size = new Size(screen_width, screen_height);
label2.Width = 1000;
label2.Height = 1000;
label1.Width = 1000;
label1.Height = 40;
label1.AutoSize = false;
/*
* button1 - quit
* button2 - Load Messages
*
*/
this.quitButton.Click += new EventHandler(quitButton_Click);
this.loadMessagesButton.Click += new EventHandler(loadMessagesButton_Click);
this.clearWindowButton.Click += new EventHandler(clearWindowButton_Click);
this.pauseButton.Click += new EventHandler(pauseButton_Click);
/*
* Add label1 to form controls
*
*/
this.Controls.Add(label1);
/*
* Show form
*
*/
this.Show();
int id = 0; // The id of the hotkey.
RegisterHotKey(this.Handle, id, (int)KeyModifier.Shift | (int)KeyModifier.Alt, Keys.E.GetHashCode());
RegisterHotKey(this.Handle, id, (int)KeyModifier.Shift | (int)KeyModifier.Alt, Keys.D.GetHashCode());
/*
* Display flashing text
*
*/
blinkLabel();
}
/*
* Some event handlers
*
*/
private void quitButton_Click(object sender, EventArgs e)
{
this.Close();
Application.Exit();
}
public void pauseButton_Click(object sender, EventArgs e)
{
if (pause_msgs == 0)
{
pauseButton.Text = "Resume";
Pause_Msgs += 1;
}
else
{
pauseButton.Text = "Pause";
Pause_Msgs -= 1;
}
}
private void loadMessagesButton_Click(object sender, EventArgs e)
{
string[] temp = readMsgsFile();
if (temp.Length != 0)
{
messages = temp;
}
else
{
MessageBox.Show(rvalues_dict[30]);
}
}
private void clearWindowButton_Click(object sender, EventArgs e)
{
quitButton.Hide();
loadMessagesButton.Hide();
clearWindowButton.Hide();
pauseButton.Hide();
}
/*
* Action: Read configuration file
* Returns: Array of lines in configuration file
*
*/
string[] readConfigFile()
{
Stream myStream = null;
string[] lines = new string[] { };
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "subMsg Configuration Files (.conf)|*.conf|All Files (*.*)|*.*";
openFileDialog1.FilterIndex = 1;
openFileDialog1.Multiselect = false;
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
string strfilename = openFileDialog1.FileName;
lines = File.ReadAllLines(strfilename);
myStream.Close();
}
}
return lines;
}
/*
* Action: Read messages file
* Returns: Array of messages
*
*/
string[] readMsgsFile()
{
Stream myStream = null;
string[] lines = new string[] { };
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Filter = "subMsg Message Files (.smsg)|*.smsg|All Files (*.*)|*.*";
openFileDialog1.FilterIndex = 1;
openFileDialog1.Multiselect = false;
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
string strfilename = openFileDialog1.FileName;
string content = File.ReadAllText(strfilename);
string decrypted = Decrypt(content);
lines = decrypted.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);
myStream.Close();
}
}
return lines;
}
/*
* Encryption
*
*/
private string Encrypt(string clearText)
{
string encryptionKey = config_dict["key"];
byte[] clearBytes = Encoding.Unicode.GetBytes(clearText);
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(encryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(), CryptoStreamMode.Write))
{
cs.Write(clearBytes, 0, clearBytes.Length);
cs.Close();
}
clearText = Convert.ToBase64String(ms.ToArray());
}
}
MessageBox.Show(clearText);
return clearText;
}
/*
* Decryption
*
*/
private string Decrypt(string cipherText)
{
string encryptionKey = config_dict["key"];
byte[] cipherBytes = Convert.FromBase64String(cipherText);
using (Aes encryptor = Aes.Create())
{
Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(encryptionKey, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
encryptor.Key = pdb.GetBytes(32);
encryptor.IV = pdb.GetBytes(16);
using (MemoryStream ms = new MemoryStream())
{
using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(), CryptoStreamMode.Write))
{
cs.Write(cipherBytes, 0, cipherBytes.Length);
cs.Close();
}
cipherText = Encoding.Unicode.GetString(ms.ToArray());
}
}
return cipherText;
}
/*
* Action: Start the timers to display the messages
*
*/
void timerFunc(string type_of_timer)
{
int msg_index = 0;
//
// Below code is only for debug purposes:
//
// int display_times = 0;
//
timer1.Tick += new System.EventHandler((s1, e1) =>
{
try
{
if ((pause_msgs == 0) && (messages.Length > 0) && (config_correct == 0))
{
Point location = new Point(xpos.Next(1, (int) (screen_width * (3.0/4.0))), ypos.Next(1, screen_height));
label1.Location = location;
msg_index = msg_index + 1;
if (msg_index >= messages.Length)
{
msg_index = 0;
}
label1.Text = messages[msg_index];
//
// Below code is just for debug purposes.
// display_times++;
// if (display_times > 10000)
// {
// display_times = 0;
// }
//
// if (debug == 1)
// {
// label2.Text = display_times.ToString() + ", " + location.ToString() + "\n";
// label2.Text += messages[msg_index] + "\n";
// label2.Text += label1.Visible.ToString();
// }
//
label1.Visible = true;
label1.Refresh();
timer2.Tick += new System.EventHandler((s2, e2) =>
{
label1.Visible = false;
label1.Refresh();
timer2.Stop();
});
timer2.Start();
}
else
{
// timer1.Stop();
}
}
catch
{
}
if (type_of_timer == "faster")
{
timer_sleep = timer_sleep - timer_change_rate;
if (timer_sleep <= 0)
{
timer_sleep = init_timer_sleep;
}
timer1.Interval = timer_sleep;
} else if (type_of_timer == "slowing")
{
timer_sleep = timer_sleep + timer_change_rate;
if (timer_sleep >= 10000)
{
timer_sleep = init_timer_sleep;
}
timer1.Interval = timer_sleep;
}
});
timer1.Start();
}
/*
* Action: Flash messages on the screen with two parameters:
* 1. Flashing time (time for which message is displayed on screen during flash)
* 2. Flashing delay (time delay between flashing of messages)
*
*/
void blinkLabel()
{
timer1.Interval = delay_time;
timer2.Interval = blink_time;
// int msg_index = 0;
if (msg_display_speed == msg_display_types["normal"]) {
timerFunc("normal");
}
else if (msg_display_speed == msg_display_types["faster"]) {
timerFunc("faster");
}
else if (msg_display_speed == msg_display_types["slowing"])
{
timerFunc("slowing");
}
}
/*
* Set the properties read from configuration file, to the following:
* 1. Global variables
* 2. Label properties
* Returns: Result of action - 0 for success, non-zero for failure
*
*/
int setProps()
{
int rvalue = 0;
int result = 0;
rvalue = setGlobalVars();
result += rvalue;
rvalue = setLabelProps();
result += rvalue;
return result;
}
/*
* Action: Set global variables
* Returns: Result of action - 0 for success, non-zero for failure
*
*/
int setGlobalVars()
{
try
{
delay_time = Int32.Parse(config_dict["delay_time"]);
blink_time = Int32.Parse(config_dict["blink_time"]);
msg_display_speed = msg_display_types[config_dict["msg_display_speed"]];
timer_change_rate = Int32.Parse(config_dict["timer_change_rate"]);
timer_sleep = Int32.Parse(config_dict["timer_sleep"]);
init_timer_sleep = timer_sleep;
timer1.Interval = delay_time;
timer2.Interval = blink_time;
}
catch
{
return 2;
}
return 0;
}
/*
* Action: Set label properties
* Returns: Result of action - 0 for success, non-zero for failure
*
*/
int setLabelProps()
{
try
{
// printConfig();
font_type = config_dict["font_type"];
font_size = (float)Int32.Parse(config_dict["font_size"]);
is_bold = Int32.Parse(config_dict["is_bold"]);
FontFamily family = new FontFamily(font_type);
if (is_bold == 1)
{
label1.Font = new Font(family, font_size, FontStyle.Bold);
} else
{
label1.Font = new Font(family, font_size, FontStyle.Regular);
}
font_color = config_dict["font_color"];
label1.ForeColor = Color.FromName(font_color);
label1.TextAlign = ContentAlignment.TopLeft;
label1.Location = new Point(100, 100);
label1.Visible = false;
label1.Refresh();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return 1;
}
return 0;
}
/*
* Action: Fill the config_hash with contents received from configuration file, into config array
*
*/
private void processConfig()
{
foreach (string line in config)
{
string[] tokens = line.Split('=');
string left = "NO_LINE";
string right = "NO_VALUE";
try
{
left = tokens[0].Trim();
right = tokens[1].Trim();
}
catch
{
config_correct = 1;
}
if (!config_dict.ContainsKey(left))
{
config_dict.Add(left, right);
}
else
{
config_dict[left] = right;
}
if (num_config_items_read == 4)
{
config_correct = 1;
num_config_items_read = 0;
}
}
}
/*
* Action: Display configuration
*
*/
private void printConfig()
{
string content = "";
foreach (KeyValuePair<string, string> entry in config_dict)
{
content += entry.Key;
content += "->";
content += entry.Value;
content += ".\n";
}
MessageBox.Show(content);
}
/*
* Action: Read configuration file, and update the values read from configuration file, onto the configuration hash
* Returns: Result of action - 0 for success, non-zero for failure
*
*/
private int readAndProcessConfig()
{
string[] temp = readConfigFile();
int rvalue = 0;
if (temp.Length != 0)
{
config = temp;
}
else
{
rvalue = 20;
return rvalue;
}
processConfig();
rvalue = setProps();
return rvalue;
}
protected override void OnKeyPress(KeyPressEventArgs ex)
{
string xo = ex.KeyChar.ToString();
if (xo == "l")
{
quitButton.Show();
loadMessagesButton.Show();
clearWindowButton.Show();
pauseButton.Show();
}
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.quitButton = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.loadMessagesButton = new System.Windows.Forms.Button();
this.clearWindowButton = new System.Windows.Forms.Button();
this.pauseButton = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// quitButton
//
this.quitButton.BackColor = System.Drawing.SystemColors.ActiveCaption;
this.quitButton.Location = new System.Drawing.Point(276, 309);
this.quitButton.Name = "quitButton";
this.quitButton.Size = new System.Drawing.Size(400, 100);
this.quitButton.TabIndex = 0;
this.quitButton.Text = "Quit";
this.quitButton.UseVisualStyleBackColor = false;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(1, 247);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(0, 13);
this.label2.TabIndex = 3;
//
// loadMessagesButton
//
this.loadMessagesButton.Location = new System.Drawing.Point(12, 12);
this.loadMessagesButton.Name = "loadMessagesButton";
this.loadMessagesButton.Size = new System.Drawing.Size(400, 100);
this.loadMessagesButton.TabIndex = 4;
this.loadMessagesButton.Text = "Load Messages";
this.loadMessagesButton.UseVisualStyleBackColor = true;
//
// clearWindowButton
//
this.clearWindowButton.Location = new System.Drawing.Point(12, 153);
this.clearWindowButton.Name = "clearWindowButton";
this.clearWindowButton.Size = new System.Drawing.Size(400, 100);
this.clearWindowButton.TabIndex = 5;
this.clearWindowButton.Text = "Clear Window";
this.clearWindowButton.UseVisualStyleBackColor = true;
//
// pauseButton
//
this.pauseButton.Location = new System.Drawing.Point(537, 12);
this.pauseButton.Name = "pauseButton";
this.pauseButton.Size = new System.Drawing.Size(397, 100);
this.pauseButton.TabIndex = 6;
this.pauseButton.Text = "Pause";
this.pauseButton.UseVisualStyleBackColor = true;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(987, 421);
this.Controls.Add(this.pauseButton);
this.Controls.Add(this.clearWindowButton);
this.Controls.Add(this.loadMessagesButton);
this.Controls.Add(this.label2);
this.Controls.Add(this.quitButton);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
/*
* Unregister HotKey when form closes
*
*/
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
UnregisterHotKey(this.Handle, 0); // Unregister hotkey with id 0 before closing the form. You might want to call this more than once with different id values if you are planning to register more than one hotkey.
}
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}