forked from xolarity/WinGrooves
-
Notifications
You must be signed in to change notification settings - Fork 2
/
FrmMain.cs
834 lines (760 loc) · 36.1 KB
/
FrmMain.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
826
827
828
829
830
831
832
833
834
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using mshtml;
using Microsoft.WindowsAPICodePack;
using Microsoft.WindowsAPICodePack.Controls;
using Microsoft.WindowsAPICodePack.Taskbar;
namespace WinGrooves
{
/// <summary>
/// Summary description for FrmMain.
/// </summary>
public class FrmMain : System.Windows.Forms.Form
{
private WebBrowser webBrowser1;
private System.ComponentModel.IContainer components;
private bool injectedSongInfoFunctions = false;
private bool closingFromTray = false;
const int WM_HOTKEY = 0x0312;
const int VK_MEDIA_NEXT_TRACK = 0xB0;
const int VK_MEDIA_PREV_TRACK = 0xB1;
const int VK_MEDIA_STOP = 0xB2;
const int VK_MEDIA_PLAY_PAUSE = 0xB3;
public static KeyboardHook hook = new KeyboardHook();
bool windowInitialized;
private NotifyIcon notifyIcon1;
private ContextMenuStrip contextMenuStrip1;
private ToolStripMenuItem HideShow;
private ToolStripMenuItem About;
private ToolStripSeparator toolStripSeparator1;
private ToolStripMenuItem Exit;
private ToolStripSeparator toolStripSeparator2;
private ToolStripMenuItem Previous;
private ToolStripMenuItem Next;
private ToolStripMenuItem toolStripMenuItem1;
private ToolStrip toolStrip1;
private ToolStripButton toolStripButton1;
private ToolStripButton toolStripButton2;
private ToolStripButton toolStripButton3;
private Timer currentSongTimer;
private ToolStripButton toolStripButton4;
private Timer alwaysListeningTimer;
private ToolStripMenuItem Play;
//These constants are needed to disable the IE click sound
private const int FEATURE_DISABLE_NAVIGATION_SOUNDS = 21;
private const int SET_FEATURE_ON_THREAD = 0x00000001;
private const int SET_FEATURE_ON_PROCESS = 0x00000002;
private const int SET_FEATURE_IN_REGISTRY = 0x00000004;
private const int SET_FEATURE_ON_THREAD_LOCALMACHINE = 0x00000008;
private const int SET_FEATURE_ON_THREAD_INTRANET = 0x00000010;
private const int SET_FEATURE_ON_THREAD_TRUSTED = 0x00000020;
private const int SET_FEATURE_ON_THREAD_INTERNET = 0x00000040;
private ToolStripMenuItem Like;
private ToolStripMenuItem Dislike;
private const int SET_FEATURE_ON_THREAD_RESTRICTED = 0x00000080;
[DllImport("urlmon.dll")]
[PreserveSig]
[return: MarshalAs(UnmanagedType.Error)]
static extern int CoInternetSetFeatureEnabled(
int FeatureEntry,
[MarshalAs(UnmanagedType.U4)] int dwFlags,
bool fEnable);
//Windows 7 features
private ThumbnailToolbarButton buttonPrev;
private bool isbuttonPaused, isMusicPlaying;
private ThumbnailToolbarButton buttonPause;
private ThumbnailToolbarButton buttonNext;
public FrmMain()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
this.Resize += new EventHandler(FrmMain_Resize);
//code to remember window size and position
// this is the default
this.WindowState = FormWindowState.Normal;
this.StartPosition = FormStartPosition.WindowsDefaultBounds;
// check if the saved bounds are nonzero and visible on any screen
if (Properties.Settings.Default.WindowPosition != Rectangle.Empty &&
IsVisibleOnAnyScreen(Properties.Settings.Default.WindowPosition))
{
// first set the bounds
this.StartPosition = FormStartPosition.Manual;
this.DesktopBounds = Properties.Settings.Default.WindowPosition;
// afterwards set the window state to the saved value (which could be Maximized)
this.WindowState = Properties.Settings.Default.WindowState;
}
else
{
// this resets the upper left corner of the window to windows standards
this.StartPosition = FormStartPosition.WindowsDefaultLocation;
// we can still apply the saved size
// msorens: added gatekeeper, otherwise first time appears as just a title bar!
if (Properties.Settings.Default.WindowPosition != Rectangle.Empty)
{
this.Size = Properties.Settings.Default.WindowPosition.Size;
}
}
windowInitialized = true;
}
private bool IsVisibleOnAnyScreen(Rectangle rect)
{
foreach (Screen screen in Screen.AllScreens)
{
if (screen.WorkingArea.IntersectsWith(rect))
{
return true;
}
}
return false;
}
protected override void OnClosed(EventArgs e)
{
base.OnClosed(e);
// only save the WindowState if Normal or Maximized
switch (this.WindowState)
{
case FormWindowState.Normal:
case FormWindowState.Maximized:
Properties.Settings.Default.WindowState = this.WindowState;
break;
default:
Properties.Settings.Default.WindowState = FormWindowState.Normal;
break;
}
# region msorens: this code does *not* handle minimized/maximized window.
// reset window state to normal to get the correct bounds
// also make the form invisible to prevent distracting the user
//this.Visible = false;
//this.WindowState = FormWindowState.Normal;
//Settings.Default.WindowPosition = this.DesktopBounds;
# endregion
Properties.Settings.Default.Save();
}
protected override void OnResize(EventArgs e)
{
base.OnResize(e);
TrackWindowState();
}
protected override void OnMove(EventArgs e)
{
base.OnMove(e);
TrackWindowState();
}
// On a move or resize in Normal state, record the new values as they occur.
// This solves the problem of closing the app when minimized or maximized.
private void TrackWindowState()
{
// Don't record the window setup, otherwise we lose the persistent values!
if (!windowInitialized) { return; }
if (WindowState == FormWindowState.Normal)
{
Properties.Settings.Default.WindowPosition = this.DesktopBounds;
}
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmMain));
this.webBrowser1 = new System.Windows.Forms.WebBrowser();
this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components);
this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components);
this.HideShow = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
this.About = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator();
this.Previous = new System.Windows.Forms.ToolStripMenuItem();
this.Next = new System.Windows.Forms.ToolStripMenuItem();
this.Play = new System.Windows.Forms.ToolStripMenuItem();
this.Like = new System.Windows.Forms.ToolStripMenuItem();
this.Dislike = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
this.Exit = new System.Windows.Forms.ToolStripMenuItem();
this.toolStrip1 = new System.Windows.Forms.ToolStrip();
this.toolStripButton1 = new System.Windows.Forms.ToolStripButton();
this.toolStripButton2 = new System.Windows.Forms.ToolStripButton();
this.toolStripButton3 = new System.Windows.Forms.ToolStripButton();
this.toolStripButton4 = new System.Windows.Forms.ToolStripButton();
this.currentSongTimer = new System.Windows.Forms.Timer(this.components);
this.alwaysListeningTimer = new System.Windows.Forms.Timer(this.components);
this.contextMenuStrip1.SuspendLayout();
this.toolStrip1.SuspendLayout();
this.SuspendLayout();
//
// webBrowser1
//
this.webBrowser1.Dock = System.Windows.Forms.DockStyle.Fill;
this.webBrowser1.Location = new System.Drawing.Point(0, 25);
this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
this.webBrowser1.Name = "webBrowser1";
this.webBrowser1.ScriptErrorsSuppressed = true;
this.webBrowser1.Size = new System.Drawing.Size(1517, 690);
this.webBrowser1.TabIndex = 9;
this.webBrowser1.Url = new System.Uri("", System.UriKind.Relative);
this.webBrowser1.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.webBrowser1_DocumentCompleted);
//
// notifyIcon1
//
this.notifyIcon1.ContextMenuStrip = this.contextMenuStrip1;
this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon")));
this.notifyIcon1.Text = "WinGrooves";
this.notifyIcon1.Visible = true;
this.notifyIcon1.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.notifyIcon1_MouseDoubleClick);
//
// contextMenuStrip1
//
this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.HideShow,
this.toolStripMenuItem1,
this.About,
this.toolStripSeparator2,
this.Previous,
this.Next,
this.Play,
this.Like,
this.Dislike,
this.toolStripSeparator1,
this.Exit});
this.contextMenuStrip1.Name = "contextMenuStrip1";
this.contextMenuStrip1.Size = new System.Drawing.Size(182, 236);
//
// HideShow
//
this.HideShow.Name = "HideShow";
this.HideShow.Size = new System.Drawing.Size(181, 22);
this.HideShow.Text = "Show/Hide";
this.HideShow.Click += new System.EventHandler(this.HideShow_Click);
//
// toolStripMenuItem1
//
this.toolStripMenuItem1.Name = "toolStripMenuItem1";
this.toolStripMenuItem1.Size = new System.Drawing.Size(181, 22);
this.toolStripMenuItem1.Text = "Options";
this.toolStripMenuItem1.Click += new System.EventHandler(this.toolStripMenuItem1_Click_1);
//
// About
//
this.About.Name = "About";
this.About.Size = new System.Drawing.Size(181, 22);
this.About.Text = "About";
this.About.Click += new System.EventHandler(this.About_Click);
//
// toolStripSeparator2
//
this.toolStripSeparator2.Name = "toolStripSeparator2";
this.toolStripSeparator2.Size = new System.Drawing.Size(178, 6);
//
// Previous
//
this.Previous.Name = "Previous";
this.Previous.Size = new System.Drawing.Size(181, 22);
this.Previous.Text = "Previous Song";
this.Previous.Click += new System.EventHandler(this.Previous_Click);
//
// Next
//
this.Next.Name = "Next";
this.Next.Size = new System.Drawing.Size(181, 22);
this.Next.Text = "Next Song";
this.Next.Click += new System.EventHandler(this.Next_Click);
//
// Play
//
this.Play.Name = "Play";
this.Play.Size = new System.Drawing.Size(181, 22);
this.Play.Text = "Play/Pause";
this.Play.Click += new System.EventHandler(this.Play_Click);
//
// Like
//
this.Like.Name = "Like";
this.Like.Size = new System.Drawing.Size(181, 22);
this.Like.Text = "Like Current Song";
this.Like.Click += new System.EventHandler(this.Like_Click);
//
// Dislike
//
this.Dislike.Name = "Dislike";
this.Dislike.Size = new System.Drawing.Size(181, 22);
this.Dislike.Text = "Dislike Current Song";
this.Dislike.Click += new System.EventHandler(this.Dislike_Click);
//
// toolStripSeparator1
//
this.toolStripSeparator1.Name = "toolStripSeparator1";
this.toolStripSeparator1.Size = new System.Drawing.Size(178, 6);
//
// Exit
//
this.Exit.Name = "Exit";
this.Exit.Size = new System.Drawing.Size(181, 22);
this.Exit.Text = "Exit";
this.Exit.Click += new System.EventHandler(this.Exit_Click);
//
// toolStrip1
//
this.toolStrip1.GripStyle = System.Windows.Forms.ToolStripGripStyle.Hidden;
this.toolStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripButton1,
this.toolStripButton2,
this.toolStripButton3,
this.toolStripButton4});
this.toolStrip1.Location = new System.Drawing.Point(0, 0);
this.toolStrip1.Name = "toolStrip1";
this.toolStrip1.Size = new System.Drawing.Size(1517, 25);
this.toolStrip1.TabIndex = 10;
this.toolStrip1.Text = "toolStrip1";
//
// toolStripButton1
//
this.toolStripButton1.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.toolStripButton1.Image = ((System.Drawing.Image)(resources.GetObject("toolStripButton1.Image")));
this.toolStripButton1.ImageTransparentColor = System.Drawing.Color.Magenta;
this.toolStripButton1.Name = "toolStripButton1";
this.toolStripButton1.Size = new System.Drawing.Size(23, 22);
this.toolStripButton1.Text = "Go back one page";
this.toolStripButton1.Click += new System.EventHandler(this.toolStripButton1_Click);
//
// toolStripButton2
//
this.toolStripButton2.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.toolStripButton2.Image = ((System.Drawing.Image)(resources.GetObject("toolStripButton2.Image")));
this.toolStripButton2.ImageTransparentColor = System.Drawing.Color.Magenta;
this.toolStripButton2.Name = "toolStripButton2";
this.toolStripButton2.Size = new System.Drawing.Size(23, 22);
this.toolStripButton2.Text = "Go forward one page";
//
// toolStripButton3
//
this.toolStripButton3.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.toolStripButton3.Image = ((System.Drawing.Image)(resources.GetObject("toolStripButton3.Image")));
this.toolStripButton3.ImageTransparentColor = System.Drawing.Color.Magenta;
this.toolStripButton3.Name = "toolStripButton3";
this.toolStripButton3.Size = new System.Drawing.Size(23, 22);
this.toolStripButton3.Text = "Options";
this.toolStripButton3.Click += new System.EventHandler(this.toolStripButton3_Click);
//
// toolStripButton4
//
this.toolStripButton4.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image;
this.toolStripButton4.Image = ((System.Drawing.Image)(resources.GetObject("toolStripButton4.Image")));
this.toolStripButton4.ImageTransparentColor = System.Drawing.Color.Magenta;
this.toolStripButton4.Name = "toolStripButton4";
this.toolStripButton4.Size = new System.Drawing.Size(23, 22);
this.toolStripButton4.Text = "About";
this.toolStripButton4.Click += new System.EventHandler(this.toolStripButton4_Click);
//
// currentSongTimer
//
this.currentSongTimer.Interval = 3000;
this.currentSongTimer.Tick += new System.EventHandler(this.currentSongTimer_Tick);
//
// alwaysListeningTimer
//
this.alwaysListeningTimer.Enabled = true;
this.alwaysListeningTimer.Interval = 600000;
this.alwaysListeningTimer.Tick += new System.EventHandler(this.alwaysListeningTimer_Tick);
//
// Win 7 toolbar buttons
//
this.buttonPrev = new ThumbnailToolbarButton(Properties.Resources.PlayerPrev, "Previous Music");
this.buttonPause = new ThumbnailToolbarButton(Properties.Resources.PlayerPlay, "Pause/Play Music");
this.isbuttonPaused = true;
this.isMusicPlaying = false;
this.buttonNext = new ThumbnailToolbarButton(Properties.Resources.PlayerNext, "Next Music");
//
// FrmMain
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.CausesValidation = false;
this.ClientSize = new System.Drawing.Size(1517, 715);
this.Controls.Add(this.webBrowser1);
this.Controls.Add(this.toolStrip1);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "FrmMain";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "WinGrooves";
this.Activated += new System.EventHandler(this.FrmMain_Activated);
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.FrmMain_FormClosing);
this.Load += new System.EventHandler(this.FrmMain_Load);
this.contextMenuStrip1.ResumeLayout(false);
this.toolStrip1.ResumeLayout(false);
this.toolStrip1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
//Check if application is aready running before running it again
bool ok;
object m = new System.Threading.Mutex(true, "WinGrooves", out ok);
if (!ok)
{
MessageBox.Show("WinGrooves is already running.");
return;
}
Application.Run(new FrmMain());
GC.KeepAlive(m);
}
private void FrmMain_Load(object sender, System.EventArgs e)
{
webBrowser1.Navigate("http://listen.grooveshark.com");
// register the event that is fired after a key press.
hook.KeyPressed += new EventHandler<KeyPressedEventArgs>(hook_KeyPressed);
webBrowser1.ObjectForScripting = this; //needed to capture JavaScript events
//disable the IE "click sound"
int feature = FEATURE_DISABLE_NAVIGATION_SOUNDS;
CoInternetSetFeatureEnabled(feature, SET_FEATURE_ON_PROCESS, true);
if (Properties.Settings.Default.startMinimized)
{
showHideWindow();
}
//Thumbnail buttons for win7 users
if (TaskbarManager.IsPlatformSupported)
{
buttonPrev.Click += new EventHandler<ThumbnailButtonClickedEventArgs>(Previous_Click);
buttonPause.Click += new EventHandler<ThumbnailButtonClickedEventArgs>(Play_Click);
buttonNext.Click += new EventHandler<ThumbnailButtonClickedEventArgs>(Next_Click);
//Add the buttons (kinda of ugly tough)
ThumbnailToolbarButton[] buttonList = new ThumbnailToolbarButton[3];
buttonList[0] = buttonPrev; buttonList[1] = buttonPause; buttonList[2] = buttonNext;
TaskbarManager.Instance.ThumbnailToolbars.AddButtons(this.Handle, buttonList);
}
}
/*
* Simulates a browser click on an html element
/// <param name="action">the HTML id of the element to click on/param>
* */
private void playerExecute(string action)
{
webBrowser1.Document.GetElementById(action).InvokeMember("click");
}
/*
* Injects some jQuery to select and click on specific elements
/// <param name="action">the jquery selector for the element to click on ("#elementid .childclass")</param>
* */
private void htmlClickOn(string selector)
{
if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
{
Object[] objArray = new Object[1];
objArray[0] = (Object)selector;
webBrowser1.Document.InvokeScript("clickElement", objArray);
}
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
Show();
WindowState = FormWindowState.Normal;
}
private void FrmMain_Resize(object sender, EventArgs e)
{
if (Properties.Settings.Default.trayMinimize)
{
if (this.WindowState == FormWindowState.Minimized)
{
Hide();
}
}
}
private void FrmMain_Activated(object sender, EventArgs e)
{
SetupGlobalHotkeys();
}
private void Exit_Click(object sender, EventArgs e)
{
Properties.Settings.Default.Save();
closingFromTray = true;
Close();
}
private void HideShow_Click(object sender, EventArgs e)
{
showHideWindow();
}
private void showHideWindow()
{
if (this.WindowState == FormWindowState.Minimized || this.Visible == false)
{
Show();
WindowState = FormWindowState.Normal;
TopMost = true;
}
else
{
this.WindowState = FormWindowState.Minimized;
if (Properties.Settings.Default.trayMinimize)
{
Hide();
}
}
}
private void Play_Click(object sender, EventArgs e)
{
if (TaskbarManager.IsPlatformSupported)
{
//this only shortens the delay of the button change. Need to have a disabled state!
if (isbuttonPaused) { buttonPause.Icon = Properties.Resources.PlayerPause; isbuttonPaused = false;}
if (!isbuttonPaused) { buttonPause.Icon = Properties.Resources.PlayerPlay; isbuttonPaused = true;}
}
playerExecute("player_play_pause");
}
private void Next_Click(object sender, EventArgs e)
{
playerExecute("player_next");
}
private void Previous_Click(object sender, EventArgs e)
{
playerExecute("player_previous");
}
private void SetupGlobalHotkeys()
{
hook.unregisterAllHotkeys(); //first unregister everything
// register the media keys
try { hook.RegisterHotKey(global::ModifierKeys.None, (Keys)VK_MEDIA_PLAY_PAUSE); }
catch (InvalidOperationException exception) { } //MessageBox.Show(exception.Message);
try { hook.RegisterHotKey(global::ModifierKeys.None, (Keys)VK_MEDIA_NEXT_TRACK); }
catch (InvalidOperationException exception) { }
try { hook.RegisterHotKey(global::ModifierKeys.None, (Keys)VK_MEDIA_PREV_TRACK); }
catch (InvalidOperationException exception) { }
//register other customizable hot keys
try { hook.RegisterHotKey(hook.Win32ModifiersFromKeys((Keys)Properties.Settings.Default.hotkeyPlay), hook.getKeyWithoutModifier((Keys)Properties.Settings.Default.hotkeyPlay)); }
catch (InvalidOperationException exception) { }
try { hook.RegisterHotKey(hook.Win32ModifiersFromKeys((Keys)Properties.Settings.Default.hotkeyNext), hook.getKeyWithoutModifier((Keys)Properties.Settings.Default.hotkeyNext)); }
catch (InvalidOperationException exception) { }
try { hook.RegisterHotKey(hook.Win32ModifiersFromKeys((Keys)Properties.Settings.Default.hotkeyPrevious), hook.getKeyWithoutModifier((Keys)Properties.Settings.Default.hotkeyPrevious)); }
catch (InvalidOperationException exception) { }
try { hook.RegisterHotKey(hook.Win32ModifiersFromKeys((Keys)Properties.Settings.Default.hotkeyLike), hook.getKeyWithoutModifier((Keys)Properties.Settings.Default.hotkeyLike)); }
catch (InvalidOperationException exception) { }
try { hook.RegisterHotKey(hook.Win32ModifiersFromKeys((Keys)Properties.Settings.Default.hotkeyDislike), hook.getKeyWithoutModifier((Keys)Properties.Settings.Default.hotkeyDislike)); }
catch (InvalidOperationException exception) { }
try { hook.RegisterHotKey(hook.Win32ModifiersFromKeys((Keys)Properties.Settings.Default.hotkeyFavorite), hook.getKeyWithoutModifier((Keys)Properties.Settings.Default.hotkeyFavorite)); }
catch (InvalidOperationException exception) { }
try { hook.RegisterHotKey(hook.Win32ModifiersFromKeys((Keys)Properties.Settings.Default.hotkeyShowHide), hook.getKeyWithoutModifier((Keys)Properties.Settings.Default.hotkeyShowHide)); }
catch (InvalidOperationException exception) { }
try { hook.RegisterHotKey(hook.Win32ModifiersFromKeys((Keys)Properties.Settings.Default.hotkeyMute), hook.getKeyWithoutModifier((Keys)Properties.Settings.Default.hotkeyMute)); }
catch (InvalidOperationException exception) { }
try { hook.RegisterHotKey(hook.Win32ModifiersFromKeys((Keys)Properties.Settings.Default.hotkeyShuffle), hook.getKeyWithoutModifier((Keys)Properties.Settings.Default.hotkeyShuffle)); }
catch (InvalidOperationException exception) { }
}
void hook_KeyPressed(object sender, KeyPressedEventArgs e)
{
switch (e.Key.ToString())
{
case "MediaPlayPause":
playerExecute("player_play_pause");
break;
case "MediaNextTrack":
playerExecute("player_next");
break;
case "MediaPreviousTrack":
playerExecute("player_previous");
break;
}
uint KeyAsInt = (uint)(e.Key | hook.keyToModifierKey(e.Modifier));
if (KeyAsInt == Properties.Settings.Default.hotkeyPlay)
{
htmlClickOn("#player_play_pause");
}
else if (KeyAsInt == Properties.Settings.Default.hotkeyNext)
{
htmlClickOn("#player_next");
}
else if (KeyAsInt == Properties.Settings.Default.hotkeyPrevious)
{
htmlClickOn("#player_previous");
}
else if (KeyAsInt == Properties.Settings.Default.hotkeyLike)
{
LikeCurrentSong();
}
else if (KeyAsInt == Properties.Settings.Default.hotkeyDislike)
{
DislikeCurrentSong();
}
else if (KeyAsInt == Properties.Settings.Default.hotkeyFavorite)
{
htmlClickOn("#playerDetails_nowPlaying_options");
htmlClickOn("#jjmenu_main .jj_menu_item_favorites");
}
else if (KeyAsInt == Properties.Settings.Default.hotkeyMute)
{
htmlClickOn("#player_volume");
}
else if (KeyAsInt == Properties.Settings.Default.hotkeyShowHide)
{
showHideWindow();
}
else if (KeyAsInt == Properties.Settings.Default.hotkeyShuffle)
{
htmlClickOn("#player_shuffle");
}
}
private void toolStripMenuItem1_Click(object sender, EventArgs e)
{
OptionsForm optionsDialog = new OptionsForm();
optionsDialog.ShowDialog(this);
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
webBrowser1.GoBack();
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
webBrowser1.GoForward();
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
OptionsForm optionsDialog = new OptionsForm();
optionsDialog.ShowDialog(this);
}
private void About_Click(object sender, EventArgs e)
{
AboutBox1 aboutDialog = new AboutBox1();
aboutDialog.ShowDialog(this);
}
private void currentSongTimer_Tick(object sender, EventArgs e)
{
if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
{
try
{
if (!injectedSongInfoFunctions)
{
HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
string injectedJquery = "function getSongTitle() { return $(\"#playerDetails_nowPlaying .song\").text(); } " +
"function getSongArtist() { return $(\"#playerDetails_nowPlaying .artist\").text(); }" +
"function mouseMove() { $(\"#page_wrapper\").mousemove(); }" +
"function clickElement(selector) { $(selector).click(); }" +
"function getMusicState() {return $(\"#player_play_pause\").hasClass(\"pause\"); }"
;
element.text = injectedJquery;
head.AppendChild(scriptEl);
injectedSongInfoFunctions = true;
}
object songTitle = webBrowser1.Document.InvokeScript("getSongTitle");
object songArtist = webBrowser1.Document.InvokeScript("getSongArtist");
//set the Windows title
if (songTitle.ToString().Length > 0)
{
this.Text = songTitle + " - " + songArtist + " - WinGrooves";
//set the tray icon text if it is less than 63 characters (the max allowed)
if ((songTitle.ToString().Length + songArtist.ToString().Length + 3) < 63)
{
notifyIcon1.Text = songTitle + " - " + songArtist;
}
else
{
try // Get what you can up to max length.
{
notifyIcon1.Text = (songTitle + " - " + songArtist).Substring(0, 62);
}
catch // Possible you land right on and under, throwing exception. handle with old fallback.
{
notifyIcon1.Text = ("WinGrooves");
}
}
}
//control thumbail icons
if (TaskbarManager.IsPlatformSupported)
{
//the element class of the play button on grooveshark changes according to the music state (contains play/paused/nothing)
//I can't figure a better way to control the thumbnail states.
if (Convert.ToBoolean(webBrowser1.Document.InvokeScript("getMusicState")))
{
buttonPause.Icon = Properties.Resources.PlayerPause;
isbuttonPaused = false;
}
else
{
buttonPause.Icon = Properties.Resources.PlayerPlay;
isbuttonPaused = true;
}
}
// HandleThumbnailButtons();
}
catch (NullReferenceException)
{
//this should avoid weird errors
}
}
}
private void toolStripMenuItem1_Click_1(object sender, EventArgs e)
{
OptionsForm optionsDialog = new OptionsForm();
optionsDialog.ShowDialog(this);
}
private void toolStripButton4_Click(object sender, EventArgs e)
{
AboutBox1 aboutDialog = new AboutBox1();
aboutDialog.ShowDialog(this);
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
currentSongTimer.Enabled = true;
}
private void alwaysListeningTimer_Tick(object sender, EventArgs e)
{
if (webBrowser1.ReadyState == WebBrowserReadyState.Complete)
{
//this will simulate moving the mouse so that the player doesn't stop playing music after a few minutes of not interacting with the page
webBrowser1.Document.InvokeScript("mouseMove");
}
}
private void FrmMain_FormClosing(object sender, FormClosingEventArgs e)
{
if (!closingFromTray && Properties.Settings.Default.trayClose)
{
e.Cancel = true;
Hide();
}
}
private void Like_Click(object sender, EventArgs e)
{
LikeCurrentSong();
}
private void LikeCurrentSong()
{
htmlClickOn("#queue_list_window .queue-item-active .smile");
}
private void DislikeCurrentSong()
{
htmlClickOn("#queue_list_window .queue-item-active .frown");
}
private void Dislike_Click(object sender, EventArgs e)
{
DislikeCurrentSong();
}
}
}